1.创建一个自定义控件UserControl1,如图1所示:
图1
2.代码如下:
// 此类用于实现绘制一条水平分隔线, 按F5运行一次,再拖动当前控件到Form窗体即可看到
// 自定义属性:DrawLineColor和自定义事件ThisLine_Selected
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
// 自定义属性:DrawLineColor和自定义事件ThisLine_Selected
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void UserControl1_Load(object sender, EventArgs e)
{
{
}
protected override void OnPaintBackground(PaintEventArgs e)
{
//不进行背景的绘制
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
//WS_EX_TRANSPARENT
cp.ExStyle |= 0x00000020;
return cp;
}
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Pen pen = new Pen(drawLineColor);
pen.Width = 2;
// 绘制一条水平分割线
e.Graphics.DrawLine(pen, new Point(5, 5), new Point(this.Width - 5, 5));
绘制panel的背景图像
//if (BackgroundImage != null) e.Graphics.DrawImage(this.BackgroundImage, new Point(0, 0));
}
protected override void OnPaintBackground(PaintEventArgs e)
{
//不进行背景的绘制
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
//WS_EX_TRANSPARENT
cp.ExStyle |= 0x00000020;
return cp;
}
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Pen pen = new Pen(drawLineColor);
pen.Width = 2;
// 绘制一条水平分割线
e.Graphics.DrawLine(pen, new Point(5, 5), new Point(this.Width - 5, 5));
绘制panel的背景图像
//if (BackgroundImage != null) e.Graphics.DrawImage(this.BackgroundImage, new Point(0, 0));
}
// 存储颜色值的内部变量
private Color drawLineColor = Color.Red;
[Description("修改此值,可修改分割线的颜色"), Category("自定义属性")]
// 控件的自定义属性值
public Color DrawLineColor
{
get
{
return drawLineColor;
}
set
{
drawLineColor = value;
// 此处修改,为自定义属性变动时,执行的操作
// 此处当颜色值属性变动时,使用新的颜色,使自定义控件重绘
this.Invalidate();
}
}
private Color drawLineColor = Color.Red;
[Description("修改此值,可修改分割线的颜色"), Category("自定义属性")]
// 控件的自定义属性值
public Color DrawLineColor
{
get
{
return drawLineColor;
}
set
{
drawLineColor = value;
// 此处修改,为自定义属性变动时,执行的操作
// 此处当颜色值属性变动时,使用新的颜色,使自定义控件重绘
this.Invalidate();
}
}
// 自定义事件的参数类型
public delegate void select_Handle(object sender, EventArgs e, string orther);
[Description("当点击控件时发生,调用选中当前控件逻辑"), Category("自定义事件")]
// 自定义事件名,
public event select_Handle ThisLine_Selected;
// 在当前控件的某个默认事件中调用自定义事件,此处使用默认的Click事件时,调用自定义的选中事件This_Selected
private void SplitLineHorizontal_Click(object sender, EventArgs e)
{
// 定义选中控件时,显示为红色
//DrawColor = Color.Red;
//调用自定义事件
if (ThisLine_Selected != null) ThisLine_Selected(this, new EventArgs(), "其它参数");
}
}
public delegate void select_Handle(object sender, EventArgs e, string orther);
[Description("当点击控件时发生,调用选中当前控件逻辑"), Category("自定义事件")]
// 自定义事件名,
public event select_Handle ThisLine_Selected;
// 在当前控件的某个默认事件中调用自定义事件,此处使用默认的Click事件时,调用自定义的选中事件This_Selected
private void SplitLineHorizontal_Click(object sender, EventArgs e)
{
// 定义选中控件时,显示为红色
//DrawColor = Color.Red;
//调用自定义事件
if (ThisLine_Selected != null) ThisLine_Selected(this, new EventArgs(), "其它参数");
}
}
3.效果如图2,3,4,5所示:
图2
图3
图4
图5