XPButton.cs代码
using System;
using System.Windows;
using System.Drawing;
using System.Drawing.DrawingD;
using System.Windows .Forms ;
namespace XPCtrl
{
/// <summary>
/// XPButton 的摘要说明。
/// </summary>
public class XPButton:System.Windows.Forms.Button
{
private bool mouseover=false;
public XPButton()
{
//
// TODO: 在此处添加构造函数逻辑
//
this.Cursor = System.Windows.Forms.Cursors.Hand;
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
// base.OnPaint (e);
// System.Drawing.Graphics pp=this.CreateGraphics();
// e.Graphics.Clear(Color.Transparent);
// e.Graphics.DrawEllipse(new System.Drawing.Pen(System.Drawing.Color.WhiteSmoke,),0,0,this.Width,this.Height);
// System.Drawing.SolidBrush dd=new SolidBrush(System.Drawing.Color.WhiteSmoke);
// e.Graphics.FillEllipse(dd,0,0,this.Width,this.Height);
// (this.BackColor.ToString ())
Color c = Color.FromArgb
(,,);
Color c = Color.FromArgb
(9,9,9);
if(mouseover)
{
c=Color.FromArgb(,,);
//c=Color.FromArgb(9,9,9);
c=Color.FromArgb(80,7,90);
}
Brush b = new System.Drawing.DrawingD.LinearGradientBrush
(ClientRectangle, c, c, LinearGradientMode.Vertical);
//System.Drawing.Region=new Region(
int offsetwidth=this.Width/0;
Point[] points=new Point[8];
points[0].X=offsetwidth;
points[0].Y=0;
points[].X=this.Width-offsetwidth;
points[].Y=0;
points[].X=this.Width;
points[].Y=offsetwidth;
points[].X=this.Width;
points[].Y=this.Height-offsetwidth;
points[].X=this.Width-offsetwidth;
points[].Y=this.Height;
points[].X=offsetwidth;
points[].Y=this.Height;
points[6].X=0;
points[6].Y=this.Height-offsetwidth;
points[7].X=0;
points[7].Y=offsetwidth;
// e.Graphics.FillRectangle (b, ClientRectangle);
e.Graphics.FillPolygon(b,points,FillMode.Winding);
if(this.Focused)
{
int offsetwidth=(this.Width-)/0+;
Point[] points=new Point[8];
points[0].X=offsetwidth;
points[0].Y=;
points[].X=this.Width-offsetwidth;
points[].Y=;
points[].X=this.Width-;
points[].Y=offsetwidth;
points[].X=this.Width-;
points[].Y=this.Height-offsetwidth;
points[].X=this.Width-offsetwidth;
points[].Y=this.Height-;
points[].X=;
points[].Y=this.Height-;
points[6].X=;
points[6].Y=this.Height-offsetwidth;
points[7].X=;
points[7].Y=offsetwidth;
// e.Graphics.DrawPolygon(new Pen(Color.Yellow,),points);
Pen p=new Pen(Color.Orange,);
Pen p=new Pen(Color.Wheat,);
//p.DashStyle=DashStyle.DashDot;
e.Graphics.DrawLine(p,points[0],points[]);
e.Graphics.DrawLine(p,points[],points[]);
e.Graphics.DrawLine(p,points[],points[]);
e.Graphics.DrawLine(p,points[],points[]);
e.Graphics.DrawLine(p,points[],points[]);
e.Graphics.DrawLine(p,points[],points[6]);
e.Graphics.DrawLine(p,points[6],points[7]);
e.Graphics.DrawLine(p,points[7],points[0]);
}
e.Graphics.DrawPolygon(new Pen(Color.DarkBlue,),points);
// e.Graphics.DrawLine(new Pen(Color.DarkBlue,),new Point(0,0),new Point(this.Width,0));
// e.Graphics.DrawLine(new Pen(Color.DarkBlue,),new Point(0,0),new Point(0,this.Height));
// e.Graphics.DrawLine(new Pen(Color.DarkBlue,),new Point(this.Width,this.Height),new Point(this.Width,0));
// e.Graphics.DrawLine(new Pen(Color.DarkBlue,),new Point(this.Width,this.Height),new Point(0,this.Height));
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.DisplayFormatControl;
drawFormat.LineAlignment=StringAlignment.Center;
drawFormat.Alignment=System.Drawing.StringAlignment.Center;
e.Graphics.DrawString(this.Text,this.Font,new LinearGradientBrush(this.ClientRectangle,Color.Black,Color.Black,LinearGradientMode.Vertical),this.ClientRectangle,drawFormat);
b.Dispose();
}
protected override void OnLeave(EventArgs e)
{
base.OnLeave (e);
}
// protected override void OnMouseHover(EventArgs e)
// {
//
// mouseover=true;
// this.Invalidate(false);
// base.OnMouseHover (e);
// }
protected override void OnMouseEnter(EventArgs e)
{
mouseover=true;
this.Invalidate(false);
base.OnMouseEnter (e);
}
protected override void OnNotifyMessage(System.Windows.Forms.Message m)
{
base.OnNotifyMessage (m);
}
protected override void OnMouseLeave(EventArgs e)
{
mouseover=false;
this.Invalidate(false);
base.OnMouseLeave (e);
}
private void DrawButton( System.Drawing.Graphics g)
{
}
protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent)
{
// Color c = Color.FromArgb
// (,,);
// Color c = Color.FromArgb
// (9,9,9);
// if(mouseover)
// {
// c=Color.FromArgb(,,);
// //c=Color.FromArgb(9,9,9);
// c=Color.FromArgb(80,7,90);
// }
// Brush b = new System.Drawing.DrawingD.LinearGradientBrush
// (ClientRectangle, c, c, LinearGradientMode.Vertical);
//pevent.Graphics .DrawRectangle(new Pen(Color.Transparent,),this.ClientRectangle);
pevent.Graphics .Clear(Color.Wheat );
//base.OnPaintBackground (pevent);
}
}
}
在Form.cs中调用
我的方法是先拖一个windows的Button过来,再改两个地方
一个是变量定义:
private XPCtrl.XPButton button;
另一个是Windows 窗体设计器生成的代码中:
this.button = new XPCtrl.XPButton();
效果:
这个我用的颜色是灰的,如果换成彩色就可以很漂亮了









