首页 > 开发 > 综合 > 正文

C#中水晶按钮的程序生成.

2024-07-21 02:20:05
字体:
来源:转载
供稿:网友
做了一个控件,这样就可以告别图片按钮了:)

效果:

程序源代码:



using system;
using system.collections;
using system.componentmodel;
using system.drawing;
using system.data;
using system.windows.forms;
using system.drawing.drawing2d;
using system.drawing.text;

namespace windowscontrollibrary1
{
/// <summary>
/// usercontrol1 的摘要说明。
/// </summary>
public class usercontrol1 : button
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
private bool xiacen=false;
private bool mousemove=false;
private color backcolor;
public usercontrol1()
{
// 该调用是 windows.forms 窗体设计器所必需的。
initializecomponent();

// todo: 在 initcomponent 调用后添加任何初始化
backcolor=this.backcolor;
//this.text=this.showfocuscues.tostring();
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.dispose();
}
base.dispose( disposing );
}

#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void initializecomponent()
{
//
// usercontrol1
//
this.mouseup += new system.windows.forms.mouseeventhandler(this.usercontrol1_mouseup);
this.paint += new system.windows.forms.painteventhandler(this.usercontrol1_paint);
this.mouseenter += new system.eventhandler(this.usercontrol1_mouseenter);
this.keyup += new system.windows.forms.keyeventhandler(this.usercontrol1_keyup);
this.keydown += new system.windows.forms.keyeventhandler(this.usercontrol1_keydown);
this.backcolorchanged += new system.eventhandler(this.usercontrol1_backcolorchanged);
this.mouseleave += new system.eventhandler(this.usercontrol1_mouseleave);
this.mousedown += new system.windows.forms.mouseeventhandler(this.usercontrol1_mousedown);

}
#endregion



protected graphicspath getgraphicspath(rectangle rect)
{
graphicspath clientpath = new system.drawing.drawing2d.graphicspath();
if(rect.width<=0)
{
rect.width=1;
}
if(rect.height<=0)
{
rect.height=1;
}

clientpath.addarc(rect.left,rect.top,rect.height,rect.height,90f,180f);
clientpath.addarc(rect.right-rect.height,rect.top,rect.height,rect.height,270f,180f);
clientpath.closefigure();
return clientpath;
}

protected graphicspath getgraphicspath1(rectangle rect)
{
graphicspath clientpath = new system.drawing.drawing2d.graphicspath();
if(rect.width<=0)
{
rect.width=1;
}
if(rect.height<=0)
{
rect.height=1;
}

clientpath.addarc(rect.left,rect.top,rect.height,rect.height,190f,80f);
clientpath.addarc(rect.right-rect.height,rect.top,rect.height,rect.height,270f,80f);
clientpath.closefigure();
return clientpath;
}

private void drawyinying(graphics gr,bool xiacen)
{
rectangle rect= this.clientrectangle;
rect.inflate(-(rect.width/10),-(rect.height)/4);
float bf1=rect.width/100f;
float bf2=rect.height/100f;

rect.y=rect.y+this.clientrectangle.height/4;
if(xiacen)
{
rect.y=rect.y+4;
}
graphicspath path;

for(int a=1;a<33;a++)
{
float bf3=bf1*a;
float bf4=bf2*a;
rectangle rect1=rect;
rect1.inflate(-(int)bf3,-(int)bf4);
path=getgraphicspath(rect1);

int r=backcolor.r;
int g=backcolor.g;
int b=backcolor.b;
r=r+3*a;
g=g+3*a;
b=b+3*a;
if(r>255) r=255;
if(g>255) g=255;
if(b>255) b=255;
gr.fillpath(new solidbrush(color.fromargb(r,g,b)),path);
}
}

private void drawgaoliang(graphics g,bool xiacen)
{
rectangle rect= this.clientrectangle;
rect.inflate(-4,-4);

if(xiacen)
{
rect.y=rect.y+4;
}
graphicspath path=getgraphicspath1(rect);
rectanglef rect1=path.getbounds();
rect1.height=rect1.height+1;
g.fillpath(new lineargradientbrush(rect1,
color.fromargb(0xff,0xff,0xff,0xff),
color.fromargb(0xff,backcolor),lineargradientmode.vertical),path);
}

private void drawtext(graphics g,bool xiacen)
{
rectangle rect= this.clientrectangle;
rectangle rect1= this.clientrectangle;
stringformat stringformat=new stringformat();
stringformat.alignment=stringalignment.center;
stringformat.linealignment=stringalignment.center;
rect.y=this.clientrectangle.height/5;
if(xiacen)
{
rect.y=rect.y+4;
rect1.y=rect1.y+4;
}

font font=this.font;


if(mousemove)
{
font=new font(this.font,fontstyle.underline);
}

g.drawstring(this.text,font,
new solidbrush(color.fromargb(0x66,backcolor)),rect,stringformat);
g.drawstring(this.text,font,new solidbrush(this.forecolor),rect1,stringformat);
}

private void usercontrol1_mousedown(object sender, system.windows.forms.mouseeventargs e)
{
if(xiacen==false)
{
xiacen=true;
this.refresh();
}
}

private void usercontrol1_mouseup(object sender, system.windows.forms.mouseeventargs e)
{
if(xiacen==true)
{
xiacen=false;
this.refresh();
}
}

private void usercontrol1_paint(object sender, system.windows.forms.painteventargs e)
{


}

protected override void onpaint(painteventargs e)
{

base.onpaint(e);
e.graphics.fillrectangle(new solidbrush(backcolor),0,0,this.width,this.height);
e.graphics.smoothingmode=smoothingmode.highquality;
e.graphics.textrenderinghint=textrenderinghint.antialiasgridfit;
rectangle rect=new rectangle(0,0,this.width,this.height);
graphicspath clientpath=getgraphicspath(rect);
e.graphics.fillpath(new solidbrush(backcolor),clientpath);
this.region=new system.drawing.region(clientpath);
drawyinying(e.graphics,xiacen);
drawgaoliang(e.graphics,xiacen);
drawtext(e.graphics,xiacen);

if(this.focused)
{
e.graphics.drawpath(new pen(color.fromargb(0x22,0xff,0xff,0xff), 3), clientpath);
}

}





private void usercontrol1_backcolorchanged(object sender, system.eventargs e)
{
int r=backcolor.r;
int g=backcolor.g;
int b=backcolor.b;
r=r+0x22;
g=g+0x22;
b=b+0x22;
if(r>255) r=255;
if(g>255) g=255;
if(b>255) b=255;
backcolor=color.fromargb(r,g,b);
}

private void usercontrol1_keydown(object sender, system.windows.forms.keyeventargs e)
{
if(xiacen==false && e.keycode==keys.space)
{
xiacen=true;
this.refresh();
}
}

private void usercontrol1_keyup(object sender, system.windows.forms.keyeventargs e)
{
if(xiacen==true && e.keycode==keys.space)
{
xiacen=false;
this.refresh();
}
}

private void usercontrol1_mouseenter(object sender, system.eventargs e)
{
if(mousemove==false)
{
mousemove=true;
this.refresh();
}
}

private void usercontrol1_mouseleave(object sender, system.eventargs e)
{
if(mousemove==true)
{
mousemove=false;
this.refresh();
}
}

}
}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表