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,新建项的菜单里分为上下两部分,上面的“项”指得是在新建一个下一级项;下面的指得是建立项的键,键的类型分为图所示的五种。
图表 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 )方法是指定键的键值。
写入的函数:
CreateSubKey(string name)方法是增加一个子键
SetValue(string 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();
}
}
此例子在vs2005,xp系统下调试通过。
生成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软件的功能了。
核心代码如下:
private void button1_Click(object sender, System.EventArgs e)
{
if(this.richTextBox1.Text=="")
return;
if(this.saveFileDialog1.ShowDialog()==DialogResult.Cancel)
return;
string FileName = this.saveFileDialog1.FileName;
if(FileName.Length<1)
return;
FileName+=".doc";
try
{
Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document doc;
object nothing = System.Reflection.Missing.Value;
doc = word.Documents.Add(ref nothing,ref nothing,ref nothing,ref nothing);
doc.Paragraphs.Last.Range.Text = this.richTextBox1.Text;
object myfileName = FileName;
//将WordDoc文档对象的内容保存为doc文档
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);
//关闭WordDoc文档对象
doc.Close(ref nothing,ref nothing,ref nothing);
//关闭WordApp组件对象
word.Quit(ref nothing,ref nothing,ref nothing);
MessageBox.Show("Word文件保存成功","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch(System.Exception ex)
{
MessageBox.Show(this,ex.Message.ToString(),"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
- C#笔记小综合
- C#综合小问题,经典答复!
- C#的小笔记
- C#综合小问题,经典答复!(一直在更新)
- 综合笔记
- 综合笔记
- 综合笔记
- c#基础学习小笔记
- 事件5:小综合
- 递归方法小综合
- 倒计时小综合
- json数组小综合
- 综合---小知识点
- Angular的小综合
- C# 自学成才综合天.
- 黑马程序员--学习笔记--一个字符串、数组、函数综合运用的小练习
- C++综合笔记
- cocos2d-x 笔记综合
- 要取得非凡成就需要记住的15件事。
- 谈谈C语言的溢出
- 用telnet收发邮件(附bat自动发送代码)
- CentOS 5.4下使用yum安装Xen
- 程序员是这样炼成的(13)-学会“抄袭”
- C#笔记小综合
- document.createElement()用法及注意事项 (ff下不兼容)
- Tomcat默认编码修改
- jrtplib的简单封装类及使用(一)
- jQuery boxy弹出层插件中文演示及讲解
- 淘宝面试归来总结
- linux下samba服务器的架设全过程
- 淘宝面试归来总结
- 淘宝面试归来总结