<font face="Verdana">C#中水晶按钮的程序生成</font><br/>
<p><font face="Verdana">C#中水晶按钮的程序生成<br/>版权所有 codesky.net 2003-2005<br/>发表时间:2005-1-26 关键字:未知</font></p>
<p><font face="Verdana">做了一个控件,这样就可以告别图片按钮了:)</font></p>
<p><font face="Verdana">程序源代码:</font></p>
<p><font face="Verdana"></font> </p>
<p><font face="Verdana">using System;<br/>using System.Collec
tions;<br/>using System.ComponentModel;<br/>using System.Drawing;<br/>using System.Data;<br/>using System.Windows.Forms;<br/>using System.Drawing.Drawing2D;<br/>using System.Drawing.Text;</font></p>
<p><font face="Verdana">namespace WindowsControlLibrary1<br/>{<br/>/// <summary><br/>/// UserControl1 的摘要说明。<br/>/// </summary><br/>public class UserControl1 : Button<br/>{<br/> /// <summary><br/> /// 必需的设计器变量。<br/> /// </summary><br/> private System.ComponentModel.Container components = null;<br/> private bool XiaCen=false;<br/> private bool mouseMove=false;<br/> private Color backColor;<br/> public UserControl1()<br/> {<br/> // 该调用是 Windows.Forms 窗体设计器所必需的。<br/> InitializeComponent();</font></p>
<p><font face="Verdana"> // TODO: 在 InitComponent 调用后添加任何初始化<br/> backColor=this.backColor;<br/> //this.Text=this.ShowFocusCues.ToString();<br/> }</font></p>
<p><font face="Verdana"> /// <summary><br/> /// 清理所有正在使用的资源。<br/> /// </summary><br/> protected override void Dispose( bool disposing )<br/> {<br/>
IF( disposing )<br/> {<br/> if( components != null )<br/> components.Dispose();<br/> }<br/> base.Dispose( disposing );<br/> }</font></p>
<p><font face="Verdana"> #region 组件设计器生成的代码<br/> /// <summary><br/> /// 设计器支持所需的方法 - 不要使用代码编辑器 <br/> /// 修改此方法的内容。<br/> /// </summary><br/> private void InitializeComponent()<br/> {<br/> // <br/> // UserControl1<br/> // <br/> this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.UserControl1_MouseUp);<br/> this.Paint += new System.Windows.Forms.PaintEventHandler(this.UserControl1_Paint);<br/> this.MouseEnter += new System.EventHandler(this.UserControl1_MouseEnter);<br/> this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.UserControl1_KeyUp);<br/> this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.UserControl1_KeyDown);<br/> this.BackColorChanged += new System.EventHandler(this.UserControl1_BackColorChanged);<br/> this.MouseLeave += new System.EventHandler(this.UserControl1_MouseLeave);<br/> this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.UserControl1_MouseDown);</font></p>
<p><font face="Verdana"> }<br/> #endregion</font></p>
<p><font face="Verdana"> <br/> <br/> protected GraphicsPath GetGraphicsPath(Rectangle rect)<br/> {<br/> GraphicsPath ClientPath = new System.Drawing.Drawing2D.GraphicsPath();<br/> if(rect.Width<=0)<br/> {<br/> rect.Width=1;<br/> }<br/> if(rect.Height<=0)<br/> {<br/> rect.Height=1;<br/> }<br/> <br/> ClientPath.AddArc(rect.Left,rect.Top,rect.Height,rect.Height,90f,180f);<br/> ClientPath.AddArc(rect.Right-rect.Height,rect.Top,rect.Height,rect.Height,270f,180f);<br/> ClientPath.CloseFigure();<br/> return ClientPath;<br/> }<br/> <br/> protected GraphicsPath GetGraphicsPath1(Rectangle rect)<br/> {<br/> GraphicsPath ClientPath = new System.Drawing.Drawing2D.GraphicsPath();<br/> if(rect.Width<=0)<br/> {<br/> rect.Width=1;<br/> }<br/> if(rect.Height<=0)<br/> {<br/> rect.Height=1;<br/> }<br/> <br/> ClientPath.AddArc(rect.Left,rect.Top,rect.Height,rect.Height,190f,80f);<br/> ClientPath.AddArc(rect.Right-rect.Height,rect.Top,rect.Height,rect.Height,270f,80f);<br/> ClientPath.CloseFigure();<br/> return ClientPath;<br/> }<br/> <br/> private void DrawYinYing(Graphics gr,bool xiacen)<br/> {<br/> Rectangle rect= this.ClientRectangle;<br/> rect.Inflate(-(rect.Width/10),-(rect.Height)/4);<br/> float bf1=rect.Width/100f;<br/> float bf2=rect.Height/100f;<br/> <br/> rect.Y=rect.Y+this.ClientRectangle.Height/4;<br/> if(xiacen)<br/> {<br/> rect.Y=rect.Y+4;<br/> }<br/> GraphicsPath path;<br/> <br/> for(int a=1;a<33;a++)<br/> {<br/> float bf3=bf1*a;<br/> float bf4=bf2*a;<br/> Rectangle rect1=rect;<br/> rect1.Inflate(-(int)bf3,-(int)bf4);<br/> path=GetGraphicsPath(rect1);<br/> <br/> int r=backColor.R;<br/> int g=backColor.G;<br/> int b=backColor.B;<br/> r=r+3*a;<br/> g=g+3*a;<br/> b=b+3*a;<br/> if(r>255) r=255;<br/> if(g>255) g=255;<br/> if(b>255) b=255;<br/> gr.FillPath(new SolidBrush(Color.FromArgb(r,g,b)),path);<br/> }<br/> }<br/> <br/> private void DrawGaoLiang(Graphics g,bool xiacen)<br/> {<br/> Rectangle rect= this.ClientRectangle;<br/> rect.Inflate(-4,-4);<br/> <br/> if(xiacen)<br/> {<br/> rect.Y=rect.Y+4;<br/> }<br/> GraphicsPath path=GetGraphicsPath1(rect);<br/> RectangleF rect1=path.GetBounds();<br/> rect1.Height=rect1.Height+1;<br/> g.FillPath(new
linearGradientBrush(rect1,<br/> Color.FromArgb(0xff,0xff,0xff,0xff),<br/> Color.FromArgb(0xff,backColor),LinearGradientMode.Vertical),path);<br/> }<br/> <br/> private void DrawText(Graphics g,bool xiacen)<br/> {<br/> Rectangle rect= this.ClientRectangle;<br/> Rectangle rect1= this.ClientRectangle;<br/> StringFormat stringFormat=new StringFormat();<br/> stringFormat.Alignment=StringAlignment.Center;<br/> stringFormat.LineAlignment=StringAlignment.Center;<br/> rect.Y=this.ClientRectangle.Height/5;<br/> if(xiacen)<br/> {<br/> rect.Y=rect.Y+4;<br/> rect1.Y=rect1.Y+4;<br/> }<br/> <br/> Font font=this.Font; <br/> <br/> <br/> if(mouseMove)<br/> {<br/> font=new Font(this.Font,FontStyle.Underline); <br/> }<br/> <br/> g.DrawString(this.Text,font,<br/> new SolidBrush(Color.FromArgb(0x66,backColor)),rect,stringFormat);<br/> g.DrawString(this.Text,font,new SolidBrush(this.ForeColor),rect1,stringFormat);<br/> }</font></p>
<p><font face="Verdana"> private void UserControl1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)<br/> {<br/> if(XiaCen==false)<br/> {<br/> XiaCen=true;<br/> this.Refresh();<br/> }<br/> }<br/> <br/> private void UserControl1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)<br/> {<br/> if(XiaCen==true)<br/> {<br/> XiaCen=false;<br/> this.Refresh();<br/> }<br/> }</font></p>
<p><font face="Verdana"> private void UserControl1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)<br/> {<br/> <br/> <br/> }<br/> <br/> protected override void OnPaint(PaintEventArgs e)<br/> {<br/> <br/> base.OnPaint(e);<br/> e.Graphics.FillRectangle(new SolidBrush(backColor),0,0,this.Width,this.Height);<br/> e.Graphics.SmoothingMode=SmoothingMode.HighQuality;<br/> e.Graphics.TextRenderingHint=TextRenderingHint.AntiAliasGridFit;<br/> Rectangle rect=new Rectangle(0,0,this.Width,this.Height);<br/> GraphicsPath ClientPath=GetGraphicsPath(rect);<br/> e.Graphics.FillPath(new SolidBrush(backColor),ClientPath);<br/> this.Region=new System.Drawing.Region(ClientPath);<br/> DrawYinYing(e.Graphics,XiaCen);<br/> DrawGaoLiang(e.Graphics,XiaCen);<br/> DrawText(e.Graphics,XiaCen);<br/> <br/> if(this.Focused)<br/> {<br/> e.Graphics.DrawPath(new Pen(Color.FromArgb(0x22,0xff,0xff,0xff), 3), ClientPath); <br/> }<br/> <br/> }</font></p>
<p><font face="Verdana"> </font></p>
<p><font face="Verdana"></font> </p>
<p><font face="Verdana"> private void UserControl1_BackColorChanged(object sender, System.EventArgs e)<br/> {<br/> int r=BackColor.R;<br/> int g=BackColor.G;<br/> int b=BackColor.B;<br/> r=r+0x22;<br/> g=g+0x22;<br/> b=b+0x22;<br/> if(r>255) r=255;<br/> if(g>255) g=255;<br/> if(b>255) b=255;<br/> backColor=Color.FromArgb(r,g,b);<br/> }</font></p>
<p><font face="Verdana"> private void UserControl1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)<br/> {<br/> if(XiaCen==false && e.KeyCode==Keys.Space)<br/> {<br/> XiaCen=true;<br/> this.Refresh();<br/> }<br/> }</font></p>
<p><font face="Verdana"> private void UserControl1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)<br/> {<br/> if(XiaCen==true && e.KeyCode==Keys.Space)<br/> {<br/> XiaCen=false;<br/> this.Refresh();<br/> }<br/> }</font></p>
<p><font face="Verdana"> private void UserControl1_MouseEnter(object sender, System.EventArgs e)<br/> {<br/> if(mouseMove==false)<br/> {<br/> mouseMove=true;<br/> this.Refresh();<br/> }<br/> }</font></p>
<p><font face="Verdana"> private void UserControl1_MouseLeave(object sender, System.EventArgs e)<br/> {<br/> if(mouseMove==true)<br/> {<br/> mouseMove=false;<br/> this.Refresh();<br/> }<br/> }<br/> <br/>}<br/>}<br/></font></p>
0