译者 :
经常在论坛上有人问如果写自己的控件 , 如何改造系统已经有的控件 . 这篇文章对这个问题作了非常清楚的描述 . 这是在 codeproject 上颇受好评的一篇入门级的文章 .
** 简介 ** ** **
这篇文章简明扼要的介绍了在 .Net Framework 中如何来产生自己的定制控件 .
我们讲述如果产生一个定制控件 , 然后在一个应用程序中使用它 . 我在这个控件中加入了自己的一些属性 , 我们可以看一下在 C# 中是如果实现的 .
** 生成控件 ** ** **
- 在 Visual Studio .net 中 , 用 Windows 控件库来作为模板 , 生成一个新的项目 . 项目的名称为 _ ctlCuteButton. _
- 首先么做的就是要修改 cuteButton 的父类
修改下面这行 :
public class cuteButton : System.Windows.Forms.Control
改为 :
public class cuteButton : System.Windows.Forms.Button
现在我们的控件就是从 System.Windows.Forms.Button 类来继承了 .
- 现在让我们给这个控件加一些属性 , 在 cuteButton 类中加入下面的代码就可以完成 .
private Color m_color1 = Color.LightGreen; // 第一种颜色
private Color m_color2 = Color.DarkBlue; // 第二种颜色
private int m_color1Transparent = 64; // 第一种颜色的透明度
private int m_color2Transparent = 64; // 第二种颜色的透明度
{
get { return m_color1; }
set { m_color1 = value; Invalidate(); }
}
public Color cuteColor2
{
get { return m_color2; }
set { m_color2 = value; Invalidate(); }
}
public int cuteTransparent1
{
get { return m_color1Transparent; }
set { m_color1Transparent = value; Invalidate(); }
}
public int cuteTransparent2
{
get { return m_color2Transparent; }
set { m_color2Transparent = value; Invalidate(); }
}
上面的 Invalidate() 方法是用来在设计和使用时用来刷新控件的内部 .
- 重写父类的 Paint 事件
// Calling the base class OnPaint
base.OnPaint(pe);
// Create two semi-transparent colors
Color c1 = Color.FromArgb(m_color1Transparent , m_color1);
Color c2 = Color.FromArgb(m_color2Transparent , m_color2);
Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle,
c1, c2, 10);
pe.Graphics.FillRectangle (b, ClientRectangle);
b.Dispose();
- 现在我们可以编译这控件了 , 可以使用快捷键
1<ctrl>+<shift>+<b>.
2
3
4
5下面是全部的代码 :
6
7using System;
8
9using System.Collections;
10
11using System.ComponentModel;
12
13using System.Drawing;
14
15using System.Data;
16
17using System.Windows.Forms;
18
19namespace ctlCuteButton
20
21{
22
23/// <summary>
24
25/// Summary description for cuteButton.
26
27/// </summary>
28
29public class cuteButton : System.Windows.Forms.Button
30
31{
32
33private Color m_color1 = Color.LightGreen; // 第一种颜色
34
35private Color m_color2 = Color.DarkBlue; // 第二种颜色
36
37private int m_color1Transparent = 64; // 第一种颜色的透明度
38
39private int m_color2Transparent = 64; // 第二种颜色的透明度
40
41public Color cuteColor1
42
43{
44
45get { return m_color1; }
46
47set { m_color1 = value; Invalidate(); }
48
49}
50
51public Color cuteColor2
52
53{
54
55get { return m_color2; }
56
57set { m_color2 = value; Invalidate(); }
58
59}
60
61public int cuteTransparent1
62
63{
64
65get { return m_color1Transparent; }
66
67set { m_color1Transparent = value; Invalidate(); }
68
69}
70
71public int cuteTransparent2
72
73{
74
75get { return m_color2Transparent; }
76
77set { m_color2Transparent = value; Invalidate(); }
78
79}
80
81public cuteButton()
82
83{
84
85}
86
87protected override void OnPaint(PaintEventArgs pe)
88
89{
90
91// Calling the base class OnPaint
92
93base.OnPaint(pe);
94
95// Create two semi-transparent colors
96
97Color c1 = Color.FromArgb
98
99(m_color1Transparent , m_color1);
100
101Color c2 = Color.FromArgb
102
103(m_color2Transparent , m_color2);</b></shift></ctrl>