扩展Button,实例演示如何自绘控件外观
在论坛中看到之后,在博客中保存一个,备查。感谢作者无私贡献。
//重载OnPaint事件
protected override void OnPaint(PaintEventArgs pe)
{
pe.Graphics.Clear(this.Parent.BackColor);
//填充一个矩形的形状来展示按钮
pe.Graphics.FillEllipse(new SolidBrush(Parent.BackColor), pe.ClipRectangle);
if (Enabled == false)
{ //没有被灰掉
DrawDisableButton(pe.Graphics);
//添加按钮文字
WriteText(pe.Graphics);
}
else if (mouseDown)
{ //当鼠标按下
DrawMouseDownButton(pe.Graphics);
WriteTextL(pe.Graphics);
}
else if (mouseHover)
{ //当鼠标悬停
DrawMouseHoverButton(pe.Graphics);
//添加按钮文字
WriteText(pe.Graphics);
}
else if (Focused)
{
//当按钮得到焦点
DrawContainFocusButton(pe.Graphics);
WriteText(pe.Graphics); //添加按钮文字
}
else //当处在正常条件下
{
DrawNomalButton(pe.Graphics);
WriteText(pe.Graphics); //添加按钮文字
}
}
private void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e) //当鼠标按下
{
mouseDown = true;
}
private void OnMouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
mouseDown = false;
PaintEventArgs pe = new PaintEventArgs(CreateGraphics(), ClientRectangle); //重画矩形按钮
OnPaint(pe);
}
private void OnMouseEnter(object sender, System.EventArgs e)
{
mouseHover = true;
PaintEventArgs pe = new PaintEventArgs(CreateGraphics(), ClientRectangle); //重画矩形按钮
OnPaint(pe);
}
private void OnMouseLeave(object sender, System.EventArgs e)
{
mouseHover = false;
PaintEventArgs pe = new PaintEventArgs(CreateGraphics(), ClientRectangle); //重画矩形按钮
OnPaint(pe);
}
private void DrawBorder(Graphics g, int state)
{
//int x=0,y=0; //填充矩形位置
if (state == 1)
{
// Pen p=new Pen(SystemColors.ControlLightLight,2);
}
else if (state == 2)
{
//当鼠标悬停的时候
}
else if (state == 3)
{
}
else if (state == 5)
{
}
else if (state == 6)
{
//当按钮灰掉的时候,重画按钮
// Pen p=new Pen(Color.FromArgb(161,161,161) ,1);
}
else
{
//当按钮为普通状态的时候
// Pen p=new Pen(Color.FromArgb(161,161,161) ,1);
}
}
private void DrawNomalButton(Graphics g)
{
DrawBorder(g, 1);
PaintBack(g, SystemColors.ControlLightLight);
}
private void DrawMouseHoverButton(Graphics g)
{
DrawBorder(g, 2);
//当鼠标悬停的时候背景变篮
PaintBack(g, Color.SkyBlue);
}
private void DrawMouseDownButton(Graphics g)
{
DrawBorder(g, 3);
PaintBack(g, SystemColors.ControlLightLight);
}
private void DrawDisableButton(Graphics g)
{
DrawBorder(g, 4);
PaintBack(g, SystemColors.ControlLightLight);
}
private void DrawContainFocusButton(Graphics g)
{
DrawBorder(g, 5);
PaintBack(g, SystemColors.ControlLightLight);
}
//融合背景色
private void PaintBack(Graphics g, Color c)
{
//用椭圆填充背景色3,3,Width-6,Height-6
g.FillEllipse(new SolidBrush(c), 3, 3, Width - 6, Height - 6);
}
private void WriteText(Graphics g)
{
int x = 0, y = 0;
Size s = g.MeasureString(Text, Font).ToSize();
x = (Width - s.Width) / 2;
y = (Height - s.Height) / 2;
if (Enabled) //画文字
{
g.DrawString(Text, Font, Brushes.Black, x, y);
}
else
{
g.DrawString(Text, Font, Brushes.Gray, x, y);
}
}
private void WriteTextL(Graphics g)
{
int x = 0, y = 0;
Size s = g.MeasureString(Text, Font).ToSize();
x = (Width - s.Width) / 2;
y = (Height - s.Height) / 2;
if (Enabled) //画文字
{
g.DrawString(Text, Font, Brushes.Black, x + 1, y + 1);
}
else
{
g.DrawString(Text, Font, Brushes.Gray, x + 1, y + 1);
}
}
}