发 帖  
原厂入驻New

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

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

举报

只有小组成员才能发言,加入小组>>

141个成员聚集在这个小组

加入小组

创建小组步骤

快速回复 返回顶部 返回列表