如果要向现有控件添加更多功能,可以创建继承自现有控件的控件。 新控件包含基本控件的所有功能和可视方面,但为你提供了扩展它的机会。 例如,如果创建了继承 Button的控件,则新控件的外观和操作与按钮完全相同。 您可以创建新的方法和属性,以自定义控件的行为。 某些控件允许替代 OnPaint 方法以更改控件的外观。
向项目添加自定义控件
创建新项目后,使用 Visual Studio 模板创建用户控件。 以下步骤演示如何将用户控件添加到项目:
在 Visual Studio 中,找到 项目资源管理器 窗格。 右键单击项目,然后选择 “添加>用户控件”。
选择 “自定义控件”(Windows 窗体) 项。
在“名称”框中,键入用户控件的名称。 Visual Studio 提供一个可以使用的默认和唯一名称。 接下来,按 添加。
在控件 的设计 模式下,按 F7 或单击 切换到代码视图 链接。
小窍门
还可以右键单击 解决方案资源管理器 窗口中的文件,然后选择“ 查看代码”。
将自定义控件更改为按钮
在本部分中,你将了解如何将自定义控件更改为计数并显示单击的按钮的次数。
CustomControl1
后,应打开控件设计器。 如果未打开,请双击解决方案资源管理器中的控件。 按照以下步骤将自定义控件转换为继承自 Button
的控件并对其进行扩展:
打开控件设计器后,按 F7 或右键单击设计器窗口,然后选择 查看代码。
在代码编辑器中,应会看到类定义:
namespace CustomControlProject { public partial class CustomControl2 : Control { public CustomControl2() { InitializeComponent(); } protected override void OnPaint(PaintEventArgs pe) { base.OnPaint(pe); } } }
Public Class CustomControl2 Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) MyBase.OnPaint(e) 'Add your custom paint code here End Sub End Class
将基类从
Control
更改为Button
。重要
如果使用 Visual Basic,则基类在控件的 *.designer.vb 文件中定义。 在 Visual Basic 中使用的基类
System.Windows.Forms.Button
。添加名为
_counter
的类范围变量。private int _counter = 0;
Private _counter As Integer = 0
重写
OnPaint
方法。 此方法将绘制控件。 控件应绘制按钮顶部的字符串,因此必须先调用基类的OnPaint
方法,然后绘制字符串。protected override void OnPaint(PaintEventArgs pe) { // Draw the control base.OnPaint(pe); // Paint our string on top of it pe.Graphics.DrawString($"Clicked {_counter} times", Font, Brushes.Purple, new PointF(3, 3)); }
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) ' Draw the control MyBase.OnPaint(e) ' Paint our string on top of it e.Graphics.DrawString($"Clicked {_counter} times", Font, Brushes.Purple, New PointF(3, 3)) End Sub
最后,替代
OnClick
方法。 每次按下控件时都会调用此方法。 代码将增加计数器,然后调用Invalidate
方法,该方法强制控件重新绘制自身。protected override void OnClick(EventArgs e) { // Increase the counter and redraw the control _counter++; Invalidate(); // Call the base method to invoke the Click event base.OnClick(e); }
Protected Overrides Sub OnClick(e As EventArgs) ' Increase the counter and redraw the control _counter += 1 Invalidate() ' Call the base method to invoke the Click event MyBase.OnClick(e) End Sub
最终代码应类似于以下代码片段:
public partial class CustomControl1 : Button { private int _counter = 0; public CustomControl1() { InitializeComponent(); } protected override void OnPaint(PaintEventArgs pe) { // Draw the control base.OnPaint(pe); // Paint our string on top of it pe.Graphics.DrawString($"Clicked {_counter} times", Font, Brushes.Purple, new PointF(3, 3)); } protected override void OnClick(EventArgs e) { // Increase the counter and redraw the control _counter++; Invalidate(); // Call the base method to invoke the Click event base.OnClick(e); } }
Public Class CustomControl1 Private _counter As Integer = 0 Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) ' Draw the control MyBase.OnPaint(e) ' Paint our string on top of it e.Graphics.DrawString($"Clicked {_counter} times", Font, Brushes.Purple, New PointF(3, 3)) End Sub Protected Overrides Sub OnClick(e As EventArgs) ' Increase the counter and redraw the control _counter += 1 Invalidate() ' Call the base method to invoke the Click event MyBase.OnClick(e) End Sub End Class
创建控件后,编译项目以使用新控件填充 工具箱 窗口。 打开窗体设计器并将控件拖动到窗体。 运行项目并按按钮。 每按一次,单击次数会增加一次。 单击总数将作为文本显示在按钮顶部。