贡献自绘CheckBox代码

   1: // 功能:复选框
   2: // 描述:
   3: // 编码:温伟鹏
   4: // 日期:12/18/2008 11:48:34
   5:  
   6: using System;
   7: using System.Collections.Generic;
   8: using System.Text;
   9: using System.ComponentModel;
  10: using System.Drawing;
  11: using System.Windows.Forms;
  12: using System.Drawing.Drawing2D;
  13: using Esint.UI.Common;
  14:  
  15: namespace Esint.UI.WinFormUI
  16: {
  17:     [ToolboxItem(true)]
  18:     public class CheckBox:ControlBase
  19:     {
  20:         /// <summary>
  21:         /// Checked属性修改事件
  22:         /// </summary>
  23:         public event EventHandler CheckedChanged;
  24:  
  25:         private bool isChecked;
  26:         /// <summary>
  27:         /// 获取或设置控件是否选中
  28:         /// </summary>
  29:         [Category("Appearance"), DefaultValue(false), Description("获取或设置控件是否选中")]
  30:         public bool Checked
  31:         {
  32:             get {
  33:                 return this.isChecked;
  34:             }
  35:             set {
  36:                 isChecked = value;
  37:                 
  38:                 this.Refresh();
  39:  
  40:                 if (CheckedChanged != null)
  41:                 {
  42:                     CheckedChanged(this, null);
  43:                 }
  44:             }
  45:         }
  46:  
  47:         public CheckBox()
  48:             :base()
  49:         {
  50:         }
  51:  
  52:         protected override void InitializeBorder()
  53:         {
  54:             base.InitializeBorder();
  55:  
  56:             this.Border.BorderColor = Color.Yellow;
  57:             this.Border.BorderWidth = 2f;
  58:             this.Border.CornerRound = 4;
  59:         }
  60:  
  61:         protected override void InitializeStyles()
  62:         {
  63:             this.SetStyle(ControlStyles.Selectable, true);
  64:  
  65:             base.InitializeStyles();
  66:         }
  67:  
  68:         protected override void InitializeSelf()
  69:         {
  70:             base.InitializeSelf();
  71:  
  72:             this.Name = "CheckBox";
  73:             this.MinimumSize = new Size(24, 24);
  74:             this.Text = "CheckBox";
  75:  
  76:             Graphics g = Graphics.FromHwnd(Handle);
  77:             Size txtSize = g.MeasureString(Text, Font).ToSize();
  78:  
  79:             this.MinimumSize = new Size(txtSize.Width + 28, 24);
  80:  
  81:             g.Dispose();
  82:  
  83:             this.Size = this.MinimumSize;
  84:         }
  85:  
  86:         protected override void OnMouseClick(System.Windows.Forms.MouseEventArgs e)
  87:         {
  88:             if (e.Button == MouseButtons.Left)
  89:             {
  90:                 //this.isFocused = true;
  91:  
  92:                 Checked = !Checked;
  93:             }
  94:  
  95:             base.OnMouseClick(e);
  96:         }
  97:  
  98:         protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
  99:         {
 100:             //base.OnPaintBackground(e);
 101:  
 102:             if (!Visible)
 103:             {
 104:                 return;
 105:             }
 106:  
 107:             Graphics g = e.Graphics;
 108:             g.SmoothingMode = SmoothingMode.AntiAlias;
 109:  
 110:             g.Clear(BackColor);
 111:         }
 112:  
 113:         protected override void OnPaintBorder(System.Windows.Forms.PaintEventArgs e)
 114:         {
 115:             //base.OnPaintBorder(e);
 116:  
 117:             if (!Visible)
 118:             {
 119:                 return;
 120:             }
 121:  
 122:             Graphics g = e.Graphics;
 123:             g.SmoothingMode = SmoothingMode.AntiAlias;
 124:  
 125:             Rectangle radioRect = new Rectangle(
 126:                 2, (int)((Height - 18 + Border.BorderWidth) / 2),
 127:                 (int)(18 - Border.BorderWidth), (int)(18 - Border.BorderWidth));
 128:             GraphicsPath radioPath = DrawHelper.CreateRoundRectangle(radioRect, Border.CornerRound);
 129:  
 130:             Pen borderPen = new Pen(new SolidBrush(Border.BorderColor),
 131:                 Border.BorderWidth);
 132:  
 133:             g.DrawPath(borderPen, radioPath);
 134:  
 135:             if (radioPath != null) { radioPath.Dispose(); }
 136:         }
 137:  
 138:         protected override void OnPaintContent(System.Windows.Forms.PaintEventArgs e)
 139:         {
 140:             //base.OnPaintContent(e);
 141:  
 142:             if (!Visible)
 143:             {
 144:                 return;
 145:             }
 146:  
 147:             Graphics g = e.Graphics;
 148:             g.SmoothingMode = SmoothingMode.AntiAlias;
 149:  
 150:             Size txtSize = g.MeasureString(Text, Font).ToSize();
 151:  
 152:             PointF txtLocation = new PointF(24, (Height - txtSize.Height) / 2);
 153:             //是否可用
 154:             if (Enabled)
 155:             {
 156:                 //检测Text属性是否为空
 157:                 if (!Utils.IsNullorEmpty(Text))
 158:                 {
 159:                     g.DrawString(Text, Font, new SolidBrush(ForeColor), txtLocation);
 160:                     //焦点
 161:                     if (Focused)
 162:                     {
 163:                         Point txtLocal = new Point(24, (int)((Height - txtSize.Height) / 2));
 164:                         Rectangle txtRect = new Rectangle(txtLocal, txtSize);
 165:                         txtRect.Width -= 2;
 166:                         ControlPaint.DrawFocusRectangle(g, txtRect);
 167:                     }
 168:                 }
 169:                 //是否选中
 170:                 if (Checked)
 171:                 {
 172:                     GraphicsPath checkedPath = new GraphicsPath(FillMode.Winding);
 173:                     checkedPath.AddLine(new Point(2, Height / 2), new Point(10, Height / 2 + 8));
 174:                     checkedPath.AddLine(new Point(19, (Height - 18) / 2), new Point(18, (Height - 18) / 2));
 175:                     checkedPath.AddLine(new Point(18, (Height - 18) / 2), new Point(9, Height / 2 + 2));
 176:                     checkedPath.AddLine(new Point(9, Height / 2 + 2), new Point(2, Height / 2));
 177:                     checkedPath.CloseFigure();
 178:  
 179:                     g.FillPath(new SolidBrush(Border.BorderColor), checkedPath);
 180:  
 181:                     if (checkedPath != null) { checkedPath.Dispose(); }
 182:                 }
 183:             }
 184:             else
 185:             {
 186:                 //检测Text属性是否为空
 187:                 if (!Utils.IsNullorEmpty(Text))
 188:                 {
 189:                     txtLocation.X++;
 190:                     txtLocation.Y++;
 191:                     g.DrawString(Text, Font, new SolidBrush(Color.White), txtLocation);
 192:                     txtLocation.X--;
 193:                     txtLocation.Y--;
 194:                     g.DrawString(Text, Font, new SolidBrush(Color.FromArgb(128, 128, 128)), txtLocation);
 195:                 }
 196:             }
 197:         }
 198:     }
 199: }
   1: /// <summary>
   2: /// 判断给定的字符串是否为空
   3: /// </summary>
   4: /// <param name="text"></param>
   5: /// <returns></returns>
   6: public static bool IsNullorEmpty(string text)
   7: {
   8:     if (text == null || text.Trim() == string.Empty)
   9:         return true;
  10:     else
  11:         return false;
  12: }
posted @ 2009-01-02 00:43  温伟鹏  阅读(2700)  评论(7编辑  收藏  举报