从Control类直接继承自定义控件(适合CF,因为CF没有UserControl基类)!
1.支持Click事件的Label控件
using System;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsApplication1
{
public enum VAlign
{
Top,Middle,Bottom
}
public enum HAlign
{
Left,Middle,Right
}
public class ClickableLabel:System.Windows.Forms.Control
{
private VAlign vrtAlign=VAlign.Middle;
private HAlign hrtAlign=HAlign.Middle;
private BorderStyle bdrStyle=BorderStyle.None;

public ClickableLabel()
{
}

public VAlign VAlign
{
get
{
return this.vrtAlign;
}
set
{
this.vrtAlign=value;
this.Invalidate();
}
}

public HAlign HAlign
{
get
{
return this.hrtAlign;
}
set
{
this.hrtAlign=value;
this.Invalidate();
}
}

public BorderStyle BorderStyle
{
get
{
return this.bdrStyle;
}
set
{
this.bdrStyle=value;
this.Invalidate();
}
}

public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
this.Invalidate();
}
}

protected override void OnPaint(PaintEventArgs e)
{
int borderSpace=0;
Graphics gr=e.Graphics;

using (Pen penDraw=new Pen(Color.Black))
{
switch(this.BorderStyle)
{
case BorderStyle.FixedSingle:
gr.DrawRectangle(penDraw,0,0,this.Width-1,this.Height-1);
borderSpace=2;
break;
case BorderStyle.Fixed3D:
gr.DrawRectangle(penDraw,this.ClientRectangle);
borderSpace=2;
break;
case BorderStyle.None:
borderSpace=0;
break;
}
SizeF sizeText=gr.MeasureString(this.Text,this.Font);
float posX=0.0F;
float posY=0.0F;
switch(this.HAlign)
{
case HAlign.Left:
posX=borderSpace;
break;
case HAlign.Middle:
posX=(this.Width-sizeText.Width)/2;
break;
case HAlign.Right:
posX=(this.Width-sizeText.Width)-borderSpace;
break;
}

switch(this.VAlign)
{
case VAlign.Top:
posY=borderSpace;
break;
case VAlign.Middle:
posY=(this.Height-sizeText.Height)/2;
break;
case VAlign.Bottom:
posY=(this.Height-sizeText.Height)-borderSpace;
break;
}
SolidBrush b=new SolidBrush(this.ForeColor);
gr.DrawString(this.Text,this.Font,b,posX,posY);
b.Dispose();
}
base.OnPaint (e);
}
}
}
2.窗体测试代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.RadioButton radioButton3;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.RadioButton radioButton4;
private System.Windows.Forms.RadioButton radioButton5;
private System.Windows.Forms.RadioButton radioButton6;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.RadioButton radioButton7;
private System.Windows.Forms.RadioButton radioButton8;
private System.Windows.Forms.RadioButton radioButton9;
private System.ComponentModel.Container components = null;
private ClickableLabel label=null;
public Form1()
{
InitializeComponent();
label=new ClickableLabel();
label.ForeColor=Color.Black;
label.Location=new System.Drawing.Point(152, 10);
label.Size=new Size(92,30);
label.Text="测试Label";
label.Click+=new EventHandler(label_Click);
this.Controls.Add(label);
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.radioButton3 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.radioButton4 = new System.Windows.Forms.RadioButton();
this.radioButton5 = new System.Windows.Forms.RadioButton();
this.radioButton6 = new System.Windows.Forms.RadioButton();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.radioButton7 = new System.Windows.Forms.RadioButton();
this.radioButton8 = new System.Windows.Forms.RadioButton();
this.radioButton9 = new System.Windows.Forms.RadioButton();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.radioButton3);
this.groupBox1.Controls.Add(this.radioButton2);
this.groupBox1.Controls.Add(this.radioButton1);
this.groupBox1.Location = new System.Drawing.Point(8, 88);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(120, 112);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "BorderStyle";
//
// radioButton3
//
this.radioButton3.Location = new System.Drawing.Point(8, 72);
this.radioButton3.Name = "radioButton3";
this.radioButton3.TabIndex = 2;
this.radioButton3.Text = "None";
this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// radioButton2
//
this.radioButton2.Location = new System.Drawing.Point(8, 48);
this.radioButton2.Name = "radioButton2";
this.radioButton2.TabIndex = 1;
this.radioButton2.Text = "Fixed3D";
this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// radioButton1
//
this.radioButton1.Location = new System.Drawing.Point(8, 24);
this.radioButton1.Name = "radioButton1";
this.radioButton1.TabIndex = 0;
this.radioButton1.Text = "FixedSingle";
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// groupBox2
//
this.groupBox2.Controls.Add(this.radioButton4);
this.groupBox2.Controls.Add(this.radioButton5);
this.groupBox2.Controls.Add(this.radioButton6);
this.groupBox2.Location = new System.Drawing.Point(144, 88);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(120, 112);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "HAlign";
//
// radioButton4
//
this.radioButton4.Location = new System.Drawing.Point(8, 72);
this.radioButton4.Name = "radioButton4";
this.radioButton4.TabIndex = 2;
this.radioButton4.Text = "Right";
this.radioButton4.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// radioButton5
//
this.radioButton5.Location = new System.Drawing.Point(8, 48);
this.radioButton5.Name = "radioButton5";
this.radioButton5.TabIndex = 1;
this.radioButton5.Text = "Middle";
this.radioButton5.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// radioButton6
//
this.radioButton6.Location = new System.Drawing.Point(8, 24);
this.radioButton6.Name = "radioButton6";
this.radioButton6.TabIndex = 0;
this.radioButton6.Text = "Left";
this.radioButton6.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// groupBox3
//
this.groupBox3.Controls.Add(this.radioButton7);
this.groupBox3.Controls.Add(this.radioButton8);
this.groupBox3.Controls.Add(this.radioButton9);
this.groupBox3.Location = new System.Drawing.Point(280, 88);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(120, 112);
this.groupBox3.TabIndex = 2;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "VAlign";
//
// radioButton7
//
this.radioButton7.Location = new System.Drawing.Point(8, 72);
this.radioButton7.Name = "radioButton7";
this.radioButton7.TabIndex = 2;
this.radioButton7.Text = "Bottom";
this.radioButton7.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// radioButton8
//
this.radioButton8.Location = new System.Drawing.Point(8, 48);
this.radioButton8.Name = "radioButton8";
this.radioButton8.TabIndex = 1;
this.radioButton8.Text = "Middle";
this.radioButton8.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// radioButton9
//
this.radioButton9.Location = new System.Drawing.Point(8, 24);
this.radioButton9.Name = "radioButton9";
this.radioButton9.TabIndex = 0;
this.radioButton9.Text = "Top";
this.radioButton9.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(416, 221);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
{
RadioButton r=null;
if (sender==this.radioButton1)
{
r=(RadioButton)sender;
if (r.Checked==true)
{
this.label.BorderStyle=BorderStyle.FixedSingle;
}
}
if (sender==this.radioButton2)
{
r=(RadioButton)sender;
if (r.Checked==true)
{
this.label.BorderStyle=BorderStyle.Fixed3D;
}
}
if (sender==this.radioButton3)
{
r=(RadioButton)sender;
if (r.Checked==true)
{
this.label.BorderStyle=BorderStyle.None;
}
}

if (sender==this.radioButton4)
{
r=(RadioButton)sender;
if (r.Checked==true)
{
this.label.HAlign=HAlign.Right;
}
}
if (sender==this.radioButton5)
{
r=(RadioButton)sender;
if (r.Checked==true)
{
this.label.HAlign=HAlign.Middle;
}
}
if (sender==this.radioButton6)
{
r=(RadioButton)sender;
if (r.Checked==true)
{
this.label.HAlign=HAlign.Left;
}
}

if (sender==this.radioButton7)
{
r=(RadioButton)sender;
if (r.Checked==true)
{
this.label.VAlign=VAlign.Bottom;
}
}
if (sender==this.radioButton8)
{
r=(RadioButton)sender;
if (r.Checked==true)
{
this.label.VAlign=VAlign.Middle;
}
}
if (sender==this.radioButton9)
{
r=(RadioButton)sender;
if (r.Checked==true)
{
this.label.VAlign=VAlign.Top;
}
}
}

private void label_Click(object sender, EventArgs e)
{
MessageBox.Show("This is a label than can be able to click!");
}
}
}
