winform中treeview 透明背景

sunnyfire 2005-09-07 11:35:35
使用C#在winform中如何给treeview添加透明背景?
...全文
1225 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
止戈而立 2005-12-01
  • 打赏
  • 举报
回复
旁听。。
diandian82 2005-12-01
  • 打赏
  • 举报
回复
树结点的背景色能不能改变?
diandian82 2005-12-01
  • 打赏
  • 举报
回复
但我发现,背景透明了以后就不能选中节点了
diandian82 2005-12-01
  • 打赏
  • 举报
回复
我试验了一下,果然可以。
this.treeView1.BackColor = Color.Tan;
this.TransparencyKey = Color.Tan;
阿非 2005-12-01
  • 打赏
  • 举报
回复
mark
jonnyyu 2005-11-30
  • 打赏
  • 举报
回复
lee_j你的方法好像不是一般意义上的透明背景的treeview,而是整个form都透明了
lee_j 2005-11-30
  • 打赏
  • 举报
回复
先把TreeView的BackColor设成某一个颜色:
treeView1.BackColor = Color.Tan;

然后设Form的TransparencyKey成为这个颜色:
Form1.TransparencyKey = treeView1.BackColor;
seekg 2005-11-30
  • 打赏
  • 举报
回复
旁听
jimh 2005-11-30
  • 打赏
  • 举报
回复
控件里
使用this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); 透明背景是不可实现的,因为TreeView,ListView内部使用了很多东西来加速滚动显示,会把背景一起滚动,如果真的要实现,响应onpaint事件,先画背景,然后分别发送DrawItem事件到每一个可见的Item是比较可行的方案,不过我没有试过。
neilwang 2005-11-30
  • 打赏
  • 举报
回复
从底层写也难?如何指定一个子窗口(treeview)的背景为透明的?
Knight94 2005-11-30
  • 打赏
  • 举报
回复
很难直接实现,如果你非要实现的话,建议重新写一个treeview,从底层写起
diandian82 2005-11-30
  • 打赏
  • 举报
回复
private void Form1_Load(object sender, System.EventArgs e)
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
}

这样不能把窗体弄成透明的啊?到底怎么弄阿?
chenyangwqq828 2005-11-30
  • 打赏
  • 举报
回复
这样做是不行的,要重画TreeNode才能实现。
julong88 2005-09-13
  • 打赏
  • 举报
回复
我在.net cf里试过onpaint()没效果的
onpaint()
singlepine 2005-09-08
  • 打赏
  • 举报
回复
使控件拥有透明背景色:
在控件的代码编辑器中找到构造函数。
在构造函数中调用窗体的 SetStyle。
' Visual Basic
SetStyle(ControlStyles.SupportsTransparentBackColor, True)
// C#
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
这将使控件能够支持透明背景色。
在步骤 1 中添加的代码行下再添加下面的代码行。这将把控件的 BackColor 设置为 Transparent。
' Visual Basic
Me.BackColor = Color.Transparent
// C#
this.BackColor = Color.Transparent;
注意,还可以通过使用 Color.FromArgb 方法创建半透明颜色。
sunnyfire 2005-09-08
  • 打赏
  • 举报
回复
这个不可能实现
jamesfay 2005-09-08
  • 打赏
  • 举报
回复
我没试过,可singlepine(小山)说的貌似可以阿,在后面加个背景不系好了吗?
littlekeen 2005-09-08
  • 打赏
  • 举报
回复
已经封装好的控件估计不好改了,你想自定义控件?
好象是改写onpaint()
julong88 2005-09-08
  • 打赏
  • 举报
回复
我估计也不可能
像这样的控件还有listview
没法下载,到这里折腾一把试试。 本文由abc2253130贡献 doc文档可能在WAP端浏览体验不佳。建议您优先选择TXT,或下载源文件到本机查看。 C#(WINFORM)学习 一、 C#基础 基础 类型和变量 类型和变量 类型 C# 支持两种类型:“值类型”和“引用类型”。值类型包括简单类型(如 char、int 和 float 等)、枚举类型和结构类型。引用类型包括类 (Class)类 型、接口类型、委托类型和数组类型。 变量的类型声明 变量的类型声明 每个变量必须预先声明其类型。如 int a; int b = 100; float j = 4.5; string s1; 用 object 可以表示所有的类型。 预定义类型 下表列出了预定义类型,并说明如何使用。 类型 object 说明 所有其他类型的最终 基类型 字符串类型; 字符串是 Unicode 字符序列 8 位有符号整型 16 位有符号整型 32 位有符号整型 64 位有符号整型 示例 object o = null; 范围 string sbyte short int long string s = "hello"; sbyte val = 12; short val = 12; int val = 12; long val1 = 12; -128 到 127 -32,768 到 32,767 -2,147,483,648 2,147,483,647 -9,223,372,036,854,775,808 到 第1页 C#(WINFORM)学习 long val2 = 34L; 到 9,223,372,036,854,775,807 byte ushort 8 位无符号整型 16 位无符号整型 byte val1 = 12; ushort val1 = 12; uint val1 = 12; uint 32 位无符号整型 uint val2 = 34U; ulong val1 = 12; ulong val2 = 34U; ulong 64 位无符号整型 ulong val3 = 56L; ulong val4 = 78UL; float 单精度浮点型 float val = 1.23F;7 位 double val1 = 1.23; double 双精度浮点型 double val2 = ±5.0 × 10?324 ±1.7 × 10 308 0 到 255 0 到 65,535 0 到 4,294,967,295 0 到 18,446,744,073,709,551,615 ±1.5 × 10?45 ±3.4 × 10 38 到 到 4.56D;15-16 布尔型;bool 值或为 真或为假 字符类型;char 值是 一个 Unicode 字符 精确的小数类型, 具有 28 个有效数字 bool val1 = true; bool val2 = false; char val = 'h'; decimal val = bool char decimal DateTime ±1.0 × 10?28 ±7.9 × 10 28 到 1.23M;28-29 变量转换 简单转换: float f = 100.1234f; 可以用括号转换: short s = (short)f 也可以利用 Convert 方法来转换: string s1; s1=Convert.ToString(a); MessageBox.Show(s1); 常用 Convert 方法有: 第2页 C#(WINFORM)学习 C# Convert.ToBoolean Convert.ToByte Convert.ToChar Convert.ToDateTime Convert.ToDecimal Convert.ToDouble Convert.ToInt16 Convert.ToInt32 Convert.ToInt64 Convert.ToSByte Convert.ToSingle Convert.ToString Convert.ToUInt16 Convert.ToUInt32 Convert.ToUInt64 备注 Math 类 常用科学计算方法: C# Math.Abs Math.Sqrt Math.Round Math.Floor Math.Cos Math.Sin Math.Tan Math.Exp Math.Log Math.Pow(x,y) Math.Max(x,y) 备注 绝对值 开方 取整,四舍五入 取整,放弃小数 余弦 正弦 正切 返回 e 的指定次幂 对数 数字 x 的 y 次幂 返回较大者 第3页 C#(WINFORM)学习 Math.Min(x,y) 返回较小者 枚举型 一般为字符串,可以定义带数字的枚举型,示例为: enum Color { Red=1, Blue=2, Green=3 } class Shape { public int Fill(Color color) { int ii; switch(color) { case Color.Red: ii=10; break; case Color.Blue: ii=11; break; case Color.Green: ii=12; break; default: ii=-1; break; } return ii; } } private void button1_Click(object sender, System.EventArgs e) { int i; Shape s1=new Shape(); i=s1.Fill((Color)2); //i=s1.Fill(Color.Blue); MessageBox.Show(i.ToString()); 第4页 C#(WINFORM)学习 } Enum 需要放在 class 外面,才能被其它 class 的程序调用。 C#关键字 关键字 abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach goto if implicit in int interface internal is lock long namespace new struct null switch object this operator throw out true override try params typeof private uint protected ulong public unchecked readonly unsafe ref ushort return using sbyte virtual sealed volatile short void sizeof while stackalloc static string 数组 定义 数组是一种排列有序的数据结构,包含于数组的变量被称为数组的元素, 它们都有相同的类型。 数组声明 int [] array1 = new int[5]; int [,,] array3 = new int[10,20,30]; int [] array1 = new int[] {1,2,4}; 第5页 C#(WINFORM)学习 数组引用 array1[0]="a1"; 注意,如果定义数组为 int[5] ,则从 0~4。 数组长度 line0.GetLength(1) 数组赋值 可以从一个已经赋值的数组 array2 向未赋值的同等数组 array1 赋值,用 array1=array2; 这时,array1 就变成和 array2 一样的数组了。 集合 集合的使用 集合可以看成是可以随意添加的数组,因此凡是在使用数组的场合,都可以 使用集合。而且集合的元素可以是任意对象,操作也比数组灵活的多。 使用集合时,必须注意集合的生命期问题。如果有两个集合 L1 和 L2,使用 了 L1=L2; 后,只要 L2 生命期没有终结,它的以后的变化就可能会影响到 L1 的数值。因 此在赋值后应该及时销毁或者初始化 L2,以免发生不可预见的错误。 比较 使用 Contains 方法。 ArrayList Array1=new ArrayList(); Array1.Add("as"); bool b1=Array1.Contains("as"); MessageBox.Show(b1.ToString()); 第6页 C#(WINFORM)学习 找到集合数量最多的一个元素 利用方法来查找,可以返回两个变量。 object Jmax0(ArrayList v11,ref int jj) { int i; object j0=0; ArrayList y11=new ArrayList(); //各个不同的元素的集合 int [] y12=new int[v11.Count]; //记录各个元素数量的数组 int xmax=0; //最大的一个元素的数量 for (i=0;iWINFORM)学习 double j0=0; object j1=0; j0=2.3; Array1.Add(j0); j0=2.3; Array1.Add(j0); j0=1.000f; Array1.Add(j0); j0=2.3; Array1.Add(j0); j0=1; Array1.Add(j0); j1=Jmax0(Array1,ref jj); MessageBox.Show(j1.ToString()+" "+jj.ToString()); } 运算符和判断 判断 if (x > 10) if (y > 20) Console.Write("Statement_1"); else Console.Write("Statement_2"); 关系运算符 <,,>= 等于:== 不等于:!= 判断字符串 string 和 char 用 Equals 方法。 逻辑运算符 与:a & b 或:a | b 第8页 C#(WINFORM)学习 非:! A 模数运算符 模数运算符 (%) 计算第二个操作数除第一个操作数后的余数。所有数值类 型都具有预定义的模数运算符。如 Console.WriteLine(5 % 2); Console.WriteLine(-5 % 2); Console.WriteLine(5.0 % 2.2); Console.WriteLine(-5.2 % 2.0); // =1 // =-1 // =0.6 // =-1.2 经常用模数运算符来判断整数为奇数(=1)或偶数(=0) 。 循环 无条件循环 int sum,x; sum=0; for(x=1;x<=100;x++) { sum+=x; } 有条件循环 private void button1_Click(object sender, System.EventArgs e) { int sum=0; int x=0; while ((sum<100) & (x<20)) { x++; sum+=x; } string s2=Convert.ToString(x); MessageBox.Show(s2); } 运行显示 14。 第9页 C#(WINFORM)学习 如果改为 while ((sum<100) | (x<20)) 运行显示 20。 多重选择 switch (i) { case 0: CaseZero(); break; case 1: CaseOne(); break; default: CaseOthers(); break; } 每个 case 后面,必须有 break 或者 goto,不允许贯穿。 Goto goto 语句将程序控制直接传递给标记语句。 for (int i = 0; i < x; i++) for (int j = 0; j =this.Width-10) { otherForm.Show(); otherForm.L1.Top=label1.Top; this.Hide(); } } private void button1_Click(object sender, System.EventArgs e) { label1.Left=label1.Left-10; } private void button3_Click(object sender, System.EventArgs e) { label1.Top=label1.Top-10; } private void button4_Click(object sender, System.EventArgs e) { label1.Top=label1.Top+10; } private void button5_Click(object sender, System.EventArgs e) { Application.Exit(); } 同样在 Form2 的 InitializeComponent()下面加上窗体定位语句: Point tempPoint = new Point(300,100); this.DesktopLocation = tempPoint; 然后把 Form2 的 StartPosition 属性改为 Manual。其余程序为: public Label L1 { get { return label1; } set { label1=value; 第 22 页 C#(WINFORM)学习 } } private void button2_Click(object sender, System.EventArgs e) { label1.Left=label1.Left+10; } private void button1_Click(object sender, System.EventArgs e) { Form1 otherForm1=new Form1(); label1.Left=label1.Left-10; if (label1.Left<=-10) { otherForm1.Show(); otherForm1.L2.Top=label1.Top; otherForm1.L2.Left=otherForm1.Width-20; this.Hide(); } } private void button3_Click(object sender, System.EventArgs e) { label1.Top=label1.Top-10; } private void button4_Click(object sender, System.EventArgs e) { label1.Top=label1.Top+10; } 动态产生窗体 public void CreateMyForm() { Form form1 = new Form(); Label label1 = new Label(); Button button1 = new Button (); TextBox text1 = new TextBox(); button1.Text = "确定"; button1.Location = new Point (110, 220); label1.Location = new Point (50,100); text1.Location = new Point (150,100); 第 23 页 C#(WINFORM)学习 form1.Text = "请输入"; label1.Text = "数据"; form1.FormBorderStyle = FormBorderStyle.FixedDialog; form1.ControlBox = false; form1.CancelButton = button1; form1.StartPosition = FormStartPosition.CenterScreen; form1.Controls.Add(button1); form1.Controls.Add(text1); form1.Controls.Add(label1); form1.ShowDialog(); ls=text1.Text; } private void button2_Click(object sender, System.EventArgs e) { CreateMyForm(); MessageBox.Show(ls); } ToolBar 普通使用 在窗体上加上 ToolBar 界面修改后的问题 在界面上修改后,最后要加上: toolBar1.Buttons.Add(toolBarButton1); toolBar1.Buttons.Add(toolBarButton2); toolBar1.Buttons.Add(toolBarButton3); // Add the event-handler delegate. toolBar1.ButtonClick (this.toolBar1_ButtonClick); += new ToolBarButtonClickEventHandler 或者把原有的程序 第 24 页 C#(WINFORM)学习 this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] { this.toolBarButton1,this.toolBarButton2,this.toolBarButton3}); 改变位置,到 toolBar1 设置的最下面。 全部设置程序为: this.toolBar1.DropDownArrows = true; this.toolBar1.Location = new System.Drawing.Point(0, 0); this.toolBar1.Name = "toolBar1"; this.toolBar1.ShowToolTips = true; this.toolBar1.Size = new System.Drawing.Size(592, 42); this.toolBar1.TabIndex = 0; toolBar1.ButtonSize = new System.Drawing.Size(60, 50); // // toolBarButton1 // this.toolBarButton1.Text = "Open"; toolBarButton1 // // toolBarButton2 // this.toolBarButton2.Text = "Save"; toolBarButton2 // // toolBarButton3 // this.toolBarButton3.Text = "Print"; toolBar1.Buttons.Add(toolBarButton1); toolBar1.Buttons.Add(toolBarButton2); toolBar1.Buttons.Add(toolBarButton3); toolBar1.ButtonClick (this.toolBar1_ButtonClick); += new ToolBarButtonClickEventHandler 设置按钮大小 如下设置,可以正常居显示 9 号字体。 toolBar1.ButtonSize = new System.Drawing.Size(60, 50); 用程序实现 可以用程序实现按钮的增加,但是无法全部实现自动化。 第 25 页 C#(WINFORM)学习 先需要手工添加 toolBar1 和 imageList1,然后把 imageList1 的图片一一加 上。 void toolBarSet() { //添加按钮 ToolBarButton toolBarButton1=new ToolBarButton(); ToolBarButton toolBarButton2=new ToolBarButton(); toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] { toolBarButton1,toolBarButton2}); toolBar1.DropDownArrows = true; toolBar1.ImageList = imageList1; toolBar1.Size = new System.Drawing.Size(408, 37); toolBar1.TabIndex = 0; toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(toolBar1_ButtonClick); // toolBarButton1 toolBarButton1.ImageIndex = 0; toolBarButton1.ToolTipText = "放大"; // toolBarButton2 toolBarButton2.ImageIndex = 1; toolBarButton2.ToolTipText = "缩小"; } private void Form1_Load(object sender, System.EventArgs e) { toolBarSet(); } private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) { switch(toolBar1.Buttons.IndexOf(e.Button)) { case 0: //放大 MessageBox.Show("放大"); break; case 1: //缩小 MessageBox.Show("缩小"); break; default: MessageBox.Show("other"); break; 第 26 页 C#(WINFORM)学习 } } listBox 普通调用 在窗体上放置一个 listBox1,一个 button1 和一个 label1。以下程序实现添加 选项,双击选项就可以显示你的选择: private void button1_Click(object sender, System.EventArgs e) { listBox1.Items.Clear(); listBox1.Items.Add(""); listBox1.Items.Add("选择1"); listBox1.Items.Add("选择2"); listBox1.SelectedIndex=0; } private void listBox1_DoubleClick(object sender, System.EventArgs e) { Label1.Text=listBox1.SelectedIndex.ToString(); } 第一项是一个缺省空项,允许用户不选取退出。 Items 是一个集合,因此增减选项可以按照集合那样操作。 用数组添加选项 System.Object[] ItemObject = new System.Object[10]; for (int i = 0; i <= 9; i++) { ItemObject[i] = "Item" + i; } listBox1.Items.AddRange(ItemObject); 第 27 页 C#(WINFORM)学习 ScrollBar 基本定义 ScrollBar 是滚动条控件,分成 HScrollBar(水平)和 VScrollBar(垂直)两 种。有些控件如 ListBox,TextBox 等可以自动添加滚动条,但是有些控件则需 要用程序添加。主要属性意义为: Value:滚动条的数值,反映当前移动块的位置。初始值设定后,运行时停 留在这个位置。运行时拉动滚动条,由 Scroll 事件的 e.NewValue 参数传递过来。 Maximum:Value 的最大值,一般为 100。 Minimum: Value 的最小值, 即端点的数值。 如果 Maximum=100, Minimum=0, LargeChange=10,则从第一个端点开始 Value=0,到另一个端点的 Value=91。 SmallChange:每次点击移动的数值,一般为 1。 LargeChange:移动块的长度,一般为 10。 控件一起使用 和 PicturBox 控件一起使用 float vi; //每个单位的移动距离 float vk=0.8f; //PicturBox显示高度和实际高度的比例 int t0,ti; //PicturBox显示Top和Height。 private void vScrollBar1_Scroll(object sender,System.Windows.Forms.ScrollEventArgs e) { this.pictureBox1.Top = t0-Convert.ToInt32(e.NewValue*vi); this.pictureBox1.Height = ti+Convert.ToInt32(e.NewValue*vi); } private void button1_Click(object sender, System.EventArgs e) { Button oButton; TextBox oTextBox; for(int i=1;i<=8;i++) { oButton = new Button(); oButton.Text = "按钮"+ i.ToString(); oButton.Location = new System.Drawing.Point(50, i*50); oButton.Click += new System.EventHandler(oButton_Click); 第 28 页 C#(WINFORM)学习 this.pictureBox1.Controls.Add(oButton); oTextBox = new TextBox(); oButton.Tag = oTextBox; oTextBox.Text = "1000"; oTextBox.Location = new System.Drawing.Point(150, i*50); this.pictureBox1.Controls.Add(oTextBox); } } private void oButton_Click(object sender, System.EventArgs e) { Button btn = (Button)sender; TextBox txt = (TextBox)btn.Tag; txt.Text = Convert.ToString(Convert.ToInt32(txt.Text) + 1); } private void Form1_Load(object sender, System.EventArgs e) { vi=vk*pictureBox1.Height/vScrollBar1.Maximum; t0=pictureBox1.Top; ti=pictureBox1.Height; } Panel 基本定义 Windows 窗体 Panel(面板)控件用于为其他控件提供可识别的分组。在设 计时所有控件均可轻松地移动,当移动 Panel 控件时,它包含的所有控件也将 移动。分组在一个面板的控件可以通过面板的 Controls 属性进行访问。 Panel 控件类似于 GroupBox 控件;但只有 Panel 控件可以有滚动条,而 且只有 GroupBox 控件显示标题。 将 AutoScroll 属性设置为 true,可以自动显示滚动条。但是这时右边界和 下边界顶头,不是太好看。这时需要增加一个不可见的控件或者图像来调整。 下例在 Panel 上用程序添加几个控件,产生滚动效果: private void button1_Click(object sender, System.EventArgs e) 第 29 页 C#(WINFORM)学习 { Button oButton; TextBox oTextBox; for(int i=1;i<=8;i++) { oButton = new Button(); oButton.Text = "按钮"+ i.ToString(); oButton.Location = new System.Drawing.Point(50, i*50); oButton.Click += new System.EventHandler(oButton_Click); this.panel1.Controls.Add(oButton); oTextBox = new TextBox(); oButton.Tag = oTextBox; oTextBox.Text = "1000"; oTextBox.Location = new System.Drawing.Point(150, i*50); this.panel1.Controls.Add(oTextBox); } //增加一个不可见按钮,调整右边界和下边界的位置 oButton = new Button(); oButton.Location = new System.Drawing.Point(260, 440); oButton.Height=0; oButton.Width=0; this.panel1.Controls.Add(oButton); } private void oButton_Click(object sender, System.EventArgs e) { Button btn = (Button)sender; TextBox txt = (TextBox)btn.Tag; txt.Text = Convert.ToString(Convert.ToInt32(txt.Text) + 1); } 控件上添加图像 在 Panel 控件上添加图像 在 Panel 控件上不能直接添加图像。 需要在 Panel 控件上添加一个 picturBox, 然后把其 SizeMode 设置为 AutoSize(随着图像大小调整控件大小)就可以实现 图像的随意滚动察看。 控件上画图 在 Panel 控件上画图 Panel 控件上也可以画图。但是滚动时遮盖的图像就消失了。这时候需要在 Panel 控 件 上 添 加 一 个 picturBox , 然 后 在 picturBox 上 画 图 , 然 后 用 一 个 LocationChanged 事件,每次滚动时重画一遍即可: 第 30 页 C#(WINFORM)学习 Pen pen1=new Pen(Color.Green,2); Graphics g1; void drawLine() { PointF p1=new PointF(0,0); PointF p2=new PointF(100,100); g1.DrawLine(pen1,p1,p2); } private void button2_Click(object sender, System.EventArgs e) { g1=this.pictureBox1.CreateGraphics(); drawLine(); } private void pictureBox1_LocationChanged(object sender, System.EventArgs e) { drawLine(); } 菜单 普通应用 手工添加即可。可以直接在其上写各个菜单项的名字,双击可以添加程序, 使用非常方便。 特殊功能 1.在设计时向菜单项添加选标记 对于在“菜单设计器”内选定的菜单项(三级菜单以下) ,单击该菜单项左侧 的区域,选标记√。或者在“属性”窗口将 Checked 属性设置为 True。 以编程方式向菜单项添加选标记 myMnuItem.Checked = true; 2.在设计时向菜单项添加快捷键 在“菜单设计器”内选择菜单项。在“属性”窗口,将 Shortcut 属性设置为 第 31 页 C#(WINFORM)学习 下拉列表提供的值之一。 以编程方式向菜单项添加快捷键 myMnuItem.Shortcut = System.Windows.Forms.Shortcut.F6; 3.向菜单项添加访问键 如键入“文件(&F)” ,显示“文件(F)” 。 若要定位到此菜单项,请按 ALT 键,将焦点移动到菜单栏,然后按该菜单 名称的访问键。当菜单打开并显示带访问键的项时,只需按该访问键就可选定该 菜单项。或者直接按 ALT+主菜单的访问键。 4.将分隔线作为菜单项添加 在菜单设计器,右击需要有分隔线的位置,然后选择“插入分隔符”。或者 在设置菜单项的 Text 属性(在“属性”窗口、菜单设计器或代码)时,输 入短划线 (–) 使该菜单项成为分隔线。 其它控件 单选按钮 单选按钮是布置一组按钮,只能选择一组控件。 本例放置 3 个单选按钮,Text 属性分别写上“已婚”“未婚”和“离异” 、 , 然后添加一个 Label 控件和一个 Button 控件,程序如下: public Form1() { InitializeComponent(); label1.Text="请选择"; …… private void button1_Click(object sender, System.EventArgs e) { if (radioButton1.Checked == true) label1.Text=radioButton1.Text; else if (radioButton2.Checked == true) label1.Text=radioButton2.Text; else label1.Text=radioButton3.Text; } 第 32 页 C#(WINFORM)学习 } 复选框 可以选择多个的一组控件。 本例放置 2 个复选按钮,Text 属性分别写上“加粗”和“斜体” ,然后添加 一个 Label 控件和一个 Button 控件,程序如下: private void button1_Click(object sender, System.EventArgs e) { if (checkBox1.Checked == true) { if (checkBox2.Checked == true) label1.Text=checkBox1.Text+checkBox2.Text; else if (checkBox2.Checked == false) label1.Text=checkBox1.Text; } else if (checkBox2.Checked == true) label1.Text=checkBox2.Text; else if (checkBox2.Checked == false) label1.Text=""; } 第 33 页 C#(WINFORM)学习 程序产生 checkBox CheckBox checkBox1=new CheckBox(); void checkSet() { this.Controls.Add(checkBox1); checkBox1.Location = new System.Drawing.Point(50, 64); checkBox1.Name = "checkBox1"; checkBox1.TabIndex = 2; checkBox1.Text = "图层1"; checkBox1.CheckedChanged += new System.EventHandler(checkBox1_CheckedChanged); } private void button1_Click(object sender, System.EventArgs e) { checkSet(); } private void checkBox1_CheckedChanged(object sender, System.EventArgs e) { if (checkBox1.Checked) MessageBox.Show("yes"); else MessageBox.Show("no"); } 如果要实现标题在左边,用 check1.Width=90; check1.CheckAlign=ContentAlignment.MiddleRight; 要在其它控件显示: check3.BringToFront(); 动态产生控件 以下程序动态动态产生一组 Button 和 TextBox 控件,以及点击 Button 的事 件。 private void button2_Click(object sender, System.EventArgs e) { Button oButton; TextBox oTextBox; 第 34 页 C#(WINFORM)学习 for(int i=1;i<=5;i++) { oButton = new Button(); oButton.Text = "按钮"+ i.ToString(); oButton.Location = new System.Drawing.Point(50, i*50); oButton.Click += new System.EventHandler(oButton_Click); this.Controls.Add(oButton); oTextBox = new TextBox(); oButton.Tag = oTextBox; oTextBox.Text = "1000"; oTextBox.Location = new System.Drawing.Point(150, i*50); this.Controls.Add(oTextBox); } } private void oButton_Click(object sender, System.EventArgs e) { Button btn = (Button)sender; TextBox txt = (TextBox)btn.Tag; txt.Text = Convert.ToString(Convert.ToInt32(txt.Text) + 1); } Splitter Windows 窗体 splitter 控件用于在运行时调整停靠控件的大小。 Splitter 控 件常用于一类窗体,这类窗体上的控件所显示的数据长度可变,如 Windows 资 源管理器,它的数据窗格所包含的信息在不同的时间有不同的宽度。 如果一个控件可由 splitter 控件调整其大小,则当用户将鼠标指针指向该控 件的未停靠的边缘时,鼠标指针将更改外观,指示该控件的大小是可以调整的。 拆分控件允许用户调整该控件紧前面的停靠控件的大小。因此,为使用户能够在 运行时调整停靠控件的大小,请将要调整大小的控件停靠在容器的一条边缘上, 然后将拆分控件停靠在该容器的同一侧。 以下例子自动产生几个控件,可以在运行调整大小。 private void CreateMySplitControls() { TreeView treeView1 = new TreeView(); ListView listView1 = new ListView(); Splitter splitter1 = new Splitter(); 第 35 页 C#(WINFORM)学习 treeView1.Dock = DockStyle.Left; splitter1.Dock = DockStyle.Left; splitter1.MinExtra = 100; splitter1.MinSize = 75; listView1.Dock = DockStyle.Fill; treeView1.Nodes.Add("TreeView Node"); listView1.Items.Add("ListView Item"); this.Controls.AddRange(new Control[]{listView1, splitter1, treeView1}); } private void button1_Click(object sender, System.EventArgs e) { CreateMySplitControls(); } tabControl Windows 窗体 TabControl 显示多个选项卡。 使用时, 先添加一个 TabControl 控件,把它拉的足够大。 然后在属性添加按钮。每个按钮可以控制 TabControl 的其余页面, 作为一 个容器,可以添加其它空间。运行时只要点击按钮,就可以切换选项卡,实现不 同的功能。 StatusBar 可以向 statusBar 添加面板(窗格) ,以分类显示信息: public void CreateStatusBarPanels() { statusBar1.Panels.Add(""); statusBar1.Panels.Add("Two"); statusBar1.Panels.Add("Three"); statusBar1.Panels[0].Width=200; statusBar1.Panels[0].Text="One"; statusBar1.ShowPanels = true; } 第 36 页 C#(WINFORM)学习 三、字符和字符串 字符和字符串 字符串的操作在程序设计非常有用,因此单独写成一章。 Char 基本定义 char 关键字用于声明一个字符。 char 类型的常数可以写成字符、十六进制换码序列或 Unicode 表示形式。 您也可以显式转换整数字符代码。 以下所有语句均声明了一个 char 变量并用字 符 X 将其初始化: char MyChar = 'X'; char MyChar = '\x0058'; char MyChar = (char)88; char MyChar = '\u0058'; // Character literal // Hexadecimal // Cast from integral type // Unicode char 类型可隐式转换为 ushort、int、uint、long、ulong、float、double 或 decimal 类型。但是,不存在从其他类型到 char 类型的隐式转换。 ToCharArray 将字符串的部分字符复制到 Unicode 字符数组。示例 string str = "012wxyz789"; char[] arr; arr = str.ToCharArray(3, 4); 显示:wxyz 计算字符串宽度 由于英文和文的显示长度不一样,所以一些场合要区分。 要引用 using System.Globalization; 第 37 页 C#(WINFORM)学习 程序为: //计算一个字符的字符类型,=0汉字,=1英文 private int getCharType(char ch) { int i0; UnicodeCategory ca1=new UnicodeCategory(); ca1=System.Char.GetUnicodeCategory(ch); switch (ca1) { case UnicodeCategory.OtherPunctuation: i0=0; //汉字 break; case UnicodeCategory.OtherLetter: i0=0; //汉字 break; case UnicodeCategory.FinalQuotePunctuation: i0=0; //汉字 break; default: i0=1; //英文 break; } return i0; } //计算字符串(ss,包含文)的实际宽度(返回)、起点(x0)和高度(height) //输入字号sz,只对于Pixel单位 public float StringWidth(string ss,float sz,ref float x0,ref float height) { char ch1; int i,i0=0; float width=0; float k1=1.02f; //汉字系数 float k2=0.55f; //英文系数 float k3=0.15f; //x0系数 float k4=1.10f; //高度系数 int i1=0; //汉字个数 int i2=0; //英文个数 height=k4*sz; x0=sz*k3; for(i=0;iWINFORM)学习 ch1=(char)ss[i]; i0=getCharType(ch1); if(i0==0) i1++; else i2++; } width=x0+i1*k1*sz+i2*k2*sz; return width; } //返回一个point单位的字体的宽度 public float PStringWidth(string ss,float sz,ref float x0,ref float height) { float width=0; sz=sz*20/15; width=StringWidth(ss,sz,ref x0,ref height); return width; } 这个方法在 sz(字体大小)5~30 内比较准确。很大时有误差。 计算字符串心 //根据给定点,找到实际标注点,使得以画出的字符串以给定点为心 PointF StringCenter(string s1,int sz,PointF p0) { PointF p1=new PointF(); float x0=0; float height=0; float width=StringWidth(s1,sz,ref x0,ref height); p1.X=p0.X-+x0-width/2; p1.Y=p0.Y-height/2; return p1; } 计算字符串尺寸示例 1—画方框 — 以下示例利用以上方法,把字符串的长度和高度画成一个方框。 private void button2_Click(object sender, System.EventArgs e) { Graphics g= this.CreateGraphics(); SolidBrush myBrush=new SolidBrush(Color.Red); float x0=0; 第 39 页 C#(WINFORM)学习 float height=0; int sz=10; float px=0,py=50; PointF p0=new PointF(px,py); string s1="我们还34fd还是和平使者"; Font myFont1 = new Font("宋体 ",sz,FontStyle.Bold,GraphicsUnit.Pixel); float width=StringWidth(s1,sz,ref x0,ref height); g.DrawString(s1, myFont1, myBrush, p0); PointF p1=new PointF(px+x0,py); PointF p2=new PointF(px+x0+width,py); PointF p3=new PointF(px+x0+width,py+height); PointF p4=new PointF(px+x0,py+height); PointF[] cur ={p1,p2,p3,p4}; Pen pen1=new Pen(Color.Blue,2); g.DrawPolygon(pen1,cur); } 计算字符串尺寸示例 2—找点 — private void button1_Click(object sender, System.EventArgs e) { Graphics g= this.CreateGraphics(); SolidBrush myBrush=new SolidBrush(Color.Red); PointF ps=new PointF(); int sz=10; PointF p0=new PointF(300,100); string s1="我们还34fd还是和平使者"; Font myFont1 = new Font("宋体 ",sz,FontStyle.Bold,GraphicsUnit.Pixel); ps=StringCenter(s1,sz,p0); g.DrawString(s1, myFont1, myBrush, ps); //以下画十字线表示心位置 PointF p1=new PointF(0,p0.Y); PointF p2=new PointF(600,p0.Y); PointF p3=new PointF(p0.X,0); PointF p4=new PointF(p0.X,300); Pen pen1=new Pen(Color.Blue,1); g.DrawLine(pen1,p1,p2); g.DrawLine(pen1,p3,p4); } 第 40 页 C#(WINFORM)学习 分行操作 回车符和换行符 回车符和换行符 “\r\n” 显示换行的语句为: textBox1.Text="ok\\r\\n"; textBox1.Text+="ok1"; 字符串分行 string myString1 = "This is the first line of my string.\n" + "This is the second line of my string.\n" + "This is the third line of the string.\n"; string myString2 = @"This is the first line of my string. This is the second line of my string. This is the third line of the string."; 字符串操作 字符串表示 用@后边的字符串不被处理。 A1=@"c:\Docs\Source\a.txt"; string s1=@"c=""a.txt"; //显示:c=”a.txt string s1=@"c=""a.txt"""; //显示:c=”a.txt” If (s1 == @"""") Then //s1="" 求字符串长度 string s1="fdkls我们"; string s2=Convert.ToString(s1.Length); MessageBox.Show(s2); 第 41 页 C#(WINFORM)学习 运行显示为 7,所有字符个数。 裁剪字符串 String s = "123abc456"; Console.WriteLine(s.Remove(3, 3)); 打印“123456”。 Split 方法 标识此实例的子字符串 (它们由数组指定的一个或多个字符进行分隔) , 然后将这些子字符串放入一个 String 数组。 简单的例子 可以按照“, ”分开,也可以去除空格。 private void button1_Click(object sender, System.EventArgs e) { string astring="123,456 78,789"; string [] split; Char [] chr=new Char [] {',',' '}; split = astring.Split(chr); MessageBox.Show("/"+split[0]+"/"); } 这时可以分成 123,456,78,789 四个字符串。 注意,前后空白也可以看成是一个字符串,要消除,用 astring=astring.Trim(); 就可以了。 复杂的例子 当存在两个空格时,就出现找出空字符串的错误。用以下方法可以去掉空的 字符串: string [] Split0(string [] sp) { string [] sp1=new string [sp.Length]; int i=0,j=0; foreach (string s1 in sp) { 第 42 页 C#(WINFORM)学习 if (s1!="") { sp1[i]=s1; i=i+1; } } string [] sp2=new string [i]; for (j=0;jWINFORM)学习 五、文件操作 文件操作 文件操作 删除 以下均为在控制台应用程序使用的程序。开始要进行以下三个引用: using System; using System.IO; using System.Text; 程序如下: class Test { public static void Main() { string path = @"d:\chen\MyTest.txt"; if (File.Exists(path)) { File.Delete(path); } } } 如果采用相对路径,用 string sfile = Directory.GetCurrentDirectory() + @"\F8.txt"; 对于在一个.net 项目,默认的路径是在\bin\debug ,如果要放在项目文 件目录,用 string sfile = Directory.GetCurrentDirectory() + @"\..\..\F8.txt"; 生成新文件 可以用: File.Create(path); 或者 FileStream fs = File.Create(path); 第 44 页 C#(WINFORM)学习 复制 File.Copy(path,path1,true); 如果最后用 false,则第二个文件有时,发生错误。 读写文本文件 using System; using System.IO; private void button1_Click(object sender, System.EventArgs e) { string s1 = @"D:\chen\mytest.txt"; string s2 = @"D:\chen\mytest1.txt"; String input; if (!File.Exists(s1)) { MessageBox.Show("File does not exist."); return; } StreamReader sr = File.OpenText(s1); StreamWriter wr = new StreamWriter(s2); while ((input=sr.ReadLine())!=null) { wr.WriteLine(input); } MessageBox.Show("The end."); sr.Close(); wr.Close(); } 读写文 string s1 = @"d:\chen\a1.txt"; StreamReader sr = new StreamReader(s1,Encoding.GetEncoding("gb2312")); string rl; while((rl=sr.ReadLine())!=null) { MessageBox.Show(rl); } sr.Close(); 第 45 页 C#(WINFORM)学习 文件操作控件 打开文件 用 openFileDialog 控件。 private void button1_Click(object sender, System.EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory=Directory.GetCurrentDirectory(); openFileDialog1.Filter = "Cursor Files|*.cur"; openFileDialog1.Title = "Select a Cursor File"; if(openFileDialog1.ShowDialog() == DialogResult.OK) { System.IO.StreamReader sr = new System.IO.StreamReader(openFileDialog1.FileName); MessageBox.Show(sr.ReadToEnd()); sr.Close(); } } 选择文件夹 在工具箱上选择 folderBrowserDialog: folderBrowserDialog1.ShowDialog(); string s1 = folderBrowserDialog1.SelectedPath.ToString()+@"\mytest.txt"; StreamWriter wr = new StreamWriter(s1); 第 46 页 C#(WINFORM)学习 六、绘图 基本绘图 画直线 在 Button1 加入以下程序: System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.Red); System.Drawing.Graphics formGraphics = this.CreateGraphics(); formGraphics.DrawLine(myPen, 0, 0, 200, 200); myPen.Dispose(); formGraphics.Dispose(); 就可以在 Form1 上面画一条线了。用 myPen = new System.Drawing.Pen(System.Drawing.Color.Red,3); 可以改变线的宽度。 最简单的画线程序 Pen pen1=new Pen(Color.Green,2); Graphics g1=this.CreateGraphics(); PointF p1=new PointF(0,0); PointF p2=new PointF(100,100); g1.DrawLine(pen1,p1,p2); pen1.Dispose(); g1.Dispose(); 最后两句可以不写,程序关闭时自动完成。 画椭圆 System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.Red); System.Drawing.Graphics formGraphics = this.CreateGraphics(); formGraphics.DrawEllipse(myPen, new Rectangle(0,0,200,300)); myPen.Dispose(); formGraphics.Dispose(); 以下画一个椭圆并填充。 System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red); 第 47 页 C#(WINFORM)学习 System.Drawing.Graphics formGraphics = this.CreateGraphics(); formGraphics.FillEllipse(myBrush, new Rectangle(0,0,200,300)); myBrush.Dispose(); formGraphics.Dispose(); 以椭圆的心点画图 //以给定点找画椭圆的原始点,使得椭圆的心点是给定点 PointF EllipseCenter(int xs,int ys,PointF p0) { float ek=0.5f; PointF p1=new PointF(); p1.X=p0.X-xs*ek; p1.Y=p0.Y-ys*ek; return p1; } private void button1_Click(object sender, System.EventArgs e) { Graphics g= this.CreateGraphics(); Pen pen1=new Pen(Color.Yellow,1); Pen pen2=new Pen(Color.Red,1); PointF ps=new PointF(); int xs=1,ys=1; //半轴 PointF p0=new PointF(300,100); ps=EllipseCenter(xs,ys,p0); //以下画十字线表示心位置 PointF p1=new PointF(0,p0.Y); PointF p2=new PointF(600,p0.Y); PointF p3=new PointF(p0.X,0); PointF p4=new PointF(p0.X,300); g.DrawLine(pen1,p1,p2); g.DrawLine(pen1,p3,p4); g.DrawEllipse(pen2,ps.X,ps.Y,xs,ys); } 点弧线 System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.Red); System.Drawing.Graphics formGraphics = this.CreateGraphics(); 第 48 页 C#(WINFORM)学习 formGraphics.DrawArc(myPen, 100, 50, 140, 70, 30, 180); myPen.Dispose(); formGraphics.Dispose(); 多点直线 System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.Red); System.Drawing.Graphics formGraphics = this.CreateGraphics(); PointF point1 = new PointF( 50.0F, 50.0F); PointF point2 = new PointF(100.0F, 25.0F); PointF point3 = new PointF(200.0F, 5.0F); PointF point4 = new PointF(250.0F, 50.0F); PointF point5 = new PointF(300.0F, 100.0F); PointF point6 = new PointF(350.0F, 200.0F); PointF point7 = new PointF(250.0F, 250.0F); PointF[] curvePoints = { point1, point2, point3, point4, point5, point6, point7 }; formGraphics.DrawLines(myPen, curvePoints); myPen.Dispose(); formGraphics.Dispose(); 第 49 页 C#(WINFORM)学习 也可以用以下方式给数组赋值: PointF[] pt=new PointF[]{new PointF(2,2),new PointF(25,150),new PointF(100,100)}; 多点弧线 数据同上,修改如下: int offset = 1; //开始点(从0开始) int numSegments = 5; //包含后续点数 float tension = 1.0F; formGraphics.DrawCurve(myPen, curvePoints, offset, numSegments, tension); 第 50 页 C#(WINFORM)学习 以下程序可以画一个封闭曲线: private void button1_Click(object sender, System.EventArgs e) { System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.Red); System.Drawing.Graphics formGraphics = this.CreateGraphics(); PointF point1 = new PointF( 50.0F, 50.0F); PointF point2 = new PointF(100.0F, 25.0F); PointF point3 = new PointF(200.0F, 5.0F); PointF point4 = new PointF(250.0F, 50.0F); PointF point5 = new PointF(300.0F, 100.0F); PointF point6 = new PointF(350.0F, 200.0F); PointF point7 = new PointF(250.0F, 250.0F); PointF point8 = new PointF(40.0F, 150.0F); PointF[] curvePoints = { point1, point2, point3, point4, point5, point6, point7, point8, point1 }; int offset = 0; 第 51 页 C#(WINFORM)学习 int numSegments = 8; float tension = 1.0F; formGraphics.DrawCurve(myPen, curvePoints, offset, numSegments, tension); // formGraphics.DrawLines(myPen, curvePoints); myPen.Dispose(); formGraphics.Dispose(); } 如果是任意 3 点(或多点) ,在起始点不容易圆滑。可以用以下方法画封闭 曲线: PointF[] curvePoints = { point3, point1, point2, point3, point1 }; int offset = 1; int numSegments = 3; float tension = 0.5F; 第 52 页 C#(WINFORM)学习 这样可以保证第一个点处比较圆滑。 参数设置 设置线段宽度: myPen.Width =3; 在 pictrueBox 上面画线,修改 this: System.Drawing.Graphics formGraphics = picBox1.CreateGraphics(); 填充 System.Drawing.SolidBrush myBrush = new ystem.Drawing.SolidBrush(System.Drawing.Color.LightPink); System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.Red,1); System.Drawing.Graphics formGraphics = pictureBox1.CreateGraphics(); PointF point1 = new PointF( 50.0F, 50.0F); PointF point2 = new PointF(100.0F, 25.0F); PointF point3 = new PointF(200.0F, 5.0F); PointF point4 = new PointF(250.0F, 50.0F); PointF point5 = new PointF(300.0F, 100.0F); PointF point6 = new PointF(350.0F, 200.0F); PointF point7 = new PointF(250.0F, 250.0F); PointF[] curvePoints = { 第 53 页 C#(WINFORM)学习 point1, point2, point3, point4, point5, point6, point7 }; formGraphics.FillPolygon(myBrush,curvePoints); formGraphics.DrawPolygon(myPen,curvePoints); myPen.Dispose(); myBrush.Dispose(); formGraphics.Dispose(); 颜色 颜色基本设置 普通颜色设置可以直接选取系统定义的颜色。高级设置采用 RGB 颜色。 Color myColor; myColor = Color.FromArgb(23,56,78); 每个数字均必须是从 0 到 255 之间的一个整数,分别表示红、绿、蓝三种 原色。其 0 表示没有该颜色,而 255 则为所指定颜色的完整饱和度。如 Color.FromArgb(0,255,0) 表 示 绿 色 , Color.FromArgb(255,255,0) 表 示 黄 色 , Color.FromArgb(0,0,0) 呈现为黑色,而 Color.FromArgb(255,255,255) 呈现为白 色。 还可以设置透明度,如 Color myColor; myColor = Color.FromArgb(127, 23, 56, 78); 127 表示 50%的透明度,255 表示完全不透明。 选择颜色 以下程序可以从调色板选择一个颜色,在 textBox 上面显示。 private void button1_Click(object sender, System.EventArgs e) { ColorDialog MyDialog = new ColorDialog(); 第 54 页 C#(WINFORM)学习 MyDialog.AllowFullOpen = false ; MyDialog.ShowHelp = true ; MyDialog.Color = textBox1.ForeColor ; if (MyDialog.ShowDialog() == DialogResult.OK) textBox1.ForeColor = MyDialog.Color; } 产生窗体选择颜色 本程序在一个菜单里调用选择程序,产生一个动态的窗体,选择颜色后返回 主程序,刷新原来的页面。 由于没有掌握控制动态控件集合的方法,只好用枚举的方法定义动态控件, 对于多于 10 个的颜色序列,需要修改程序。 Form form1; bool Cform=true; //颜色设置 private void menuItem31_Click(object sender, System.EventArgs e) { while (Cform) { CreateColorForm(); Tuli1(); DisLine(); } Cform=true; } //产生颜色输入窗体 private void CreateColorForm() { int i; form1=new Form(); Button [] ColorButton Label [] ColorLabel = new Button [myPloys.marks1.Count]; = new Label [myPloys.marks1.Count]; Button button0 = new Button (); form1.Width=130; form1.Height=130+myPloys.marks1.Count*30; form1.Text = "等值线颜色输入"; form1.FormBorderStyle = FormBorderStyle.FixedDialog; 第 55 页 C#(WINFORM)学习 form1.ControlBox = false; form1.StartPosition = FormStartPosition.CenterScreen; button0.Text = "退出"; button0.Width=80; button0.Location=new Point(25,50+myPloys.marks1.Count*30); form1.Controls.Add(button0); form1.CancelButton = button0; for (i=0;iWINFORM)学习 case 7: ColorButton[i].Click += new System.EventHandler(ColorButton7_Click); break; case 8: ColorButton[i].Click += new System.EventHandler(ColorButton8_Click); break; case 9: ColorButton[i].Click += new System.EventHandler(ColorButton9_Click); break; case 10: ColorButton[i].Click += new System.EventHandler(ColorButton10_Click); break; default: break; } form1.Controls.Add(ColorButton[i]); } button0.Click += new System.EventHandler(button0_Click); form1.ShowDialog(); } private void ColorSelect(int si) { ColorDialog MyDialog = new ColorDialog(); MyDialog.AllowFullOpen = true ; MyDialog.ShowHelp = true ; MyDialog.Color = cColor[si] ; if (MyDialog.ShowDialog() == DialogResult.OK) cColor[si] = form1.Dispose(); } private void ColorButton0_Click(object sender, System.EventArgs e) { ColorSelect(0); { ColorSelect(1); { ColorSelect(2); { ColorSelect(3); { ColorSelect(4); { ColorSelect(5); } } } } } } private void ColorButton1_Click(object sender, System.EventArgs e) private void ColorButton2_Click(object sender, System.EventArgs e) private void ColorButton3_Click(object sender, System.EventArgs e) private void ColorButton4_Click(object sender, System.EventArgs e) private void ColorButton5_Click(object sender, System.EventArgs e) MyDialog.Color; 第 57 页 C#(WINFORM)学习 private void ColorButton6_Click(object sender, System.EventArgs e) { ColorSelect(6); { ColorSelect(7); { ColorSelect(8); { ColorSelect(9); { ColorSelect(10); } } } } } private void ColorButton7_Click(object sender, System.EventArgs e) private void ColorButton8_Click(object sender, System.EventArgs e) private void ColorButton9_Click(object sender, System.EventArgs e) private void ColorButton10_Click(object sender, System.EventArgs e) private void button0_Click(object sender, System.EventArgs e) { Cform=false; form1.Dispose(); } Font 和标注 基本操作 可以在指定的点开始写字,也可以在一个范围(如矩形)内写字。 Font myFont = new Font("Times New Roman", 14); Graphics g = this.CreateGraphics(); Pen myPen=new Pen(Color.Black); System.Drawing.SolidBrush myBrush=new SolidBrush(Color.Red); g.DrawRectangle(myPen,10, 10, 100, 200); g.DrawString("Look at this text!", myFont, myBrush, new RectangleF(10, 10, 100, 200)); g.DrawString("Look at this text!", myFont, myBrush, 10, 250); 画点和标注 System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.Red,1); System.Drawing.Graphics formGraphics = this.CreateGraphics(); System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black); System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 8); string drawString = "201"; float x = 100.0f; 第 58 页 C#(WINFORM)学习 float y = 100.0f; formGraphics.DrawEllipse(myPen, x, y,4,4); formGraphics.DrawString(drawString, drawFont, drawBrush, x, y); myPen.Dispose(); drawFont.Dispose(); drawBrush.Dispose(); formGraphics.Dispose(); Font 定义 public Font( FontFamily family, float emSize, FontStyle style, GraphicsUnit unit ); 以下的例子可以改变字体,大小、字形、单位,还可以设置成垂直。 // Font myFont = new Font("Times New Roman",14,FontStyle.Italic,GraphicsUnit.Millimeter); Font myFont = new Font("隶书",34,FontStyle.Underline,GraphicsUnit.Pixel); Graphics g = this.CreateGraphics(); System.Drawing.SolidBrush myBrush=new SolidBrush(Color.Red); System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat (StringFormatFlags.DirectionVertical); g.DrawString("Hello!你好", myFont, myBrush, 10, 10,drawFormat); 根据给定点找到实际标注点 //根据给定点,找到实际标注点,使得以画出的字符串以给定点为心 public PointF StringCenter(string s1,float sz,PointF p0) { PointF p1=new PointF(); float x0=0; float height=0; float width=StringWidth(s1,sz,ref x0,ref height); p1.X=p0.X-+x0-width/2; p1.Y=p0.Y-height/2; return p1; } 第 59 页 C#(WINFORM)学习 pictureBox 用 pictureBox 画图 和在 form 上面画图类似,只是把 this 替换成 pictureBox1: System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.Red,1); System.Drawing.Graphics formGraphics = pictureBox1.CreateGraphics(); System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black); System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 8); string drawString = "201"; float x = 200.0f; float y = 100.0f; formGraphics.DrawEllipse(myPen, x, y,4,4); formGraphics.DrawString(drawString, drawFont, drawBrush, x, y); myPen.Dispose(); drawFont.Dispose(); drawBrush.Dispose(); formGraphics.Dispose(); 用 pictureBox 显示一个图片 有固定模式和缩放模式,通过设置 SizeMode 实现。程序如下: pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; //按图像文件大小缩放 // pictureBox1.Image = Image.FromFile ("abc.bmp"); //文件放在\bin\Debug文件夹 //文件放在程序文件夹 pictureBox1.Image = Image.FromFile (@"..\..\abc1.bmp"); Region 基本概念 指示由矩形和由路径构成的图形形状的内部。 基本操作 基本操作 System.Drawing.Graphics e = this.CreateGraphics(); 第 60 页 C#(WINFORM)学习 Rectangle regionRect = new Rectangle(20, 20, 100, 100); e.DrawRectangle(Pens.Black, regionRect); RectangleF complementRect = new RectangleF(90, 30, 100, 100); e.DrawRectangle(Pens.Red,Rectangle.Round(complementRect)); Region myRegion = new Region(regionRect); myRegion.Intersect(complementRect); SolidBrush myBrush = new SolidBrush(Color.Blue); e.FillRegion(myBrush, myRegion); 主要图形操作还有 Union 和 Xor。 Polygon 的镂空 采用异或(Xor)运算。需要把 polygon 转换成路径: Graphics g1 = this.CreateGraphics(); GraphicsPath myPath1=new GraphicsPath(); PointF[] pts=new PointF[]{new PointF(2,20),new PointF(250,0) ,new PointF(100,100),new PointF(90,150),new PointF(10,70)}; myPath1.AddPolygon(pts); g1.DrawPath(Pens.Black,myPath1); GraphicsPath myPath2=new GraphicsPath(); PointF[] pts1=new PointF[]{new PointF(20,30),new PointF(50,70) ,new PointF(100,40)}; myPath2.AddPolygon(pts1); g1.DrawPath(Pens.Black,myPath2); Region myRegion = new Region(myPath1); myRegion.Xor(myPath2); SolidBrush myBrush = new SolidBrush(Color.Blue); g1.FillRegion(myBrush, myRegion); 如果要镂空多个空洞,需要把这些空洞都加入到 myPath2 ,其余操作同样。 采用交、并和异或运算,都可以把两个对象互换。 显示数据 public void DisplayRegionData(Graphics e, int len,RegionData dat) { int i; float x = 20, y = 140; 第 61 页 C#(WINFORM)学习 Font myFont = new Font("Arial", 8); SolidBrush myBrush = new SolidBrush(Color.Black); e.DrawString("myRegionData = ",myFont, y = 160; for(i = 0; i 300) { y += 20; x = 20; } e.DrawString(dat.Data[i].ToString(),myFont, x += 30; } } myBrush,new PointF(x, y)); myBrush,new PointF(x, y)); 路径 基本应用 用 GraphicsPath 可以把各种绘图元素 (包括文字) 包含进来, 最后用 DrawPath 画出来。 需要:using System.Drawing.Drawing2D; private void button2_Click(object sender, System.EventArgs e) { Pen myPen = new Pen(System.Drawing.Color.Red,1); System.Drawing.Graphics formGraphics = this.CreateGraphics(); GraphicsPath myPath1=new GraphicsPath(); myPath1.AddLine(0, 0, 10, 20); myPath1.AddEllipse(20,20,10,10); myPath1.AddLine(40, 40, 50, 120); formGraphics.DrawPath(myPen, myPath1); myPath1.Dispose(); myPen.Dispose(); formGraphics.Dispose(); } 第 62 页 C#(WINFORM)学习 不连续线条 用一个半径为 0 的点加在间: myPath1.AddLine(0, 0, 10, 20); myPath1.AddEllipse(20,20,0,0); myPath1.AddLine(40, 40, 50, 120); 也可以用两个 path,然后用 path2.AddPath(path3,false); 复杂应用 路径可以包含其它路径,汇总成一个大的图形。 还可以通过矩阵变换进行缩放、平移和旋转。 简单 Matrix 变换 可以通过矩阵变换进行图形的变换。 private void button1_Click(object sender, System.EventArgs e) { System.Drawing.Graphics e1 = this.CreateGraphics(); Pen myPen = new Pen(Color.Blue, 1); Pen myPen2 = new Pen(Color.Red, 1); Matrix myMatrix1 = new Matrix(1.0f, 0.0f, 0.0f, 3.0f, 0.0f, 0.0f); // x和y方向都 放大3倍 // Matrix myMatrix1 = new Matrix(1.0f, 0.0f, 0.0f, 1.0f, 50.0f, 50.0f); // 平移50,50 e1.DrawRectangle(myPen, 0, 0, 100, 100); e1.Transform = myMatrix1; e1.DrawRectangle(myPen2, 0, 0, 100, 100); myPen.Dispose(); e1.Dispose(); } 复杂 Matrix 变换 下例显示缩放、旋转和移动变换。 public void MultiplyExample(PaintEventArgs e) { Pen myPen = new Pen(Color.Blue, 2); Pen myPen2 = new Pen(Color.Red, 1); 第 63 页 C#(WINFORM)学习 Matrix myMatrix1 = new Matrix(3.0f, 0.0f, 0.0f, 3.0f, 0.0f, 0.0f); // Scale Matrix myMatrix2 = new Matrix(0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f); // Rotate 90, Matrix myMatrix3 = new Matrix(1.0f, 0.0f, 0.0f, 1.0f, 250.0f, 50.0f); // Translate ListMatrixElements(e, myMatrix1, "Beginning Matrix", 6, 40); myMatrix1.Multiply(myMatrix2, MatrixOrder.Append); ListMatrixElements(e,myMatrix1,"Matrix After 1st Multiplication",6,60); myMatrix1.Multiply(myMatrix3, MatrixOrder.Append); ListMatrixElements(e, myMatrix1,"Matrix After 2nd Multiplication",6,80); e.Graphics.DrawRectangle(myPen, 0, 0, 100, 100); e.Graphics.Transform = myMatrix1; e.Graphics.DrawRectangle(myPen2, 0, 0, 100, 100); } public void ListMatrixElements(PaintEventArgs e,Matrix matrix,string matrixName, int numElements,int y) { int i; float x = 20, X = 200; Font myFont = new Font("Arial", 8); SolidBrush myBrush = new SolidBrush(Color.Black); e.Graphics.DrawString(matrixName + ": for(i=0; i中自己加上缩放和平移计算) 来做。 从路径转换到点集用 PathPoints 属性。 第 64 页 C#(WINFORM)学习 Image 简单保存 可以利用 Image 类的 Save 方法保存目前显示的文件。需要在 Form 定义: public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.PictureBox pictureBox1; private Image cuurimage; 然后在程序引用即可。 cuurimage=pictureBox1.Image; cuurimage.Save("abc2.bmp"); 利用动态 Image 画图 PointF p1 = new PointF(); PointF p2 = new PointF(); Pen myPen = new Pen(Color.Black,1); cuurimage = Image.FromFile("abc.bmp"); Graphics g = Graphics.FromImage(cuurimage); Color [] c ={Color.LightPink,Color.Red,Color.Blue,Color.Brown,Color.Black}; for(k=0;k<=LineColor.Count-1;k++) { myPen.Color = c[(int)LineColor[k]]; p1=(PointF)LineArray[i]; i++; p2=(PointF)LineArray[i]; i++; g.DrawLine(myPen,p1,p2); } myPen.Dispose(); g = CreateGraphics(); g.DrawImage(cuurimage,50,0,cuurimage.Width+100,cuurimage.Height+10); g.Dispose(); 第 65 页 C#(WINFORM)学习 画图保存 在一个 pictureBox 上保存所画图像。 先需要加载一个背景图片,把图画在这个图片上,最后保存到另外一个文件 。 private Image cuurimage; private void button2_Click(object sender, System.EventArgs e) { Pen myPen = new Pen(System.Drawing.Color.Red,2); pictureBox1.Image = Image.FromFile (@"..\..\abc1.bmp"); cuurimage = pictureBox1.Image; Graphics g = Graphics.FromImage(cuurimage); g.DrawEllipse(myPen, 100, 100,44,14); cuurimage.Save("abc0.bmp"); myPen.Dispose(); g.Dispose(); } 利用 Image 擦除图像 下例要先准备一个 jpg 图像文件。 先
第一部分 Visual C++ 2010开发与新特性 第1章 Visual C++ 2010开发环境简介 1 1.1 Visual C++ 2010简介 1 1.2 Visual C++ 2010下载安装指南 1 1.3 Visual C++ 2010主要特点与新特性 3 1.4 Visual C++ 2010开发环境操作指南 6 1.4.1 创建Visual C++应用程序 6 1.4.2 Visual C++ 2010菜单介绍 9 1.5 Visual C++ 2010 MFC简介 12 1.6 Visual C++ 2010 clr简介 13 1.7 Visual C++ 2010 64位编程 14 1.8 支持新的C++语言标准 14 1.8.1 支持新的C++语言标准(C++ 0x) 14 1.8.2 Lambda表达式 15 1.8.3 静态断言static_assert 17 1.8.4 auto关键字 18 1.8.5 右值引用 19 1.8.6 安全数据类型 22 1.8.7 移动构造 23 1.9 支持开发并行程序 26 1.9.1 运行库支持native代码 26 1.9.2 调试和分析工具 29 1.10 对MFC的增强 31 1.10.1 任务对话框CTaskDialog 31 1.10.2 重启管理器(Restart Manager)支持 33 1.10.3 支持新的用户界面风格 34 第2章 MFC应用程序框架 37 2.1 用MFC向导生成应用程序 37 2.1.1 生成的程序框架 37 2.1.2 生成的应用程序类型 43 2.1.3 向导生成工程文件简介 46 2.2 应用程序框架分析 46 2.2.1 框架简介 47 2.2.2 MFC应用程序运行流程 50 2.2.3 应用程序窗口 51 2.2.4 消息机制与消息循环 59 2.3 文档与视图 65 2.3.1 文档类结构分析 65 2.3.2 视图类结构分析 65 2.3.3 文档与视图的关系 67 2.3.4 sdi应用程序分析 69 2.3.5 mdi应用程序分析 69 2.4 工具栏与状态栏 69 2.4.1 工具栏 70 2.4.2 状态栏 78 2.5 MFC新功能——Office 2007风格程序分析 85 2.6 MFC新功能——Visual Studio风格程序分析 90 2.7 MFC新功能——Windows资源管理器风格程序分析 98 2.8 应用程序框架类对象分析 100 2.8.1 视图类对文档类的调用 100 2.8.2 在框架类获得当前的文档类和视图类对象指针 101 2.8.3 获得应用程序类对象的指针 101 2.8.4 从应用类对象获得主框架类对象的指针 101 2.9 小结 102 第3章 Visual C++ 2010 MFC菜单编程 103 3.1 菜单编程 103 3.1.1 创建菜单 104 3.1.2 创建菜单热键 115 3.1.3 标记菜单 115 3.1.4 给菜单加入图标 119 3.1.5 禁用菜单 121 3.1.6 移除与加载菜单 127 3.2 菜单消息的传输机制 129 3.2.1 菜单消息的分类 129 3.2.2 菜单消息的传输路由 129 3.3 动态菜单操作 131 3.3.1 添加菜单 131 3.3.2 插入菜单 133 3.3.3 删除菜单 135 3.3.4 插入菜单的命令响应 136 3.3.5 修改菜单 137 3.4 小结 140 第4章 Visual C++ 2010 MFC对话框编程 141 4.1 对话框简介 141 4.1.1 对话框的控件简介 141 4.1.2 对话框的种类简介 149 4.1.3 设计对话框 150 4.2 创建与销毁对话框 153 4.2.1 模态对话框 153 4.2.2 非模式对话框 159 4.2.3 属性页对话框 163 4.3 消息对话框 173 4.4 通用对话框 175 4.4.1 文件打开对话框 176 4.4.2 文件保存对话框 178 4.4.3 颜色对话框 179 4.4.4 字体对话框 181 4.4.5 查找对话框 183 4.4.6 页面设置对话框 185 4.4.7 打印对话框 186 4.5 小结 187 第5章 Visual C++ 2010 MFC对话框控件 188 5.1 Visual C++ 2010 Button控件简介与开发 191 5.2 Visual C++ 2010 List Box控件简介与开发 193 5.3 Visual C++ 2010 Com boBox控件简介与开发 195 5.3.1 创建扩展组合框控件 196 5.3.2 在扩展组合框控件使用 5.3.2 图像列表 197 5.3.3 设置各项的图像 197 5.3.4 处理扩展组合框控件的通知消息 198 5.4 Visual C++ 2010 List控件简介与开发 198 5.4.1 列表控件和列表视图 199 5.4.2 列表项和图像列表 199 5.4.3 回调项和回调屏蔽 200 5.4.4 创建列表控件 200 5.4.5 创建图像列表 201 5.4.6 向控件添加列(报表视图) 204 5.4.7 向控件添加项 205 5.4.8 在列表控件滚动、排列、排序和查找 205 5.4.9 在列表控件实现工作区 205 5.4.10 处理列表控件的通知消息 206 5.4.11 更改列表控件样式 206 5.4.12 虚拟列表控件 207 5.4.13 列表控件的消息映射 209 5.4.14 列表控件的风格选项及表头设置 210 5.4.15 销毁列表控件 210 5.5 Visual C++ 2010 Edit控件简介与开发 211 5.6 Visual C++ 2010 Rich Edit控件简介与开发 213 5.6.1 Rich Edit控件的字符格式 215 5.6.2 Rich Edit控件的段落格式 215 5.6.3 Rich Edit控件的当前选定内容 215 5.6.4 Rich Edit控件的分词 216 5.6.5 Rich Edit控件的剪贴板操作 216 5.6.6 Rich Edit控件的流操作 216 5.6.7 Rich Edit控件的打印操作 216 5.6.8 无底的Rich Edit控件 217 5.6.9 来自Rich Edit控件的通知 217 5.7 Visual C++ 2010 Progress控件简介与开发 219 5.7.1 进度控件的样式 219 5.7.2 进度控件的设置 219 5.7.3 操作进度控件 220 5.8 Visual C++ 2010 Tree控件简介与开发 220 5.8.1 树控件样式 221 5.8.2 树控件父项和子项 221 5.8.3 树控件项位置 222 5.8.4 树控件项标签 222 5.8.5 树控件标签编辑 223 5.8.6 树控件项的状态 223 5.8.7 树控件图像列表 224 5.8.8 树控件项选择 224 5.8.9 树控件拖放操作 224 5.8.10 树控件项信息 225 5.8.11 树控件通知消息 225 5.9 Visual C++ 2010 DBgrid控件简介与开发 226 5.9.1 示例程序1 226 5.9.2 示例程序2 227 5.10 Visual C++ 2010 Rebar控件简介与开发 228 5.10.1 在Rebar控件使用图像列表 230 5.10.2 在Rebar控件使用对话栏 231 5.10.3 处理Rebar控件的通知消息 231 5.11 Visual C++ 2010 Timer控件简介与开发 232 5.12 Visual C++ 2010 Tab控件简介与开发 234 5.12.1 选项卡和选项卡控件属性 235 5.12.2 选项卡控件的使用方法 235 5.12.3 创建选项卡控件的方法 235 5.12.4 处理选项卡控件通知消息 236 5.12.5 ctabctrl类简介 236 5.13 Visual C++ 2010 IP控件简介与开发 241 5.14 Visual C++ 2010 Picture控件简介与开发 241 5.15 Visual C++ 2010 Slider控件简介与开发 244 5.15.1 滑块控件样式 244 5.15.2 滑块控件成员函数 245 5.15.3 滑块控件通知消息 246 5.16 Visual C++ 2010 Scroll Bar控件简介与开发 246 5.17 Visual C++ 2010 Hot Key控件简介与开发 248 5.17.1 使用热键控件 248 5.17.2 设置热键 249 5.18 Visual C++ 2010 Animation控件简介与开发 249 5.18.1 使用动画控件 249 5.18.2 动画控件发送的通知 250 5.19 Visual C++ 2010 Spin控件简介与开发 250 5.19.1 数值调节钮的样式 250 5.19.2 数值调节钮成员函数 251 5.20 Visual C++ 2010 GroupBox控件简介与开发 251 5.21 Visual C++ 2010 Data Time Picker控件简介与开发 252 5.21.1 创建日期和时间选择器控件 253 5.21.2 访问嵌入的月历控件 253 5.21.3 在日期和时间选择器控件使用自定义格式字符串 254 5.21.4 在日期和时间选择器控件使用回调字段 254 5.21.5 处理日期和时间选择器控件的通知消息 256 5.22 Visual C++ 2010 Month Canlendar控件简介与开发 256 5.22.1 创建月历控件 257 5.22.2 处理月历控件的通知消息 257 5.22.3 设置月历控件的日状态 257 5.23 Visual C++ 2010 Custom控件简介与开发 258 5.23.1 使用MFC方法定制控件必备的几个基本概念 259 5.23.2 定制自定义控件的3种常见方法 260 5.24 Visual C++ 2010 SysLink控件简介与开发 260 5.25 Visual C++ 2010 Split Button控件简介与开发 261 5.26 Visual C++ 2010 Network Address控件简介与开发 262 5.27 Visual C++ 2010 Check Box控件简介与开发 262 5.28 Visual C++ 2010 Radio Button控件简介与开发 264 5.28.1 为单选按钮控件分组 264 5.28.2 获得被选的单选按钮的文本 264 5.29 Visual C++ 2010 Mediaplayer控件简介与开发 265 5.30 小结 266 第二部分 Visual C++ 2010下MFC开发 第6章 计算机测控系统概述 267 6.1 Visual C++ 2010 SDI开发简介 267 6.1.1 建立应用程序基本框架 267 6.1.2 处理视图 267 6.1.3 处理文档 271 6.1.4 串行化处理 274 6.1.5 sdi应用程序编程思路 275 6.2 Visual C++ 2010 MDI开发简介 277 6.2.1 多文档接口 277 6.2.2 生成程序 278 6.2.3 程序类、文件和代码 279 6.2.4 自定义资源 281 6.3 Visual C++ 2010 View开发 282 6.3.1 生成源文件 283 6.3.2 初始化视图类数据成员 283 6.3.3 加入消息处理功能 285 6.3.4 设计程序资源 290 6.3.5 定制miniDraw窗口 292 6.3.6 程序清单 293 6.4 Visual C++ 2010 EditView开发 298 6.4.1 生成MiniEdit程序 299 6.4.2 修改程序菜单 300 6.4.3 编辑加速键 301 6.4.4 程序清单 303 6.5 Visual C++ 2010 FormView开发 306 6.5.1 自定义FormDemo程序 307 6.5.2 程序清单 314 6.6 Visual C++ 2010 ScrollView开发 319 6.6.1 加入滚动功能 319 6.6.2 坐标换算 319 6.6.3 限制图形大小 322 6.6.4 改变鼠标光标 325 6.7 Visual C++ 2010 HtmlEditView开发 328 6.8 Visual C++ 2010 HtmlView开发 331 6.9 Visual C++ 2010 ListView开发 335 6.10 Visual C++ 2010 RichEditView开发 335 6.11 VisualC++ 2010 TreeView开发 336 6.12 Visual C++ 2010 Office 2007风格文档视图开发框架 337 6.13 Visual C++ 2010 Visual Studio 2008风格文档视图开发框架 342 6.14 Visual C++ 2010 Windows资源管理器风格文档视图开发框架 346 6.15 小结 350 第7章 Visual C++ 2010 MFC应用程序界面与美化 351 7.1 应用程序窗口风格美化 351 7.1.1 借助ActiveSkin美化窗口 351 7.1.2 修改窗口外观 352 7.2 应用程序窗口图标与背景修改 359 7.2.1 修改窗口图标 359 7.2.2 修改背景 360 7.3 工具栏编程与美化 365 7.3.1 创建工具栏 365 7.3.2 在工具栏添加、删除按钮 366 7.3.3 从对话框创建工具栏 368 7.4 状态栏编程与美化 370 7.4.1 创建状态栏 370 7.4.2 在状态栏插入进度条 370 7.5 鼠标光标编程 371 7.5.1 鼠标光标编程步骤 371 7.5.2 鼠标的消息处理机制 373 7.5.3 示例 374 7.6 创建启动界面 376 7.7 创建特效窗口启动应用程序 378 7.8 创建特效窗口关闭应用程序 378 7.9 小结 383 第8章 Visual C++ 2010 MFC文本与字体 384 8.1 CFont字体类简介 384 8.1.1 CFont字体类成员介绍 384 8.1.2 CFont字体类初始化函数 385 8.1.3 其他成员介绍 390 8.2 创建文本插入符与图片插入符 391 8.2.1 创建文本插入符 391 8.2.2 创建图片插入符 394 8.2.3 创建随鼠标移动的插入符 396 8.3 输出文字与字体格式 397 8.3.1 输出固定文字 397 8.3.2 设定输出字体的格式 398 8.3.3 字符输入 399 8.4 输出彩色文字与变色文字 404 8.4.1 DrawText()函数和字符串资源 404 8.4.2 定时器和变色文字 408 8.5 小结 410 第9章 Visual C++ 2010 MFC图形图像编程 411 9.1 Windows绘图简介 411 9.1.1 设备描述表 411 9.1.2 绘图属性 412 9.1.3 元文件和路径 412 9.1.4 颜色和调色板 412 9.1.5 图形设备接口函数 413 9.2 Windows屏幕绘图简介 414 9.2.1 窗口客户区 414 9.2.2 映射模式 414 9.2.3 图形刷新 416 9.3 微软GDI绘图简介 416 9.3.1 GDI基础 416 9.3.2 GDI结构 417 9.3.3 GDI函数调用 417 9.3.4 GDI基本图形 418 9.4 GDI笔绘图 419 9.4.1 CPen类简介 419 9.4.2 使用GDI绘制线条 419 9.4.3 使用CPen类绘制指定的线条 422 9.4.4 绘制连续的线条 424 9.5 GDI画刷绘图 425 9.5.1 CBrush类介绍 426 9.5.2 CBrush类简单画刷的实现 429 9.5.3 CBrush类位图画刷的实现 430 9.5.4 透明画刷的实现 431 9.6 小结 433 第10章 Visual C++ 2010 MFC动态函数链接库 434 10.1 动态函数链接库简介 434 10.1.1 什么是动态函数链接库 434 10.1.2 动态函数链接库的优点 435 10.1.3 动态函数链接库的起源 436 10.1.4 动态函数链接库的原理 436 10.2 调用动态函数链接库 436 10.2.1 静态链接 436 10.2.2 动态链接 438 10.3 Dll的框架简介 439 10.3.1 DllMain()函数简介 439 10.3.2 Dll的导出函数 439 10.4 创建MFC Dll范例 440 10.4.1 建立MFC Dll工程 440 10.4.2 添加实现代码 442 10.4.3 编译并调用 443 10.5 创建Win32 Dll范例 443 10.5.1 建立Win32工程 443 10.5.2 添加动态链接库代码 444 10.5.3 编译工程 444 10.6 创建资源Dll范例 445 10.6.1 建立MFC Application工程 445 10.6.2 建立文资源Dll 445 10.6.3 加载资源Dll 446 10.7 hook技术 446 10.7.1 hook函数类型 446 10.7.2 使用hook函数 449 10.7.3 hook鼠标 449 10.7.4 hook键盘 450 10.8 小结 452 第11章 Visual C++ 2010 MFC Activex控件 454 11.1 Activex控件简介 454 11.2 Activex控件测试与注册 455 11.2.1 Activex控件的测试 455 11.2.2 Activex控件的注册 457 11.3 MFC Activex控件向导 458 11.4 Activex控件属性开发 458 11.4.1 添加常用属性 459 11.4.2 添加自定义属性 460 11.4.3 高级属性实现 460 11.4.4 访问环境属性 461 11.5 Activex控件事件开发 461 11.5.1 添加常用事件 462 11.5.2 添加自定义事件 463 11.6 Activex控件方法开发 464 11.6.1 添加常用方法 465 11.6.2 添加自定义方法 465 11.6.3 从方法返回错误代码 466 11.7 完整Activex控件范例 467 11.7.1 创建工程 467 11.7.2 clock控件的实现 469 11.7.3 添加常用属性 470 11.7.4 添加自定义属性 473 11.7.5 添加方法 474 11.7.6 添加常用事件 475 11.7.7 添加自定义事件 476 11.8 调用Activex控件 477 11.9 小结 478 第12章 Visual C++ 2010 MFC文件与注册表操作 479 12.1 文本操作串行化 479 12.1.1 文档类serialize()函数 479 12.1.2 CArchive对文件进行读写 482 12.1.3 文档操作串行化代码分析 485 12.2 CFile类 492 12.2.1 打开文件操作 493 12.2.2 读写文件操作 494 12.2.3 定位文件操作 496 12.2.4 关闭文件操作 497 12.2.5 异常操作 497 12.2.6 文件管理操作 498 12.3 .ini文件读写操作 500 12.4 注册表读写操作 502 12.4.1 注册表简介 502 12.4.2 注册表API 504 12.4.3 访问并修改注册表 507 12.5 小结 509 第13章 Visual C++ 2010 MFC数据库开发 510 13.1 数据库基本知识 510 13.2 SQL语言的基础知识 511 13.3 ODBC访问数据库 512 13.3.1 注册ODBC数据库 512 13.3.2 创建一个MFC的ODBC程序 514 13.3.3 程序结构分析 515 13.3.4 在视图上显示数据库查询结果 520 13.3.5 对查询结果排序及设置查询条件 524 13.3.6 动态设置查询条件并更新查询结果 527 13.4 ODBC更新数据库 532 13.5 ODBC访问SQL server 540 13.6 ado数据库访问 543 13.6.1 ado数据库访问概述 543 13.6.2 在Visual C++使用ado编程 546 13.7 ado访问SQL server数据库 556 13.8 小结 556 第14章 Visual C++2010 MFC多线程程序设计 557 14.1 进程和多线程的概念 557 14.2 线程的创建 558 14.2.1 创建工作者线程 558 14.2.2 创建用户界面线程 559 14.3 线程的终止 560 14.4 设置线程的优先级 562 14.5 暂停及重新启动线程 563 14.6 线程间的通信 571 14.7 线程的同步 572 14.7.1 临界区 572 14.7.2 互斥量 573 14.7.3 事件 573 14.7.4 信号量 574 14.8 小结 579 第15章 Visual C++ 2010 MFC网络程序设计 580 15.1 计算机网络的基础知识 580 15.1.1 TCP/IP协议模型 580 15.1.2 ip地址 582 15.1.3 端口 582 15.1.4 数据封装 582 15.2 Winsock简介 583 15.3 MFC对Windows Sockets的支持 583 15.3.1 Socket的定义 584 15.3.2 casyncSocket类介绍 584 15.3.3 cSocket类介绍 592 15.4 一个基于udp的聊天室示例 593 15.4.1 MFC对Windows Sockets的初始化 593 15.4.2 服务器端的实现 595 15.4.3 客户端的实现 599 15.5 一个基于TCP的聊天室示例 605 15.5.1 服务器端的实现 606 15.5.2 客户端的实现 609 15.6 小结 612 第16章 Visual C++ 2010 MFC进程通信 613 16.1 剪贴板通信 613 16.1.1 Openclipboard()函数 613 16.1.2 Closeclipboard()函数 614 16.1.3 emptyclipboard()函数 614 16.1.4 Setclipboarddata()函数 614 16.1.5 globalalloc()函数 615 16.1.6 globallock()函数 616 16.1.7 globalunlock()函数 616 16.1.8 Getclipboarddata()函数 616 16.1.9 一个利用剪贴板在不同进程之间交换数据的示例 616 16.2 邮槽通信 619 16.2.1 Createmailslot()函数 619 16.2.2 Getmailslotinfo()函数 620 16.2.3 Setmailslotinfo()函数 621 16.2.4 ReadFile()函数 621 16.2.5 GetFiletime()函数和SetFiletime()函数 621 16.2.6 CreateFile()函数 622 16.2.7 WriteFile()函数 623 16.2.8 Closehandle()函数 623 16.2.9 一个利用邮槽在不同进程间通信的示例 623 16.3 匿名管道通信 628 16.3.1 Createpipe()函数 628 16.3.2 Createprocess()函数 629 16.3.3 Getstdhandle()函数 631 16.3.4 ReadFile()和WriteFile()函数 632 16.3.5 一个利用匿名管道在父子进程间通信的示例 632 16.4 命名管道通信 637 16.4.1 Createnamedpipe()函数 637 16.4.2 connectnamedpipe()函数 639 16.4.3 disconnectnamedpipe()函数 640 16.4.4 waitnamedpipe()函数 640 16.4.5 利用命名管道通信的基本流程 640 16.4.6 一个利用命名管道在不同进程间通信的示例 641 16.5 共享内存通信 647 16.5.1 CreateFilemApping()函数 647 16.5.2 mapViewofFile()函数 648 16.5.3 unmapViewofFile()函数 649 16.5.4 OpenFilemApping()函数 649 16.5.5 利用共享内存通信的基本流程 649 16.5.6 一个利用共享内存在不同进程间通信的示例 650 16.6 小结 655 第三部分 Visual C++ 2010下MFC与clr进行开发 第17章 Visual C++ 2010 clr开发基础 656 17.1 什么是.net 656 17.2 .net框架 656 17.3 公共语言运行时(clr) 657 17.3.1 托管代码 659 17.3.2 代码验证 659 17.3.3 代码访问验证 659 17.3.4 垃圾回收 659 17.3.5 语言的互操作性 660 17.3.6 实时编译(jit) 660 17.4 通用类型系统(cts) 661 17.5 通用语言规范(cls) 663 17.6 程序集 664 17.6.1 元数据 664 17.6.2 程序集版本管理 665 17.6.3 微软间语言(msil) 665 17.6.4 资源 666 17.7 .net开发应用程序的范畴 666 17.8 .net框架类库 667 17.9 C++/clr开发语法简介 668 17.10 小结 670 第18章 Visual C++ 2010 clr Windows窗口编程 671 18.1 创建Windows应用程序 671 18.2 类层次结构 677 18.3 control类 677 18.3.1 大小与位置 678 18.3.2 外观 679 18.3.3 用户交互操作 679 18.3.4 Windows功能 680 18.4 标准Windows控件使用指南 681 18.4.1 Button控件 681 18.4.2 checkBox控件 681 18.4.3 radioButton控件 682 18.4.4 comboBox控件、ListBox控件和checkedListBox控件 682 18.4.5 datetimepicker控件 684 18.4.6 errorprovider组件 685 18.4.7 helpprovider组件 686 18.4.8 imageList组件 686 18.4.9 label控件 686 18.4.10 ListView控件 687 18.4.11 pictureBox控件 688 18.4.12 progressbar控件 689 18.4.13 TextBox控件、RichTextBox控件与maskedTextBox 18.4.13 控件 689 18.4.14 panel控件 690 18.4.15 flowlayoutpanel控件和tablelayoutpanel控件 690 18.4.16 splitcontainer控件 691 18.4.17 tabcontrol控件和tabpage控件 691 18.4.18 toolstrip控件 692 18.4.19 menustrip控件 694 18.4.20 conTextmenustrip控件 694 18.4.21 toolstripmenuitem控件 694 18.4.22 toolstripmanager类 695 18.4.23 toolstripcontainer控件 695 18.5 窗体Winform 695 18.5.1 form类 695 18.5.2 多文档界面 700 18.5.3 定制控件 700 18.6 小结 707 第19章 Visual C++ 2010 MFC与.net交互编程 708 19.1 编写托管扩展应用程序 708 19.2 编写访问.net的MFC程序 709 19.3 混合模式编程问题 711 19.4 运用.net类型 713 19.4.1 定义和使用托管类型 713 19.4.2 将非托管对象作为托管类的成员 715 19.4.3 装箱和拆箱 716 19.4.4 指针 717 19.4.5 在非托管代码使用托管数组 719 19.5 小结 720 第四部分 发布Visual C++ 2010程序 第20章 Visual C++ 2010应用程序部署 721 20.1 Windows installer介绍 722 20.2 一个简单的Windows应用程序 723 20.3 使用安装向导快速创建安装包 726 20.4 手动创建安装程序 728 20.5 使用安装编辑器 731 20.5.1 File system(文件系统编辑器) 731 20.5.2 registry编辑器 732 20.5.3 File types编辑器 733 20.5.4 user interface编辑器 734 20.5.5 custom actions编辑器 735 20.5.6 launch conditions编辑器 736 20.6 小结 736 第五部分 基于Windows 7平台用Visual C++ 2010开发 第21章 Visual C++ 2010基于Windows 7新特性开发 737 21.1 实现C++兼容开发 737 21.1.1 实现uac数据重定向 737 21.1.2 实现高dpi 741 21.1.3 实现安装程序检测 742 21.1.4 会话0隔离 743 21.1.5 用户界面特权隔离(uipi) 746 21.1.6 版本检查 748 21.2 Windows 7系统专题 750 21.2.1 实现超级任务栏 750 21.2.2 实现shell库 759 21.2.3 实现后台服务 762 21.2.4 开发基于Windows 7的 21.1.5 设备与性能应用 763 21.3 开发基于Windows 7的新特性 769 21.3.1 实现多点触摸 769 21.3.2 实现获取传感器与位置 771 21.3.3 实现Windows 7 ribbon界面开发 774 21.3.4 基于Visual C++ 2010开发基于Windows 7的语音识别与语音合成 776 21.3.5 基于Visual C++ 2010与Windows sdk for Windows 7开发Windows 7平台的tablet pc应用 787 21.3.6 开发Windows 7的安全体验cryptoAPI加密 804

111,088

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧