using System;
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
namespace ControlSet
{
/**//// <summary>
/// 自绘按钮
/// </summary>
public class MyButton : System.Windows.Forms.Button
{
private bool mouseDown=false;
private bool mouseHover=false;
public MyButton()
{
SetStyle(ControlStyles.UserPaint,true);
MouseDown+=new MouseEventHandler(OnMouseDown);
MouseUp+=new MouseEventHandler(OnMouseUp);
MouseEnter+=new EventHandler(OnMouseEnter);
MouseLeave+=new EventHandler(OnMouseLeave);
Height=23;
Width=75;
}
protected override void OnPaint(PaintEventArgs pe)
{
pe.Graphics.FillRectangle(new SolidBrush(Parent.BackColor),
pe.ClipRectangle);
if (Enabled == false)
{
DrawDisableButton(pe.Graphics);
}
else if (mouseDown)
{
DrawMouseDownButton(pe.Graphics);
}
else if (mouseHover)
{
DrawMouseHoverButton(pe.Graphics);
}
else if (Focused)
{
DrawContainFocusButton(pe.Graphics);
}
else
{
DrawNormalButton(pe.Graphics);
}
WriteText(pe.Graphics);
}
private void OnMouseDown(object sender,MouseEventArgs e)
{
mouseDown=true;
}
private void OnMouseUp(object sender,MouseEventArgs e)
{
mouseDown=false;
PaintEventArgs pe =
new PaintEventArgs(CreateGraphics(),ClientRectangle);
OnPaint(pe);
}
private void OnMouseEnter(object sender,EventArgs e)
{
mouseHover=true;
PaintEventArgs pe =
new PaintEventArgs(CreateGraphics(),ClientRectangle);
OnPaint(pe);
}
private void OnMouseLeave(object sender,EventArgs e)
{
mouseHover=false;
PaintEventArgs pe =
new PaintEventArgs(CreateGraphics(),ClientRectangle);
OnPaint(pe);
}
private void DrawBorder(Graphics g,int state)
{
if (state==1)
{
Pen p = new Pen(SystemColors.ControlLightLight,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(Color.Yellow,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(SystemColors.ControlDark,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(SystemColors.ControlLight,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(Color.SkyBlue,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(Color.FromArgb(161,161,146),1);
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,SystemColors.ControlLightLight);
}
private void DrawMouseHoverButton(Graphics g)
{
DrawBorder(g,2);
PaintBack(g,SystemColors.ControlLightLight);
}
private void DrawMouseDownButton(Graphics g)
{
DrawBorder(g,3);
PaintBack(g,SystemColors.ControlLight);
}
private void DrawDisableButton(Graphics g)
{
DrawBorder(g,4);
PaintBack(g,SystemColors.ControlLight);
}
private void DrawContainFocusButton(Graphics g)
{
DrawBorder(g,5);
PaintBack(g,SystemColors.ControlLightLight);
}
private void PaintBack(Graphics g,Color c)
{
g.FillRectangle(new SolidBrush(c),3,3,
Width-6,Height-6);
}
private void WriteText(Graphics g)
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
namespace ControlSet
{
/**//// <summary>
/// 自绘按钮
/// </summary>
public class MyButton : System.Windows.Forms.Button
{
private bool mouseDown=false;
private bool mouseHover=false;
public MyButton()
{
SetStyle(ControlStyles.UserPaint,true);
MouseDown+=new MouseEventHandler(OnMouseDown);
MouseUp+=new MouseEventHandler(OnMouseUp);
MouseEnter+=new EventHandler(OnMouseEnter);
MouseLeave+=new EventHandler(OnMouseLeave);
Height=23;
Width=75;
}
protected override void OnPaint(PaintEventArgs pe)
{
pe.Graphics.FillRectangle(new SolidBrush(Parent.BackColor),
pe.ClipRectangle);
if (Enabled == false)
{
DrawDisableButton(pe.Graphics);
}
else if (mouseDown)
{
DrawMouseDownButton(pe.Graphics);
}
else if (mouseHover)
{
DrawMouseHoverButton(pe.Graphics);
}
else if (Focused)
{
DrawContainFocusButton(pe.Graphics);
}
else
{
DrawNormalButton(pe.Graphics);
}
WriteText(pe.Graphics);
}
private void OnMouseDown(object sender,MouseEventArgs e)
{
mouseDown=true;
}
private void OnMouseUp(object sender,MouseEventArgs e)
{
mouseDown=false;
PaintEventArgs pe =
new PaintEventArgs(CreateGraphics(),ClientRectangle);
OnPaint(pe);
}
private void OnMouseEnter(object sender,EventArgs e)
{
mouseHover=true;
PaintEventArgs pe =
new PaintEventArgs(CreateGraphics(),ClientRectangle);
OnPaint(pe);
}
private void OnMouseLeave(object sender,EventArgs e)
{
mouseHover=false;
PaintEventArgs pe =
new PaintEventArgs(CreateGraphics(),ClientRectangle);
OnPaint(pe);
}
private void DrawBorder(Graphics g,int state)
{
if (state==1)
{
Pen p = new Pen(SystemColors.ControlLightLight,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(Color.Yellow,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(SystemColors.ControlDark,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(SystemColors.ControlLight,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(Color.SkyBlue,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(Color.FromArgb(161,161,146),1);
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,SystemColors.ControlLightLight);
}
private void DrawMouseHoverButton(Graphics g)
{
DrawBorder(g,2);
PaintBack(g,SystemColors.ControlLightLight);
}
private void DrawMouseDownButton(Graphics g)
{
DrawBorder(g,3);
PaintBack(g,SystemColors.ControlLight);
}
private void DrawDisableButton(Graphics g)
{
DrawBorder(g,4);
PaintBack(g,SystemColors.ControlLight);
}
private void DrawContainFocusButton(Graphics g)
{
DrawBorder(g,5);
PaintBack(g,SystemColors.ControlLightLight);
}
private void PaintBack(Graphics g,Color c)
{
g.FillRectangle(new SolidBrush(c),3,3,
Width-6,Height-6);
}
private void WriteText(Graphics g)