龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C#开发 >

浅析C# WinForm控件开发前期准备

时间:2011-04-12 23:18来源:未知 作者:admin 点击:
分享到:
对于C# WinForm控件开发,MSDN上提供了很多使用的方法,那么这里就向你介绍对于C# WinForm控件开发的基本了解内容,C#开发WinForm控件的类型以及各自的特点。 C# WinForm控件开发的伊始: 其

对于C# WinForm控件开发,MSDN上提供了很多使用的方法,那么这里就向你介绍对于C# WinForm控件开发的基本了解内容,C#开发WinForm控件的类型以及各自的特点。

C# WinForm控件开发的伊始:

其实C#开发WinForm控件并不是很复杂,.NET为我们提供了丰富的底层支持。如果你有MFC或者API图形界面的开发经验,那么学会WinForm控件可能只需要很短的时间就够了。

自己开发的WinForm控件通常有三种类型:复合控件(Composite Controls),扩展控件(Extended Controls),自定义控件(Custom Controls)。

◆复合控件:将现有的各种控件组合起来,形成一个新的控件,将集中控件的功能集中起来。

◆扩展控件:在现有控件的控件的基础上派生出一个新的控件,为原有控件增加新的功能或者修改原有控件的控能。

◆自定义控件:直接从System.Windows.Forms.Control类派生出来。Control类提供控件所需要的所有基本功能,包括键盘和鼠标的事件处理。自定义控件是最灵活最强大的方法,但是对开发者的要求也比较高,你必须为Control类的OnPaint事件写代码,你也可以重写Control类的WndProc方法,处理更底层的Windows消息,所以你应该了解GDI+和Windows API。

C# WinForm控件开发之控件(可视化的)的基本特征:

1. 可视化。

2. 可以与用户进行交互,比如通过键盘和鼠标。

3. 暴露出一组属性和方法供开发人员使用。

4. 暴露出一组事件供开发人员使用。

5. 控件属性的可持久化。

6. 可发布和可重用。

这些特征是我自己总结出来,不一定准确,或者还有遗漏,但是基本上概括了控件的主要方面。

接下来我们做一个简单的控件来增强一下感性认识。首先启动VS2005创建一个ClassLibrary工程,命名为CustomControlSample,VS会自动为我们创建一个solution与这个工程同名,然后删掉自动生成的Class1.cs文件,最后在Solution explorer里右键点击CustomControlSample工程选择Add->Classes…添加一个新类,将文件的名称命名为FirstControl。下边是代码:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Windows.Forms;  
  5. using System.ComponentModel;  
  6. using System.Drawing;  
  7.  
  8. namespace CustomControlSample  
  9. {  
  10. public class FirstControl : Control  
  11. {  
  12.  
  13. public FirstControl()  
  14. {  
  15.  
  16. }  
  17.  
  18. // ContentAlignment is an enumeration defined in the System.Drawing  
  19. // namespace that specifies the alignment of content on a drawing   
  20. // surface.  
  21. private ContentAlignment alignmentValue = ContentAlignment.MiddleLeft;  
  22.  
  23. [  
  24. Category("Alignment"),  
  25. Description("Specifies the alignment of text.")  
  26. ]  
  27. public ContentAlignment TextAlignment  
  28. {  
  29.  
  30. get 
  31. {  
  32. return alignmentValue;  
  33. }  
  34. set 
  35. {  
  36. alignmentValue = value;  
  37.  
  38. // The Invalidate method invokes the OnPaint method described   
  39. // in step 3.  
  40. Invalidate();  
  41. }  
  42. }  
  43.  
  44.  
  45. protected override void OnPaint(PaintEventArgs e)  
  46. {  
  47. base.OnPaint(e);  
  48. StringFormat style = new StringFormat();  
  49. style.Alignment = StringAlignment.Near;  
  50. switch (alignmentValue)  
  51. {  
  52. case ContentAlignment.MiddleLeft:  
  53. style.Alignment = StringAlignment.Near;  
  54. break;  
  55. case ContentAlignment.MiddleRight:  
  56. style.Alignment = StringAlignment.Far;  
  57. break;  
  58. case ContentAlignment.MiddleCenter:  
  59. style.Alignment = StringAlignment.Center;  
  60. break;  
  61. }  
  62.  
  63. // Call the DrawString method of the System.Drawing class to write   
  64. // text. Text and ClientRectangle are properties inherited from  
  65. // Control.  
  66. e.Graphics.DrawString(  
  67. Text,  
  68. Font,  
  69. new SolidBrush(ForeColor),  
  70. ClientRectangle, style);  
  71.  
  72. }  
  73. }  
  74. }  

C# WinForm控件开发的基本前期需要了解的内容就向你介绍到这里,希望对你进行C# WinForm控件开发有所帮助。


精彩图集

赞助商链接