日期:2011-05-18 浏览次数:20412 次
因为我们这个控件是实现背景渐变及文字填充,所以override Paint事件以完成自画。
为了完成override,现在以下的准备工作(写几个在Paint事件用的着的事件).
//使用角度的方法渐近重画Button
private void DrawButtonWithAngle(Graphics dbg)
{
LinearGradientBrush brush=new LinearGradientBrush(new Rectangle(0,0,this.Width,this.Height),froColor,backColor,angle);
dbg.FillRectangle(brush,0,0,this.Width,this.Height);
brush.Dispose();
}
////使用模式的方法渐近重画Button
private void DrawButtonWithMode(Graphics dbg,LinearGradientMode Mode)
{
LinearGradientBrush brush=new LinearGradientBrush(new Rectangle(0,0,this.Width,this.Height),froColor,backColor,Mode);
dbg.FillRectangle(brush,0,0,this.Width,this.Height);
brush.Dispose();
}
//重画Button的文本(Text),不使用图案填充
private void DrawButtonText(Graphics dbg)
{
StringFormat format=new StringFormat();
format.LineAlignment=StringAlignment.Center;
format.Alignment=StringAlignment.Center;
dbg.DrawString(this.Text,this.Font,new SolidBrush(this.ForeColor),new Rectangle(0,0,this.Width,this.Height),format);
}
//override DrawButtonText函数,使之可以用图案填充文本
private void DrawButtonText(Graphics dbg, HatchStyle hs)
{
StringFormat format=new StringFormat();
format.LineAlignment=StringAlignment.Center;
format.Alignment=StringAlignment.Center;