html5中文学习网

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

C# winform 自定义颜色显示的RadioButton_.NET教程_编程技术

[ ] 已经帮助:人解决问题
  1. using System; 
  2. using System.Windows.Forms; 
  3. using System.Drawing; 
  4. using System.ComponentModel; 
  5.  
  6. namespace WindowsApplication4 
  7.     /// <summary> 
  8.     /// Summary description for CustomColorRadioButton. 
  9.     /// </summary> 
  10.     public class CustomColorRadioButton : RadioButton 
  11.     { 
  12.         // Fields 
  13.         private Color checkColor; 
  14.  
  15.         public CustomColorRadioButton() 
  16.         { 
  17.             this.checkColor = this.ForeColor; 
  18.             this.Paint += new PaintEventHandler(this.PaintHandler); 
  19.         } 
  20.  
  21.         [Description("The color used to display the check painted in the RadioButton")] 
  22.         public Color CheckColor  
  23.         { 
  24.             get  
  25.             { 
  26.                 return checkColor; 
  27.             } 
  28.             set  
  29.             { 
  30.                 checkColor = value; 
  31.                 this.Invalidate(); 
  32.             } 
  33.         } 
  34.  
  35.         private void PaintHandler (object sender, PaintEventArgs pe)  
  36.         { 
  37.             if (this.Checked)  
  38.             { 
  39.                 Point pt = new Point(); 
  40.  
  41.                 if (this.CheckAlign == ContentAlignment.BottomCenter) 
  42.                 { 
  43.                     pt.X = (this.Width / 2) - 3; 
  44.                     pt.Y = this.Height - 9; 
  45.                 } 
  46.                 if (this.CheckAlign == ContentAlignment.BottomLeft) 
  47.                 { 
  48.                     pt.X = 4; 
  49.                     pt.Y = this.Height - 9; 
  50.                 } 
  51.                 if (this.CheckAlign == ContentAlignment.BottomRight) 
  52.                 { 
  53.                     pt.X = this.Width - 9; 
  54.                     pt.Y = this.Height - 9; 
  55.                 } 
  56.                 if (this.CheckAlign == ContentAlignment.MiddleCenter) 
  57.                 { 
  58.                     pt.X = (this.Width / 2) - 3; 
  59.                     pt.Y = (this.Height / 2) - 3; 
  60.                 } 
  61.                 if (this.CheckAlign == ContentAlignment.MiddleLeft) 
  62.                 { 
  63.                     pt.X = 4; 
  64.                     pt.Y = (this.Height / 2) - 3; 
  65.                 } 
  66.                 if (this.CheckAlign == ContentAlignment.MiddleRight) 
  67.                 { 
  68.                     pt.X = this.Width - 9; 
  69.                     pt.Y = (this.Height / 2) - 3; 
  70.                 } 
  71.                 if (this.CheckAlign == ContentAlignment.TopCenter) 
  72.                 { 
  73.                     pt.X = (this.Width / 2) - 3; 
  74.                     pt.Y = 4; 
  75.                 } 
  76.                 if (this.CheckAlign == ContentAlignment.TopLeft) 
  77.                 { 
  78.                     pt.X = 4; 
  79.                     pt.Y = 4; 
  80.                 } 
  81.                 if (this.CheckAlign == ContentAlignment.TopRight) 
  82.                 { 
  83.                     pt.X = this.Width - 9; 
  84.                     pt.Y = 4; 
  85.                 } 
  86.  
  87.                 DrawCheck(pe.Graphics, this.checkColor,pt); 
  88.             } 
  89.         } 
  90.  
  91.         public void DrawCheck(Graphics g, Color c, Point pt)  
  92.         { 
  93.             Pen pen = new Pen(this.checkColor);  
  94.             g.DrawLine(pen, pt.X, pt.Y + 1, pt.X + 3, pt.Y + 1); 
  95.             g.DrawLine(pen, pt.X, pt.Y + 2, pt.X + 3, pt.Y + 2); 
  96.             g.DrawLine(pen, pt.X + 1, pt.Y, pt.X + 1, pt.Y + 3); 
  97.             g.DrawLine(pen, pt.X + 2, pt.Y, pt.X + 2, pt.Y + 3); 
  98.  
  99.         } 
  100.     } 
HK4HTML5中文学习网 - HTML5先行者学习网
HK4HTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助