xpbutton.cs代码
using system;
using system.windows;
using system.drawing;
using system.drawing.drawing2d;
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,5),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 c5 = color.fromargb
(255,255,255);
color c2 = color.fromargb
(192,192,192);
if(mouseover)
{
c5=color.fromargb(245,245,245);
//c2=color.fromargb(192,192,192);
c2=color.fromargb(180,175,190);
}
brush b = new system.drawing.drawing2d.lineargradientbrush
(clientrectangle, c5, c2, lineargradientmode.vertical);
//system.drawing.region=new region(
int offsetwidth=this.width/50;
point[] points=new point[8];
points[0].x=offsetwidth;
points[0].y=0;
points[1].x=this.width-offsetwidth;
points[1].y=0;
points[2].x=this.width;
points[2].y=offsetwidth;
points[3].x=this.width;
points[3].y=this.height-offsetwidth;
points[4].x=this.width-offsetwidth;
points[4].y=this.height;
points[5].x=offsetwidth;
points[5].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 offsetwidth1=(this.width-5)/50+2;
point[] points1=new point[8];
points1[0].x=offsetwidth1;
points1[0].y=2;
points1[1].x=this.width-offsetwidth1;
points1[1].y=2;
points1[2].x=this.width-1;
points1[2].y=offsetwidth1;
points1[3].x=this.width-1;
points1[3].y=this.height-offsetwidth1;
points1[4].x=this.width-offsetwidth1;
points1[4].y=this.height-1;
points1[5].x=1;
points1[5].y=this.height-1;
points1[6].x=2;
points1[6].y=this.height-offsetwidth1;
points1[7].x=2;
points1[7].y=offsetwidth1;
// e.graphics.drawpolygon(new pen(color.yellow,2),points1);
pen p=new pen(color.orange,2);
pen p1=new pen(color.wheat,2);
//p.dashstyle=dashstyle.dashdot;
e.graphics.drawline(p1,points1[0],points1[1]);
e.graphics.drawline(p,points1[1],points1[2]);
e.graphics.drawline(p,points1[2],points1[3]);
e.graphics.drawline(p,points1[3],points1[4]);
e.graphics.drawline(p,points1[4],points1[5]);
e.graphics.drawline(p,points1[5],points1[6]);
e.graphics.drawline(p1,points1[6],points1[7]);
e.graphics.drawline(p1,points1[7],points1[0]);
}
e.graphics.drawpolygon(new pen(color.darkblue,2),points);
// e.graphics.drawline(new pen(color.darkblue,2),new point(0,0),new point(this.width,0));
// e.graphics.drawline(new pen(color.darkblue,2),new point(0,0),new point(0,this.height));
// e.graphics.drawline(new pen(color.darkblue,2),new point(this.width,this.height),new point(this.width,0));
// e.graphics.drawline(new pen(color.darkblue,2),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 c5 = color.fromargb
// (255,255,255);
// color c2 = color.fromargb
// (192,192,192);
// if(mouseover)
// {
// c5=color.fromargb(245,245,245);
// //c2=color.fromargb(192,192,192);
// c2=color.fromargb(180,175,190);
// }
// brush b = new system.drawing.drawing2d.lineargradientbrush
// (clientrectangle, c5, c2, lineargradientmode.vertical);
//pevent.graphics .drawrectangle(new pen(color.transparent,2),this.clientrectangle);
pevent.graphics .clear(color.wheat );
//base.onpaintbackground (pevent);
}
}
}
在form1.cs中调用
我的方法是先拖一个windows的button过来,再改两个地方
一个是变量定义:
private xpctrl.xpbutton button1;
另一个是windows 窗体设计器生成的代码中:
this.button1 = new xpctrl.xpbutton();
效果:
这个我用的颜色是灰的,如果换成彩色就可以很漂亮了