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

开发 后端
C# WinForm控件开发是我们在C#使用过程中的一个阶段,那么掌握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。下边是代码:

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

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

【编辑推荐】

  1. C# XML编程删除XML文件内容操作详解
  2. 浅谈C# 加密中MD5和SHA1加密实现
  3. 简析散列算法在C# 加密中的应用
  4. RSA实现C# 加密详解
  5. 详解TripleDES实现C# 加密操作
责任编辑:仲衡 来源: 百度空间
相关推荐

2009-09-03 17:49:59

C#浏览器开发

2009-09-11 11:33:58

C# WinForm控Attribute

2009-08-11 14:45:41

C# DataGrid

2009-08-28 15:05:35

C#编写Calenda

2009-08-12 10:35:50

C#调用ActiveX

2009-08-06 16:58:40

C#编写ActiveX

2009-09-04 17:58:38

C# Web Brow

2009-08-20 09:30:03

C#开发WinForm

2009-08-20 10:24:52

C#开发WinForm

2009-08-17 15:48:47

C# WinForm进

2009-09-11 12:31:15

C# WinForm控设置默认值

2009-09-11 12:07:12

C# WinForm控

2009-09-01 10:59:22

C#项目

2009-09-09 10:47:29

C# CheckBox

2009-08-26 13:36:33

C#打印控件

2009-08-24 09:55:24

C#集成开发环境

2009-08-28 16:31:21

C# treeview

2009-10-10 14:54:44

treeView1控件

2009-09-01 10:35:59

C# WinForm控

2009-09-08 14:54:40

C# listBox控
点赞
收藏

51CTO技术栈公众号