【Asp.net精品源码栏目提醒】:网学会员--在 Asp.net精品源码编辑为广大网友搜集整理了:C#自定义控件三Vista按钮 - 其它资料绩等信息,祝愿广大网友取得需要的信息,参考学习。
C自定义控件三 Vista 按钮效果图:Vista 和 Win7 操作系统的按钮非常华丽,用 CGDI去实现也挺方便的,这个是参考网上
源码修改过来的,个人觉得非常有学习价值。
第一步:添加用户控件,命名为 myVistaButton第二步:确定需要的属性,例如文字、背景颜色、图片等。
第三步:重写 Paint 事件,为了增强效果,还可以重写鼠标移入移出等事件。
贴出核心代码:region 方法 private GraphicsPath RoundRectRectangleF r float r1 float r2 float r3float r4 float x r.X y r.Y w r.Width h r.Height GraphicsPath rr new GraphicsPath rr.AddBezierx y r1 x y x r1 y x r1 y rr.AddLinex r1 y x w - r2 y rr.AddBezierx w - r2 y x w y x w y r2 x w y r2 rr.AddLinex w y r2 x w y h - r3 rr.AddBezierx w y h - r3 x w y h x w - r3 y h x w -r3 y h rr.AddLinex w - r3 y h x r4 y h rr.AddBezierx r4 y h x y h x y h - r4 x y h - r4 rr.AddLinex y h - r4 x y r1 return rr private StringFormat StringFormatAlignmentContentAlignment textalign StringFormat sf new StringFormat switch textalign case ContentAlignment.TopLeft: case ContentAlignment.TopCenter: case ContentAlignment.TopRight: sf.LineAlignment StringAlignment.Near break case ContentAlignment.MiddleLeft: case ContentAlignment.MiddleCenter: case ContentAlignment.MiddleRight: sf.LineAlignment StringAlignment.Center break case ContentAlignment.BottomLeft: case ContentAlignment.BottomCenter: case ContentAlignment.BottomRight: sf.LineAlignment StringAlignment.Far break switch textalign case ContentAlignment.TopLeft: case ContentAlignment.MiddleLeft: case ContentAlignment.BottomLeft: sf.Alignment StringAlignment.Near break case ContentAlignment.TopCenter: case ContentAlignment.MiddleCenter: case ContentAlignment.BottomCenter: sf.Alignment StringAlignment.Center break case ContentAlignment.TopRight: case ContentAlignment.MiddleRight: case ContentAlignment.BottomRight: sf.Alignment StringAlignment.Far break return sf /// /// 绘制外部边框 /// /// private void DrawOuterStrokeGraphics g if this.ButtonStyle Style.Flat this.mButtonState State.None return Rectangle r this.ClientRectangle r.Width - 1 r.Height - 1 using GraphicsPath rr RoundRectr CornerRadius CornerRadiusCornerRadius CornerRadius using Pen p new Penthis.ButtonColor g.DrawPathp rr /// /// 绘制内部边框 /// /// private void DrawInnerStrokeGraphics g if this.ButtonStyle Style.Flat this.mButtonState State.None return Rectangle r this.ClientRectangle r.X r.Y r.Width - 3 r.Height - 3 using GraphicsPath rr RoundRectr CornerRadius CornerRadiusCornerRadius CornerRadius using Pen p new Penthis.HighlightColor g.DrawPathp rr /// /// 绘制背景 /// /// private void DrawBackgroundGraphics g if this.ButtonStyle Style.Flat this.mButtonState State.None return int alpha mButtonState State.Pressed 204 : 127 Rectangle r this.ClientRectangle r.Width-- r.Height-- using GraphicsPath rr RoundRectr CornerRadius CornerRadiusCornerRadius CornerRadius using SolidBrush sb new SolidBrushthis.BaseColor g.FillPathsb rr SetClipg if this.BackImage null g.DrawImagethis.BackImagethis.ClientRectangle g.ResetClip using SolidBrush sb new SolidBrushColor.FromArgbalphathis.ButtonColor g.FillPathsb rr /// /// 绘制高亮颜色 /// /// private void DrawHighlightGraphics g if this.ButtonStyle Style.Flat this.mButtonState State.None return int alpha mButtonState State.Pressed 60 : 150 Rectangle rect new Rectangle0 0 this.Width this.Height / 2 using GraphicsPath r RoundRectrect CornerRadius CornerRadius0 0 using LinearGradientBrush lg newLinearGradientBrushr.GetBounds Color.FromArgbalphathis.HighlightColor Color.FromArgbalpha / 3this.HighlightColor LinearGradientMode.Vertical g.FillPathlg r /// /// 绘制鼠标移入颜色 /// /// private void DrawGlowGraphics g if this.mButtonState State.Pressed return SetClipg using GraphicsPath glow new GraphicsPath glow.AddEllipse-5 this.Height / 2 - 10 this.Width 11this.Height 11 using PathGradientBrush gl new PathGradientBrushglow gl.CenterColor Color.FromArgbmGlowAlphathis.GlowColor gl.SurroundColors new Color Color.FromArgb0this.GlowColor g.FillPathgl glow g.ResetClip /// /// 绘制文本 /// /// private void DrawTextGraphics g StringFormat sf StringFormatAlignmentthis.TextAlign Rectangle r new Rectangle8 8 this.Width - 17 this.Height - 17 g.DrawStringthis.ButtonText this.Font newSolidBrushthis.ForeColor r sf /// /// 绘制图片 /// /// private void DrawImageGraphics g if this.Image null return Rectangle r new Rectangle8 8 this.ImageSize.Widththis.ImageSize.Height switch this.ImageAlign case ContentAlignment.TopCenter: r new Rectanglethis.Width / 2 - this.ImageSize.Width / 28 this.ImageSize.Width this.ImageSize.Height break case ContentAlignment.TopRight: r new Rectanglethis.Width - 8 - this.ImageSize.Width 8this.ImageSize.Width this.ImageSize.Height break case ContentAlignment.MiddleLeft: r new Rectangle8 this.Height / 2 - this.ImageSize.Height /2 this.ImageSize.Width this.ImageSize.Height break case ContentAlignment.MiddleCenter: r new Rectanglethis.Width / 2 - this.ImageSize.Width / 2this.Height / 2 - this.ImageSize.Height / 2 this.ImageSize.Widththis.ImageSize.Height break case ContentAlignment.MiddleRight: r new Rectanglethis.Width - 8 - this.ImageSize.Widththis.Height / 2 - this.ImageSize.Height / 2 this.ImageSize.Widththis.ImageSize.Height break case ContentAlignment.BottomLeft: r new Rectangle8 this.Height - 8 - this.ImageSize.Heightthis.ImageSize.Width this.ImageSize.Height break case ContentAlignment.BottomCenter: r new Rectanglethis.Width / 2 - this.ImageSize.Width / 2this.Height - 8 - this.ImageSize.Height this.ImageSize.Width this.ImageSize.Height break case ContentAlignment.BottomRight: r new Rectanglethis.Width - 8 - this.ImageSize.Widththis.Height - 8 - this.ImageSize.Height this.ImageSize.Width this.ImageSize.Height break g.DrawImagethis.Image r private void SetClipGraphics g Rectangle r this.ClientRectangle r.X r.Y r.Width - 3 r.Height - 3 using GraphicsPath rr RoundRectr CornerRadius CornerRadiusCornerRadius CornerRadius g.SetCliprr endregion重写 Paint 事件:private void myVistaButton_Paintobject sender PaintEventArgs e e.Graphics.SmoothingMode SmoothingMode.AntiAlias e.Graphics.InterpolationMode InterpolationMode.HighQualityBicubic DrawBackgrounde.Graphics DrawHighlighte.Graphics DrawImagee.Graphics DrawTexte.Graphics DrawGlowe.Graphics DrawOuterStrokee.Graphics DrawInnerStrokee.Graphics 点击这里下载源代码
上一篇:
ASP动态网页制作
下一篇:
爱情只不过就是,爱与不爱