我本人不是专业的控件开发人员,只是在平常的工作中,需要自己开发一些控件。在自己开发
WinForm
控件的时候,没有太多可以借鉴的资料,只能盯着
MSDN
使劲看,还好总算有些收获。现在我会把这些经验陆陆续续的总结出来,写成一系列方章,希望对看到的朋友有所帮助。今天我来开个头。
其实开发 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 。
本系列文章主要介绍自定义控件的开发方法。
控件(可视化的)的基本特征:
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;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
namespace
CustomControlSample
{
public class FirstControl : Control
{
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
public FirstControl()
{
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
// 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;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
[
Category("Alignment"),
Description("Specifies the alignment of text.")
]
public ContentAlignment TextAlignment
{
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
get
{
return alignmentValue;
}
set
{
alignmentValue = value;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
// The Invalidate method invokes the OnPaint method described
// in step 3.
Invalidate();
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
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;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
// 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);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
}
}
}
其实开发 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 。
本系列文章主要介绍自定义控件的开发方法。
控件(可视化的)的基本特征:
1. 可视化。
2. 可以与用户进行交互,比如通过键盘和鼠标。
3. 暴露出一组属性和方法供开发人员使用。
4. 暴露出一组事件供开发人员使用。
5. 控件属性的可持久化。
6. 可发布和可重用。
这些特征是我自己总结出来,不一定准确,或者还有遗漏,但是基本上概括了控件的主要方面。
接下来我们做一个简单的控件来增强一下感性认识。首先启动VS2005创建一个ClassLibrary工程,命名为CustomControlSample,VS会自动为我们创建一个solution与这个工程同名,然后删掉自动生成的Class1.cs文件,最后在Solution explorer里右键点击CustomControlSample工程选择Add->Classes…添加一个新类,将文件的名称命名为FirstControl。下边是代码:
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
晚了,今天写到这里,下一篇文章介绍怎样使用我们写好的控件。
本文转自纶巾客博客园博客,原文链接:http://www.cnblogs.com/guanjinke/archive/2006/12/04/582084.html,如需转载请自行联系原作者