【WinForm窗体控件开发】之二 简单的窗体控件
我们做一个简单的例子:
下面是继承自Control类的自定义窗体控件的代码:
FirstControl继承控件代码
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace 之二简单的窗体控件Demo
{
public class FirstControl:Control
{
/// <summary>
/// Constructor
/// </summary>
public FirstControl() { }
#region FirstControl 使用该属性来设置从 Control 继承的 Text 属性的显示格式
// 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;
[
Category("Alignment"),
Description("Specifies the alignment of text.")
]
public ContentAlignment TextAlignment
{
get
{
return alignmentValue;
}
set
{
alignmentValue = value;
// The Invalidate method invokes the OnPaint method described
// in step 3.
// 在设置更改控件可视显示的属性时,必须调用 Invalidate 方法来重新绘制该控件。
// Invalidate 是在 Control 基类中定义的。
Invalidate();
}
}
#endregion
/// <summary>
/// 重写从 Control 继承的受保护的 OnPaint 方法,以便为控件提供呈现逻辑。
/// 如果不改写 OnPaint,您的控件将无法自行绘制。
/// 在下面的代码片段中,OnPaint 方法显示从 Control 继承的 Text 属性,并使用 alignmentValue 字段指定的对齐方式。
/// </summary>
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;
}
// 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);
}
}
}
窗体代码:
namespace 之二简单的窗体控件Demo
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.firstControl1 = new 之二简单的窗体控件Demo.FirstControl();
this.firstControl2 = new 之二简单的窗体控件Demo.FirstControl();
this.firstControl3 = new 之二简单的窗体控件Demo.FirstControl();
this.SuspendLayout();
//
// firstControl1
//
this.firstControl1.Location = new System.Drawing.Point(12, 24);
this.firstControl1.Name = "firstControl1";
this.firstControl1.Size = new System.Drawing.Size(122, 21);
this.firstControl1.TabIndex = 0;
this.firstControl1.Text = "firstControl1";
this.firstControl1.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
//
// firstControl2
//
this.firstControl2.Location = new System.Drawing.Point(12, 51);
this.firstControl2.Name = "firstControl2";
this.firstControl2.Size = new System.Drawing.Size(122, 23);
this.firstControl2.TabIndex = 1;
this.firstControl2.Text = "firstControl2";
this.firstControl2.TextAlignment = System.Drawing.ContentAlignment.MiddleCenter;
//
// firstControl3
//
this.firstControl3.Location = new System.Drawing.Point(12, 80);
this.firstControl3.Name = "firstControl3";
this.firstControl3.Size = new System.Drawing.Size(122, 23);
this.firstControl3.TabIndex = 2;
this.firstControl3.Text = "firstControl3";
this.firstControl3.TextAlignment = System.Drawing.ContentAlignment.MiddleRight;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(214, 182);
this.Controls.Add(this.firstControl3);
this.Controls.Add(this.firstControl2);
this.Controls.Add(this.firstControl1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private FirstControl firstControl1;
private FirstControl firstControl2;
private FirstControl firstControl3;
}
}
效果图:
控件编译后会在Toolbox工具箱中出现一个齿轮样子的小工具,直接在Form窗体上使用即可;
注意属性栏中最下面一个TextAligment属性,这是我们自己为FirstControl控件添加的属性
关于控件的常用的Attribute的详细介绍我们在下一节中进行详细介绍。