Friday, April 24, 2009

.Net 下自定义控件创建(1):User Control 与 Custom Control 的关系、区别和使用

简而言之,两者是一个继承关系。UserControl继承Control   
  System.Object  
        System.MarshalByRefObject  
              System.ComponentModel.Component  
                    System.Windows.Forms.Control  
                          System.Windows.Forms.ScrollableControl  
                                System.Windows.Forms.ContainerControl  
                                      System.Windows.Forms.UserControl 

看到这里,大家应该可以猜到,继承UserControl应该简单一下。确实如此,UserControl基本使用方法是将若干个控件的功能合成一个可重新使用的单元。封装在公共容器内的 Windows 窗体控件的集合。此容器包含与每个 Windows 窗体控件相关联的所有固有功能,允许您有选择地公开和绑定它们的属性。用户控件的例子之一可能是已生成的显示数据库中客户地址数据的控件。该控件可能包括几个用来显示字段的文本框,几个用来通过记录定位的按钮 (Button) 控件。可以有选择地公开数据绑定属性,可以将整个控件打包并在应用程序之间重复使用。

而Custom Control相对就复杂了,通过从 Control 继承来完全从头地创建一个控件Control 类提供控件(例如事件)所需的所有基本功能,但不提供控件特定的功能或图形接口。与通过从用户控件或现有 Windows 窗体控件继承来创建控件相比,通过从 Control 类继承来创建控件需要耗费的心思和精力要多得多。因为作者必须为控件的 OnPaint 事件编写代码以及所需的任何功能特定代码,但同时也允许作者根据自己的需要,灵活地自定义调整控件。时钟控件即是一个自定义控件,它复制模拟时钟的外观和操作,自定义绘图将被调用来促使时钟指针走动,以响应内部计时器组件的 Tick 事件。处于下列情况时使用Custom Control:

  • 想要提供控件的自定义图形化表示形式。
  • 需要实现无法从标准控件获得的自定义功能。

如果大家创建一个c#工程,分别添加2种Item,就会发现区别了。这个系列第一篇就先给大家一个Hello World 级别的代码吧。后面将陆续介绍.net framework下自定义控件的制作。大家复制添加一个Custom Control,粘贴下面的代码,再新建一个Form,应该就可以把控件拖到窗口里了。

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;


namespace CustomCtrl
{
public partial class HelloWorld : Control
{
public HelloWorld()
{
InitializeComponent();
this.Size = new Size(100, 40);
this.BackColor = SystemColors.ActiveBorder;
}

private string helloValue = "Hello World!";

[
Category("Demo"),
Description("Demo from idothink.com")
]
public String CustomValue
{

get
{
return helloValue;
}
set
{
helloValue = value;

// The Invalidate method invokes the OnPaint
Invalidate();
}
}

protected override void OnPaint(PaintEventArgs pe)
{
// Calling the base class OnPaint
base.OnPaint(pe);

// Custom paint code here
pe.Graphics.DrawString(
helloValue,
Font,
new SolidBrush(ForeColor),
ClientRectangle, new StringFormat());
}
}
}

No comments: