html5中文学习网

您的位置: 首页 > 网络编程 > ASP.NET » 正文

C# winform 逼真的仿Vista效果的按钮控件button_.NET教程_编程技术

[ ] 已经帮助:人解决问题
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.ComponentModel; 
  4. using System.Drawing; 
  5. using System.Drawing.Imaging; 
  6. using System.Drawing.Drawing2D; 
  7. using System.Data; 
  8. using System.Text; 
  9. using System.Windows.Forms; 
  10.  
  11. namespace MyControls 
  12.     public partial class CrystalButton : Button    
  13.     { 
  14.         private enum MouseActionType 
  15.         { 
  16.             None, 
  17.             Hover, 
  18.             Click 
  19.         } 
  20.  
  21.         private MouseActionType mouseAction; 
  22.         private ImageAttributes imgAttr = new ImageAttributes(); 
  23.         private Bitmap buttonBitmap; 
  24.         private Rectangle buttonBitmapRectangle; 
  25.  
  26.         public CrystalButton() 
  27.         { 
  28.             InitializeComponent(); 
  29.  
  30.             mouseAction = MouseActionType.None; 
  31.  
  32.             this.SetStyle(ControlStyles.AllPaintingInWmPaint  
  33.                 ControlStyles.DoubleBuffer  
  34.                 ControlStyles.UserPaint, true); 
  35.  
  36.             //The following defaults are better suited to draw the text outline 
  37.             this.Font = new Font("Arial Black", 12, FontStyle.Bold); 
  38.             this.BackColor = Color.DarkTurquoise; 
  39.             this.Size = new Size(112, 48); 
  40.         } 
  41.  
  42.         private GraphicsPath GetGraphicsPath(Rectangle rc, int r) 
  43.         { 
  44.             int x = rc.X, y = rc.Y, w = rc.Width, h = rc.Height; 
  45.             GraphicsPath path = new GraphicsPath(); 
  46.             path.AddArc(x, y, r, r, 180, 90);               //Upper left corner 
  47.             path.AddArc(x + w - r, y, r, r, 270, 90);           //Upper right corner 
  48.             path.AddArc(x + w - r, y + h - r, r, r, 0, 90);     //Lower right corner 
  49.             path.AddArc(x, y + h - r, r, r, 90, 90);            //Lower left corner 
  50.             path.CloseFigure(); 
  51.             return path; 
  52.         } 
  53.  
  54.         protected override void OnPaint(PaintEventArgs e) 
  55.         { 
  56.             Graphics g = e.Graphics; 
  57.             //g.Clear(Color.White); 
  58.             g.Clear(SystemColors.ButtonFace ); 
  59.             Color clr = this.BackColor; 
  60.             int shadowOffset = 8; 
  61.             int btnOffset = 0; 
  62.             switch (mouseAction) 
  63.             { 
  64.                 case MouseActionType.Click: 
  65.                     shadowOffset = 4; 
  66.                     clr = Color.LightGray; 
  67.                     btnOffset = 2; 
  68.                     break
  69.                 case MouseActionType.Hover: 
  70.                     clr = Color.LightGray; 
  71.                     break
  72.             } 
  73.             g.SmoothingMode = SmoothingMode.AntiAlias; 
  74.  
  75.             /// 
  76.             /// 创建按钮本身的图形 
  77.             ///  
  78.             Rectangle rc = new Rectangle(btnOffset, btnOffset, this.ClientSize.Width - 8 - btnOffset, this.ClientSize.Height - 8 - btnOffset); 
  79.             GraphicsPath path1 = this.GetGraphicsPath(rc, 20); 
  80.             LinearGradientBrush br1 = new LinearGradientBrush(new Point(0, 0), new Point(0, rc.Height + 6), clr, Color.White); 
  81.  
  82.             /// 
  83.             /// 创建按钮阴影 
  84.             ///  
  85.             Rectangle rc2 = rc; 
  86.             rc2.Offset(shadowOffset, shadowOffset); 
  87.             GraphicsPath path2 = this.GetGraphicsPath(rc2, 20); 
  88.             PathGradientBrush br2 = new PathGradientBrush(path2); 
  89.             br2.CenterColor = Color.Black; 
  90.             br2.SurroundColors = new Color[] {SystemColors.ButtonFace}; 
  91.             //为了更逼真,我们将渐变结束颜色设定为窗体前景颜色,可以根据窗口的前景颜色适当调整 
  92.             
  93.             /// 
  94.             /// 创建按钮顶部白色渐变 
  95.             ///  
  96.             Rectangle rc3 = rc; 
  97.             rc3.Inflate(-5, -5); 
  98.             rc3.Height = 15; 
  99.             GraphicsPath path3 = GetGraphicsPath(rc3, 20); 
  100.             LinearGradientBrush br3 = new LinearGradientBrush(rc3, Color.FromArgb(255, Color.White), Color.FromArgb(0, Color.White), LinearGradientMode.Vertical); 
  101.  
  102.             /// 
  103.             /// 绘制图形 
  104.             /// 
  105.             g.FillPath(br2, path2); //绘制阴影 
  106.             g.FillPath(br1, path1); //绘制按钮 
  107.             g.FillPath(br3, path3); //绘制顶部白色泡泡 
  108.  
  109.             /// 
  110.             ///设定内存位图对象,进行二级缓存绘图操作 
  111.             /// 
  112.             buttonBitmapRectangle = new Rectangle(rc.Location, rc.Size); 
  113.             buttonBitmap = new Bitmap(buttonBitmapRectangle.Width, buttonBitmapRectangle.Height); 
  114.             Graphics g_bmp = Graphics.FromImage(buttonBitmap); 
  115.             g_bmp.SmoothingMode = SmoothingMode.AntiAlias; 
  116.             g_bmp.FillPath(br1, path1); 
  117.             g_bmp.FillPath(br3, path3); 
  118.  
  119.             /// 
  120.             ///将region赋值给button 
  121.             Region rgn = new Region(path1); 
  122.             rgn.Union(path2); 
  123.             this.Region = rgn; 
  124.  
  125.             /// 
  126.             /// 绘制按钮的文本 
  127.             ///  
  128.             GraphicsPath path4 = new GraphicsPath(); 
  129.  
  130.             RectangleF path1bounds = path1.GetBounds(); 
  131.             
  132.             Rectangle rcText = new Rectangle((int)path1bounds.X + btnOffset, (int)path1bounds.Y + btnOffset, (int)path1bounds.Width, (int)path1bounds.Height); 
  133.  
  134.             StringFormat strformat = new StringFormat(); 
  135.             strformat.Alignment = StringAlignment.Center; 
  136.             strformat.LineAlignment = StringAlignment.Center; 
  137.             path4.AddString(this.Text, this.Font.FontFamily, (int)this.Font.Style, this.Font.Size, rcText, strformat); 
  138.  
  139.             Pen txtPen = new Pen(this.ForeColor , 1); 
  140.             g.DrawPath(txtPen, path4); 
  141.             g_bmp.DrawPath(txtPen, path4); 
  142.         } 
  143.  
  144.         protected override void OnMouseDown(MouseEventArgs e) 
  145.         { 
  146.             if (e.Button == MouseButtons.Left) 
  147.             { 
  148.                 this.mouseAction = MouseActionType.Click; 
  149.                 this.Invalidate(); 
  150.             } 
  151.             base.OnMouseDown(e); 
  152.         } 
  153.  
  154.         protected override void OnMouseUp(MouseEventArgs e) 
  155.         { 
  156.             this.mouseAction = MouseActionType.Hover; 
  157.             this.Invalidate(); 
  158.             base.OnMouseUp(e); 
  159.         } 
  160.  
  161.         protected override void OnMouseHover(EventArgs e) 
  162.         { 
  163.             this.mouseAction = MouseActionType.Hover; 
  164.             this.Invalidate(); 
  165.             base.OnMouseHover(e); 
  166.         } 
  167.  
  168.         protected override void OnMouseEnter(EventArgs e) 
  169.         { 
  170.             this.mouseAction = MouseActionType.Hover; 
  171.             this.Invalidate(); 
  172.             base.OnMouseEnter(e); 
  173.         } 
  174.  
  175.         protected override void OnMouseLeave(EventArgs e) 
  176.         { 
  177.             this.mouseAction = MouseActionType.None; 
  178.             this.Invalidate(); 
  179.             base.OnMouseLeave(e); 
  180.         } 
  181.     } 
xC8HTML5中文学习网 - HTML5先行者学习网
xC8HTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助