首页 > 代码库 > winform自定义控件 (转帖)
winform自定义控件 (转帖)
定义控件
本文以按钮为例,制作一个imagebutton,继承系统button,
分四种状态
1,正常状态
2,获得焦点
3,按下按钮
4,禁用
当然你得准备一张图片,包含四种状态的样式,同样你也可以准备四张图片,这取决于你如何draw按钮
我们定义一个构造函数
private enum btnState
{
Button_UP = 0,//正常状态
Button_Focus = 1,//获得焦点
Button_Down = 2,//按下按钮
Button_Disabled = 3,//禁用按钮
}
然后设置控件“状态”属性
private btnState imgState=0;
//设置属性
public btnState ButtonState
{
get
{
return imgState;
}
set
{
if (imgState != value)
{
imgState = value; Refresh();
}
}
}
然后判断按钮是否被禁用,同样写一个属性
private bool _buttonEnabled = true;
public bool BtnEnabled
{
get
{
return _buttonEnabled;
}
set
{
if (value == false)
{
_buttonEnabled = false;
imgState = btnState.Button_Disabled;
//删除按钮所有事件
disBindEvent();
}
else
{
_buttonEnabled = true;
imgState = btnState.Button_UP;
//绑定按钮所有事件
bindEvent();
}
}
}
好,到这里,我们继续上面设置属性中的事情,开始写按钮时间的绑定和删除
先列举一下,按钮有哪些事件:
从我们定义的按钮状态着手:
Button_UP = 0,//正常状态--------------->mouseleaver,lostfocus
Button_Focus = 1,//获得焦点------------>mousehover,mouseup
Button_Down = 2,//按下按钮------------->mousedown
Button_Disabled = 3,//禁用按钮--------->enable
private void bindEvent()
{
MouseLeave += new EventHandler(imgButton_MouseLeave );
LostFocus += new EventHandler(imgButton_LostFocus );
MouseHover += new EventHandler(imgButton_MouseHover );
MouseUp += new MouseEventHandler(imgButton_MouseUP);
MouseDown += new MouseEventHandler(imgButton_MouseDown);
EnabledChanged += new EventHandler(imgButton_EnAbleChanged);
}
private void disBindEvent()
{
MouseLeave -= new EventHandler(imgButton_MouseLeave);
LostFocus -= new EventHandler(imgButton_LostFocus);
MouseHover -= new EventHandler(imgButton_MouseHover);
MouseUp -= new MouseEventHandler(imgButton_MouseUP);
MouseDown -= new MouseEventHandler(imgButton_MouseDown);
EnabledChanged -= new EventHandler(imgButton_EnAbleChanged);
}
然后接着写上面用到的事件
//鼠标事件
private void imgButton_MouseLeave(object sender, EventArgs e)
{
ButtonState = btnState.Button_UP;
Invalidate();
}
private void imgButton_LostFocus(object sender, EventArgs e)
{
ButtonState = btnState.Button_UP;
Invalidate();
}
private void imgButton_MouseHover(object sender,MouseEventArgs e)
{
ButtonState = btnState.Button_Focus;
Invalidate();
}
private void imgButton_MouseUP(object sender, MouseEventArgs e)
{
ButtonState = btnState.Button_Focus;
Invalidate();
}
private void imgButton_MouseDown(object sender, EventArgs e)
{
ButtonState = btnState.Button_Down;
Invalidate();
}
private void imgButton_EnAbleChanged(object sender, EventArgs e)
{
if (Enabled)
{
ButtonState = btnState.Button_UP;
Invalidate();
}
else
{
ButtonState = btnState.Button_Disabled;
Invalidate();
}
}
到这里,按钮一些事件就写完了,下面开始制作按钮的外观,进行按钮描绘
//绘制按钮外观
private void imgButtonPaint(object sender,PaintEventArgs e)
{
Graphics gr=e .Graphics ;
int indexWidth = Size.Width * (int)imgState ;
if (Image == null) return;
if (Image.Width > indexWidth)
{
gr.DrawImage(Image, 0, 0, new Rectangle(new Point(indexWidth, 0), Size), GraphicsUnit.Pixel);
}
else
{
gr.DrawImage(Image, 0, 0, new Rectangle(new Point(0, 0), Size), GraphicsUnit.Pixel);
}
}
到这里,基本一个imagebutton就制作完了,有兴趣可以试一试,抛砖引玉,这个方法也是刚学过来的
http://www.cnblogs.com/njnudt/archive/2007/08/16/857719.html
winform自定义控件 (转帖)