第三章例题:自定义控件
对教材p120部分创建控件进行演示
1
using System;
2
using System.Drawing;
3
using System.ComponentModel;
4
using System.Windows.Forms;
5
6
namespace my_control
7
{
8
//所有控件必须派生自Control类
9
public class CustomControl : System.Windows.Forms.Control
10
{
11
//重载控件的OnPaint方法
12
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
13
{
14
//绘制字符串
15
e.Graphics.DrawString("Written with GDI+ on OnPaint event",
16
new Font("Arial",12), new SolidBrush(Color.Red), 0, 0);
17
}
18
}
19
}
using System;2
using System.Drawing;3
using System.ComponentModel;4
using System.Windows.Forms;5

6
namespace my_control7
{8
//所有控件必须派生自Control类9
public class CustomControl : System.Windows.Forms.Control 10
{11
//重载控件的OnPaint方法12
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)13
{14
//绘制字符串15
e.Graphics.DrawString("Written with GDI+ on OnPaint event", 16
new Font("Arial",12), new SolidBrush(Color.Red), 0, 0);17
}18
}19
}完整控件代码和控件测试程序下载:my_control.rar


浙公网安备 33010602011771号