C#笔记小综合

来源:互联网 发布:网络教育华南理工大学 编辑:程序博客网 时间:2024/05/18 03:02

竖排显示:

竖排1

label1.Text = "竖排文字设计方式" ;

              //这里面一定要保证Label的宽度大于一个字宽而小于两个字宽

label1.Size = new System.Drawing.Size ( (int)label1.Font.Size + 8 , label1.Font.Height * label1.Text.Length ) ;

label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;

label1.AutoSize = false;//一定要设为false

 

竖排2:

PaintEventArgs pe = new PaintEventArgs(label1.CreateGraphics(), label1.ClientRectangle); ;

Graphics g = pe.Graphics;

g.FillRectangle(new SolidBrush(Color.Pink), 10, 10, 100, 400);

g.DrawString(str.ToString(),new Font("Verdana", 20), new SolidBrush(Color.Black), -2, (20) + 2, new StringFormat(StringFormatFlags.DirectionVertical));

 

打开文件对话框

OpenFileDialog ofd = new OpenFileDialog();

ofd.Title = "设置后标题文字:)";

ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Templates);

ofd.Filter = "Text Document(*.txt)|*.txt|All Files|*.*|我要显示的文件类型(*.exe)|*.exe";

if (ofd.ShowDialog() == DialogResult.OK)

{

fName = ofd.FileName;

ts = new StreamReader(fName,System.Text.Encoding.Default);

isFileHaveName = true;

textBox1.AppendText(ts.ReadLine());

}

 

 

DatagridView

正常插入:

dataGridView1.Columns.Add("StuID", "学 号");

dataGridView1.Columns.Add("StuName", "姓 名");

dataGridView1.Rows.Add();

dataGridView1.Rows[0].Cells[0].Value = "1";

dataGridView1.Rows[0].Cells[1].Value = "Hello";

 

倒序插入:

dataGridView1.Rows.Insert(0, new string[] { DateTime.Now.ToString(), "系统开始运行1..." });

dataGridView1.Rows.Insert(0, new string[] { DateTime.Now.ToString(), "系统开始运行2..." });

dataGridView1.Rows.Insert(0, new string[] { DateTime.Now.ToString(), "系统开始运行3..." });

 

打开文件显示到DataGridView

dataGridView1.Columns.Add("StuID", "学 号");

dataGridView1.Columns.Add("StuName", "姓 名");

dataGridView1.Columns[0].Width = 20;

dataGridView1.Columns[1].Width = 500;

//dataGridView1.RowPostPaint += new System.Windows.Forms.DataGridViewRowPostPaintEventHandler(this.dataGridView1_RowPostPaint);

OpenFileDialog ofd = new OpenFileDialog();

ofd.Title = "设置后标题文字:)";

ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Templates);

ofd.Filter = "Text Document(*.txt)|*.txt|All Files|*.*|我要显示的文件类型(*.exe)|*.exe";

int nCount = 0;

if (ofd.ShowDialog() == DialogResult.OK)

{

     fName = ofd.FileName;

     ts = new StreamReader(fName, System.Text.Encoding.Default);

     string str = "";

     while(str != null)

     {

         str = ts.ReadLine();

         dataGridView1.Rows.Add();

         dataGridView1.Rows[nCount].Cells[0].Value = nCount.ToString();

         dataGridView1.Rows[nCount].Cells[1].Value = str;

         nCount++;

     }

}

 

支持BIG5繁体

byte[] byteArray = System.Text.Encoding.Default.GetBytes( str );

str = System.Text.Encoding.GetEncoding("Big5").GetString(byteArray);  

 

C#操作注册表

  一.首先学习一下注册表方面要用到的知识:

我们在注册表编辑器里右键点击一个子项选择“新建”,我们就会看到如图1,新建项的菜单里分为上下两部分,上面的“项”指得是在新建一个下一级项;下面的指得是建立项的键,键的类型分为图所示的五种。

说明: http://www.itwis.com/upimg/allimg/091204/1758080.jpg                                               图表 1

二.接着来熟悉一下C#中修改注册表的方法与函数。

VC#中提供了Registry类、RegistryKey类来实现对注册表的操作。

其中Registry类封装了注册表的七个基本主健:

  Registry.ClassesRoot 对应于HKEY_CLASSES_ROOT主键

  Registry.CurrentUser 对应于HKEY_CURRENT_USER主键

  Registry.LocalMachine 对应于 HKEY_LOCAL_MACHINE主键

  Registry.User 对应于 HKEY_USER主键

  Registry.CurrentConfig 对应于HEKY_CURRENT_CONFIG主键

  Registry.DynDa 对应于HKEY_DYN_DATA主键

  Registry.PerformanceData 对应于HKEY_PERFORMANCE_DATA主键

 

RegistryKey类封装了对注册表的基本操作,包括读取,写入,删除。

其中读取的主要函数有: 

OpenSubKey ( string name )方法主要是打开指定的子键。

GetSubKeyNames ( )方法是获得主键下面的所有子键的名称,它的返回值是一个字符串数组。

GetValueNames ( )方法是获得当前子键中的所有的键名称,它的返回值也是一个字符串数组。

GetValue ( string name )方法是指定键的键值。

写入的函数:

    CreateSubKeystring name)方法是增加一个子键

    SetValuestring name,string value)方法是设置一个键的键值

删除的函数:

  DeleteSubKey ( )方法:删除一个指定的子键。

  DeleteSubKeyTree ( )方法:此方法是彻底删除指定的子键目录,即:删除该子键以及该子键以下的全部子键。

在下面的例子里,要注意:

   建项函数:CreateSubKey()

       建子键函数:SetValue();

class Program

    {

        static string Generateguid()//建立GUID值的函数

        {

            return System.Guid.NewGuid().ToString();

        }

        static void Main(string[] args)

        {

            RegistryKey pregkey;

            string preName;

            //pregkey = Registry.CurrentUser.OpenSubKey("Control Panel//Desktop//WindowMetrics",true);

            pregkey = Registry.LocalMachine.OpenSubKey("SOFTWARE//Microsoft//Internet Explorer//Extensions",true);//以只读方式检索子项

            if (pregkey == null)

            {

                Console.WriteLine("该键值不存在");

              

            }

            else

            {

                preName = Generateguid();

                Console.WriteLine(preName);

                Console.WriteLine("找到该键值");

                preName = "{" + preName + "}";

                //pregkey.SetValue("MinAnimate","1");

                pregkey.CreateSubKey(preName);//建立子项

                pregkey = Registry.LocalMachine.OpenSubKey("SOFTWARE//Microsoft//Internet Explorer//Extensions//"+preName, true);

                //pregkey.SetValue("bbbbb", "0");//建立子键,如果有此子键,则设值。

                pregkey.SetValue("ButtonText", "添加");//鼠标放到bar上时显示的文字

                pregkey.SetValue("CLSID", "{1FBA04EE-3024-11d2-8F1F-0000F87ABD16}");//CLSID固定

                pregkey.SetValue("Default Visible", "yes");//默认可看见

                pregkey.SetValue("Exec", "D:/IEbar/IEpinout.exe");//关联的exe文件

                pregkey.SetValue("HotIcon", "D:/IEbar/Icon2.ico");//鼠标放到bar上时显示的图样

                pregkey.SetValue("Icon", "D:/IEbar/Icon1.ico");//鼠标未放在bar上时显示的图样

                //pregkey.SetValue("MenuStatusBar", "");

                //pregkey.SetValue("MenuText", "我是杨!");

              

            }

            pregkey.Close();

        }

    }

 

此例子在vs2005xp系统下调试通过。

生成exe文件,直接运行,然后打开ie查看工具栏上的效果。

注意:必需要有"D:/IEbar/Icon2.ico""D:/IEbar/Icon1.ico"这两个文件,否则在ie上看不出效果。

 

如何保存文本为Word文件

要在.net中操作Word,就需要在项目中引用Word的对象库文件MSWORD.OLB,这可以在office安装目录下找到(C:"Program Files"Microsoft Office"OFFICE11"MSWORD.OLB,把它引入项目中就可以使用Word对象的各种方法来实现Word软件的功能了。

说明: 06121406.JPG

核心代码如下:

说明: http://www.cnblogs.com/Images/OutliningIndicators/None.gif    private void button1_Click(object sender, System.EventArgs e)
说明: http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif        {
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
if(this.richTextBox1.Text=="")
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                
return;
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
if(this.saveFileDialog1.ShowDialog()==DialogResult.Cancel)
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                
return;
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
string FileName = this.saveFileDialog1.FileName;
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
if(FileName.Length<1)
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                
return;
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif             FileName+=".doc";
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
try
说明: http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            {
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                 Microsoft.Office.Interop.Word.ApplicationClass word =
 new Microsoft.Office.Interop.Word.ApplicationClass();
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                 Microsoft.Office.Interop.Word.Document doc;
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                
object nothing  = System.Reflection.Missing.Value;
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                 doc = word.Documents.Add(
ref nothing,ref nothing,ref nothing,ref nothing);
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                 doc.Paragraphs.Last.Range.Text =
 this.richTextBox1.Text;
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                
object myfileName = FileName;
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
//WordDoc文档对象的内容保存为doc文档
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
                         doc.SaveAs(ref myfileName,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing);
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
//关闭WordDoc文档对象
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
                 doc.Close(ref nothing,ref nothing,ref nothing);
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
//关闭WordApp组件对象
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
                 word.Quit(ref nothing,ref nothing,ref nothing);
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                 MessageBox.Show("Word
文件保存成功","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
说明: http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif             }
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
catch(System.Exception ex)
说明: http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            {
说明: http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                 MessageBox.Show(
this,ex.Message.ToString(),"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
说明: http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif             }
说明: http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif         }
说明: http://www.cnblogs.com/Images/OutliningIndicators/None.gif
说明: http://www.cnblogs.com/Images/OutliningIndicators/None.gif