using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace clMessage
{
public partial class mybutton : Button
{
private bool my_mouseDown = false;//鼠标是否按下.
private bool my_mouseHover = false;//鼠标是否经过
private Color bgDisable ;
[Description("鼠标不可用时的背景色")]
public Color BgDisable
{
get { return bgDisable; }
set { bgDisable = value; }
}
private Color bgMouseDown ;
[Description("鼠标按下时按钮的背景色")]
public Color BgMouseDown
{
get { return bgMouseDown; }
set { bgMouseDown = value; }
}
private Color bgMouseHover;
[Description("鼠标经过时按钮的背景色")]
public Color BgMouseHover
{
get { return bgMouseHover; }
set { bgMouseHover = value; }
}
private Color bgNormal ;
[Description("鼠标正常时按钮的背景色")]
public Color BgNormal
{
get { return bgNormal; }
set { bgNormal = value; }
}
private Color bgFocused ;
[Description("按钮得到焦点时的背景色")]
public Color BgFocused
{
get { return bgFocused; }
set { bgFocused = value; }
}
private Color bcDisable;
[Description("鼠标不可用时的内边框色")]
public Color BcDisable
{
get { return bcDisable; }
set { bcDisable = value; }
}
private Color bcMouseDown;
[Description("鼠标按下时内边框的颜色")]
public Color BcMouseDown
{
get { return bcMouseDown; }
set { bcMouseDown = value; }
}
private Color bcMouseHover;
[Description("鼠标经过时内边框的颜色")]
public Color BcMouseHover
{
get { return bcMouseHover; }
set { bcMouseHover = value; }
}
private Color bcNormal;
[Description(" 鼠标正常时内边框的颜色")]
public Color BcNormal
{
get { return bcNormal; }
set { bcNormal = value; }
}
private Color bcFocused;
[Description("鼠标得到焦点时内的边框的颜色")]
public Color BcFocused
{
get { return bcFocused; }
set { bcFocused = value; }
}
private Color boderColorDisable;
[Description("不可用时外边框的颜色")]
public Color BoderColorDisable
{
get { return boderColorDisable; }
set { boderColorDisable = value; }
}
private Color m_textcolor = System.Drawing.Color.Black;
[Description("Red")]
public Color textcolor//文字颜色
{
get
{
return m_textcolor;
}
set
{
m_textcolor = value;
this.Invalidate();
}
}
public mybutton()//初始化
{
InitializeComponent();
this.SetStyle(ControlStyles.UserPaint, true);
this.MouseDown += new MouseEventHandler(my_OnMouseDown);
this.MouseUp += new MouseEventHandler(my_OnMouseUp);
this.MouseEnter += new EventHandler(my_OnMouseEnter);
this.MouseLeave += new EventHandler(my_OnMouseLeave);
Height = 23;
Width = 75;
this.BcNormal = Color.FromArgb(47, 116, 197);
this.BcFocused = Color.FromArgb(252, 184, 51);
this.BcDisable = Color.FromArgb(47, 116, 197);
this.BcMouseDown = Color.FromArgb(47, 116, 197);
this.BcMouseHover = Color.FromArgb(47, 116, 197);
this.BgFocused = Color.FromArgb(207, 234, 255);
this.BgNormal = Color.FromArgb(207, 234, 255);
this.BgMouseHover = Color.FromArgb(191, 226, 255);
this.BgMouseDown = Color.FromArgb(153, 204, 255);
this.BgDisable = Color.FromArgb(204, 199, 184);
}
[System.Diagnostics.DebuggerStepThrough()]
private void InitializeComponent()
{
this.Name = "myButton";
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs pevent)
{
pevent.Graphics.FillRectangle(new SolidBrush(this.Parent.BackColor), pevent.ClipRectangle);
//填充按钮(用背景色)
if ((Enabled == false))//不可用时的状态
{
DrawDisableButton(pevent.Graphics);
}
else if ((my_mouseDown)) //按下时的样式
{
DrawMouseDownButton(pevent.Graphics);
}
else if ((my_mouseHover))//经过时的样式
{
DrawMouseHoverButton(pevent.Graphics);
}
else if ((Focused))//得到焦点的样式。
{
DrawContainFocusButton(pevent.Graphics);
}
else
{
DrawNormalButton(pevent.Graphics);//正常时的样式。
}
WriteText(pevent.Graphics);
}
private void my_OnMouseDown(object sender, MouseEventArgs e)
{
my_mouseDown = true;
PaintEventArgs pe = new PaintEventArgs(CreateGraphics(), ClientRectangle);
OnPaint(pe);
}
private void my_OnMouseUp(object sender, MouseEventArgs e)
{
my_mouseDown = false;
PaintEventArgs pe = new PaintEventArgs(CreateGraphics(), ClientRectangle);
OnPaint(pe);
}
private void my_OnMouseEnter(object sender, EventArgs e)
{
my_mouseHover = true;
PaintEventArgs pe = new PaintEventArgs(CreateGraphics(), ClientRectangle);
OnPaint(pe);
}
private void my_OnMouseLeave(object sender, EventArgs e)
{
my_mouseHover = false;
PaintEventArgs pe = new PaintEventArgs(CreateGraphics(), ClientRectangle);
OnPaint(pe);
}
private void DrawBorder(Graphics g, int state)
{
if ((state == 1))//按钮 正常时的内边框颜色
{
Pen p = new Pen(BcNormal , 2);
g.DrawLine(p, 1, 1, 1, Height - 2);
g.DrawLine(p, 1, 1, Width - 2, 1);
g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2);
g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1);
}
else if ((state == 2))//鼠标经过内边框的颜色
{
Pen p = new Pen( BcMouseHover ,2);
g.DrawLine(p, 1, 1, 1, Height - 2);
g.DrawLine(p, 1, 1, Width - 2, 1);
g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2);
g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1);
}
else if ((state == 3))//鼠标按下时的内边框的颜色
{
Pen p = new Pen(BcMouseDown , 2);
g.DrawLine(p, 1, 1, 1, Height - 2);
g.DrawLine(p, 1, 1, Width - 2, 1);
g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2);
g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1);
}
else if ((state == 4))//不可用时内边框的颜色
{
Pen p = new Pen(BcDisable , 2);
g.DrawLine(p, 1, 1, 1, Height - 2);
g.DrawLine(p, 1, 1, Width - 2, 1);
g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2);
g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1);
}
else if ((state == 5))//得到焦点内边框的颜色
{
Pen p = new Pen( BcFocused , 2);
g.DrawLine(p, 1, 1, 1, Height - 2);
g.DrawLine(p, 1, 1, Width - 2, 1);
g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2);
g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1);
}
if ((state == 4))// 禁用时外边框的样子。
{
Pen p = new Pen( BoderColorDisable , 2);
g.DrawLine(p, 0, 2, 0, Height - 3);
g.DrawLine(p, 2, 0, Width - 3, 0);
g.DrawLine(p, Width - 1, 2, Width - 1, Height - 3);
g.DrawLine(p, 2, Height - 1, Width - 3, Height - 1);
g.DrawLine(p, 0, 2, 2, 0);
g.DrawLine(p, 0, Height - 3, 2, Height - 1);
g.DrawLine(p, Width - 3, 0, Width - 1, 2);
g.DrawLine(p, Width - 3, Height - 1, Width - 1, Height - 3);
}
else//可用内边框时的样子
{
g.DrawLine(Pens.Black, 0, 2, 0, Height - 3);
g.DrawLine(Pens.Black, 2, 0, Width - 3, 0);
g.DrawLine(Pens.Black, Width - 1, 2, Width - 1, Height - 3);
g.DrawLine(Pens.Black, 2, Height - 1, Width - 3, Height - 1);
g.DrawLine(Pens.Black, 0, 2, 2, 0);
g.DrawLine(Pens.Black, 0, Height - 3, 2, Height - 1);
g.DrawLine(Pens.Black, Width - 3, 0, Width - 1, 2);
g.DrawLine(Pens.Black, Width - 3, Height - 1, Width - 1, Height - 3);
}
}
private void DrawNormalButton(Graphics g)//按钮背景色(无事件)
{
DrawBorder(g, 1);
PaintBack(g, bgNormal );
}
private void DrawMouseHoverButton(Graphics g)//鼠标经过时的颜色
{
DrawBorder(g, 2);
PaintBack(g, this.bgMouseHover );
}
private void DrawMouseDownButton(Graphics g)//鼠标按下时的
{
DrawBorder(g, 3);
PaintBack(g, this.bgMouseDown );
}
private void DrawDisableButton(Graphics g)//不可用时
{
DrawBorder(g, 4);
PaintBack(g,bgDisable );
}
private void DrawContainFocusButton(Graphics g) //得到焦点的样式。
{
DrawBorder(g, 5);
PaintBack(g, bgFocused);
}
private void PaintBack(Graphics g, Color c)//背景色
{
g.FillRectangle(new SolidBrush(c), 3, 3, Width - 6, Height - 6);
}
private void WriteText(Graphics g)
{
int x = 0;
int y = 0;
Size s = g.MeasureString(Text, Font).ToSize();
x = (Width - s.Width) / 2;
y = (Height - s.Height) / 2;
if ((Enabled))
{
SolidBrush b = new SolidBrush(m_textcolor);
g.DrawString(Text, Font, b, x, y);
}
else
{
g.DrawString(Text, Font, Brushes.Gray, x, y);
}
}
}
}