123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.ComponentModel;
- namespace LYFZ.ComponentLibrary
- {
- public class CheckBoxEx:System.Windows.Forms.CheckBox
- {
- public enum QQControlState
- {
- /// <summary>
- /// 正常状态
- /// </summary>
- Normal = 0,
- /// <summary>
- /// /鼠标进入
- /// </summary>
- Highlight = 1,
- /// <summary>
- /// 鼠标按下
- /// </summary>
- Down = 2,
- /// <summary>
- /// 获得焦点
- /// </summary>
- Focus = 3,
- /// <summary>
- /// 控件禁止
- /// </summary>
- Disabled = 4
- }
- internal class ColorTable
- {
- public static Color QQBorderColor = LYFZ.ComponentLibrary.GetUIResources.CheckBoxExBorderColor;// Color.LightBlue; //LightBlue = Color.FromArgb(173, 216, 230)
- public static Color QQHighLightColor = GetColor(QQBorderColor, 255, -63, -11, 23); //Color.FromArgb(110, 205, 253)
- public static Color QQHighLightInnerColor = GetColor(QQBorderColor, 255, -100, -44, 1); //Color.FromArgb(73, 172, 231);
- }
- /// <summary>
- /// 返回给定的颜色的ARGB的分量差值的颜色
- /// </summary>
- /// <param name="colorBase"></param>
- /// <param name="a">A</param>
- /// <param name="r">R</param>
- /// <param name="g">G</param>
- /// <param name="b">B</param>
- /// <returns></returns>
- public static Color GetColor(Color colorBase, int a, int r, int g, int b)
- {
- int a0 = colorBase.A;
- int r0 = colorBase.R;
- int g0 = colorBase.G;
- int b0 = colorBase.B;
- if (a + a0 > 255) { a = 255; } else { a = Math.Max(a + a0, 0); }
- if (r + r0 > 255) { r = 255; } else { r = Math.Max(r + r0, 0); }
- if (g + g0 > 255) { g = 255; } else { g = Math.Max(g + g0, 0); }
- if (b + b0 > 255) { b = 255; } else { b = Math.Max(b + b0, 0); }
- return Color.FromArgb(a, r, g, b);
- }
- #region Field
-
- private QQControlState _state = QQControlState.Normal;
- private Image _checkImg = new Bitmap(LYFZ.ComponentLibrary.GetUIResources.check);
- private Image _checkImgDel = new Bitmap(LYFZ.ComponentLibrary.GetUIResources.checkDel);
- /// <summary>
- /// 特别状态时的图片
- /// </summary>
- private Image _checkImgSpecially = new Bitmap(LYFZ.ComponentLibrary.GetUIResources.check_Gr);
- private Font _defaultFont = new Font("微软雅黑", 9);
- private static readonly ContentAlignment RightAlignment = ContentAlignment.TopRight | ContentAlignment.BottomRight | ContentAlignment.MiddleRight;
- private static readonly ContentAlignment LeftAlignment = ContentAlignment.TopLeft | ContentAlignment.BottomLeft | ContentAlignment.MiddleLeft;
- /// <summary>
- /// 当前状态图片
- /// </summary>
- private Image currentStateImage = new Bitmap(LYFZ.ComponentLibrary.GetUIResources.check_Gr);
- #endregion
- #region Constructor
- public CheckBoxEx()
- {
- SetStyles();
- this.BackColor = Color.Transparent;
-
- this.ForeColor = LYFZ.ComponentLibrary.GetUIResources.DefaultTextColor;
- _defaultFont = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));
- this.Font = _defaultFont;
- this.CheckStateChanged += CheckBoxEx_CheckStateChanged;
- this.CheckedChanged += CheckBoxEx_CheckedChanged;
- // this.Paint += CheckBoxEx_Paint;
- }
- void CheckBoxEx_Paint(object sender, PaintEventArgs e)
- {
- this.ForeColor = LYFZ.ComponentLibrary.GetUIResources.DefaultTextColor;
- }
- bool isGreenTick = false;
- /// <summary>
- /// 在开启四种状态下 但又关闭ThreeState功能时 设为true时 显示绿是勾选
- /// </summary>
- public bool IsGreenTick
- {
- get { return isGreenTick; }
- set { isGreenTick = value; }
- }
- void CheckBoxEx_CheckedChanged(object sender, EventArgs e)
- {
- if (this.Fourstates)
- {
- if (this.ThreeState)
- {
- if (this.Checked)
- {
- this.CurrentCheckState = CheckStateEx.Checked;
- }
- }
- else {
- if (this.Checked || IsGreenTick)
- {
- this.CurrentCheckState = CheckStateEx.Checked;
- }
- else {
- this.CurrentCheckState = CheckStateEx.Indeterminate;
- }
- }
- }
- }
- void CheckBoxEx_CheckStateChanged(object sender, EventArgs e)
- {
- if (this.Fourstates)
- {
- switch (this.CheckState)
- {
- case System.Windows.Forms.CheckState.Checked:
- this.CurrentCheckState = CheckStateEx.Checked;
- break;
- case System.Windows.Forms.CheckState.Indeterminate:
- this.CurrentCheckState = CheckStateEx.Indeterminate;
- break;
- case System.Windows.Forms.CheckState.Unchecked:
- if (this.ThreeState || IsGreenTick)
- {
- this.CurrentCheckState = CheckStateEx.Unchecked;
- }
- else {
- this.CurrentCheckState = CheckStateEx.Indeterminate;
- }
- break;
- }
- }
- }
- #endregion
- #region Properites
- [Description("获取CheckBox左边正方形的宽度")]
- protected virtual int CheckRectWidth
- {
- get { return 13; }
- }
- #endregion
- CheckStateEx currentCheckState = CheckStateEx.Unchecked;
- /// <summary>
- /// 扩展当前状态
- /// </summary>
- public CheckStateEx CurrentCheckState
- {
- get { return currentCheckState; }
- set { currentCheckState = value;
- this.Invalidate();
- }
- }
- bool fourstates = false;
- /// <summary>
- /// 是否开启四种状态显示
- /// </summary>
- public bool Fourstates
- {
- get { return fourstates; }
- set {
- this.ThreeState = value;
- fourstates = value; }
- }
- #region Override
- protected override void OnMouseEnter(EventArgs e)
- {
- _state = QQControlState.Highlight;
- base.OnMouseEnter(e);
- }
- protected override void OnMouseLeave(EventArgs e)
- {
- _state = QQControlState.Normal;
- base.OnMouseLeave(e);
- }
- protected override void OnMouseDown(MouseEventArgs mevent)
- {
- if (mevent.Button == MouseButtons.Left)
- {
- _state = QQControlState.Down;
- }
- base.OnMouseDown(mevent);
- }
- protected override void OnMouseUp(MouseEventArgs mevent)
- {
- if (mevent.Button == MouseButtons.Left)
- {
- if (ClientRectangle.Contains(mevent.Location))
- {
- _state = QQControlState.Highlight;
- }
- else
- {
- _state = QQControlState.Normal;
- }
- }
- base.OnMouseUp(mevent);
- }
- protected override void OnEnabledChanged(EventArgs e)
- {
- if (Enabled)
- {
- _state = QQControlState.Normal;
- }
- else
- {
- _state = QQControlState.Disabled;
- }
- base.OnEnabledChanged(e);
- }
- protected override void OnPaint(PaintEventArgs pevent)
- {
- base.OnPaint(pevent);
- base.OnPaintBackground(pevent);
- Graphics g = pevent.Graphics;
- g.SmoothingMode = SmoothingMode.AntiAlias;
- g.InterpolationMode = InterpolationMode.HighQualityBicubic;
- Rectangle checkRect, textRect;
- CalculateRect(out checkRect, out textRect);
- if (Enabled == false)
- {
- _state = QQControlState.Disabled;
- }
- switch (_state)
- {
- case QQControlState.Highlight:
- case QQControlState.Down:
- DrawHighLightCheckRect(g, checkRect);
- break;
- case QQControlState.Disabled:
- DrawDisabledCheckRect(g, checkRect);
- break;
- default:
- DrawNormalCheckRect(g, checkRect);
- break;
- }
- Color textColor = (Enabled == true) ? ForeColor : SystemColors.GrayText;
- TextRenderer.DrawText(
- g,
- Text,
- Font,
- textRect,
- textColor,
- GetTextFormatFlags(TextAlign, RightToLeft == RightToLeft.Yes));
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- if (_checkImg != null)
- {
- _checkImg.Dispose();
- }
- if (_defaultFont != null)
- {
- _defaultFont.Dispose();
- }
- if (_checkImgDel != null) {
- _checkImgDel.Dispose();
- }
- if (_checkImgSpecially != null)
- {
- _checkImgSpecially.Dispose();
- }
-
- }
- _checkImg = null;
- _checkImgDel = null;
- _checkImgSpecially = null;
- _defaultFont = null;
- base.Dispose(disposing);
- }
- #endregion
- #region Private
- private void DrawNormalCheckRect(Graphics g, Rectangle checkRect)
- {
- g.FillRectangle(Brushes.White, checkRect);
- using (Pen borderPen = new Pen(ColorTable.QQBorderColor))
- {
- g.DrawRectangle(borderPen, checkRect);
- }
- if (!Fourstates)
- {
- switch (this.CheckState)
- {
- case System.Windows.Forms.CheckState.Checked:
- g.DrawImage(_checkImg, checkRect, 0, 0, _checkImg.Width, _checkImg.Height, GraphicsUnit.Pixel);
- break;
- case System.Windows.Forms.CheckState.Indeterminate:
- g.DrawImage(_checkImgDel, checkRect, 0, 0, _checkImg.Width, _checkImg.Height, GraphicsUnit.Pixel);
- break;
- }
- }
- else {
- switch (this.CurrentCheckState)
- {
- case CheckStateEx.Checked:
- g.DrawImage(_checkImgSpecially, checkRect, 0, 0, _checkImg.Width, _checkImg.Height, GraphicsUnit.Pixel);
- break;
- case CheckStateEx.Indeterminate:
- g.DrawImage(_checkImgDel, checkRect, 0, 0, _checkImg.Width, _checkImg.Height, GraphicsUnit.Pixel);
- break;
- case CheckStateEx.Specially:
- g.DrawImage(_checkImg, checkRect, 0, 0, _checkImg.Width, _checkImg.Height, GraphicsUnit.Pixel);
- break;
- }
- }
- /* if (Checked)
- {
- g.DrawImage(_checkImg,checkRect, 0,0,_checkImg.Width, _checkImg.Height,GraphicsUnit.Pixel);
- }*/
- }
- private void DrawHighLightCheckRect(Graphics g, Rectangle checkRect)
- {
- DrawNormalCheckRect(g, checkRect);
- using (Pen p = new Pen(ColorTable.QQHighLightInnerColor))
- {
- g.DrawRectangle(p, checkRect);
- checkRect.Inflate(1, 1);
- p.Color = ColorTable.QQHighLightColor;
- g.DrawRectangle(p, checkRect);
- }
- }
- private void DrawDisabledCheckRect(Graphics g, Rectangle checkRect)
- {
- g.DrawRectangle(SystemPens.ControlDark, checkRect);
- if (!Fourstates)
- {
- switch (this.CheckState)
- {
- case System.Windows.Forms.CheckState.Checked:
- g.DrawImage(_checkImg, checkRect, 0, 0, _checkImg.Width, _checkImg.Height, GraphicsUnit.Pixel);
- break;
- case System.Windows.Forms.CheckState.Indeterminate:
- g.DrawImage(_checkImgDel, checkRect, 0, 0, _checkImg.Width, _checkImg.Height, GraphicsUnit.Pixel);
- break;
- }
- }
- else {
- switch (this.CurrentCheckState)
- {
- case CheckStateEx.Checked:
- g.DrawImage(_checkImgSpecially, checkRect, 0, 0, _checkImg.Width, _checkImg.Height, GraphicsUnit.Pixel);
- break;
- case CheckStateEx.Indeterminate:
- g.DrawImage(_checkImgDel, checkRect, 0, 0, _checkImg.Width, _checkImg.Height, GraphicsUnit.Pixel);
- break;
- case CheckStateEx.Specially:
- g.DrawImage(_checkImg, checkRect, 0, 0, _checkImg.Width, _checkImg.Height, GraphicsUnit.Pixel);
- break;
- }
- }
- /* if (Checked)
- {
- g.DrawImage(_checkImg,checkRect,0,0, _checkImg.Width,_checkImg.Height,GraphicsUnit.Pixel);
- }*/
- }
- private void SetStyles()
- {
- SetStyle(ControlStyles.UserPaint, true);
- SetStyle(ControlStyles.AllPaintingInWmPaint, true);
- SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
- SetStyle(ControlStyles.ResizeRedraw, true);
- SetStyle(ControlStyles.SupportsTransparentBackColor, true);
- UpdateStyles();
- }
- private void CalculateRect(out Rectangle checkRect, out Rectangle textRect)
- {
- checkRect = new Rectangle(
- 0, 0, CheckRectWidth, CheckRectWidth);
- textRect = Rectangle.Empty;
- bool bCheckAlignLeft = (int)(LeftAlignment & CheckAlign) != 0;
- bool bCheckAlignRight = (int)(RightAlignment & CheckAlign) != 0;
- bool bRightToLeft = (RightToLeft == RightToLeft.Yes);
- if ((bCheckAlignLeft && !bRightToLeft) ||
- (bCheckAlignRight && bRightToLeft))
- {
- switch (CheckAlign)
- {
- case ContentAlignment.TopRight:
- case ContentAlignment.TopLeft:
- checkRect.Y = 2;
- break;
- case ContentAlignment.MiddleRight:
- case ContentAlignment.MiddleLeft:
- checkRect.Y = (Height - CheckRectWidth) / 2;
- break;
- case ContentAlignment.BottomRight:
- case ContentAlignment.BottomLeft:
- checkRect.Y = Height - CheckRectWidth - 2;
- break;
- }
- checkRect.X = 1;
- textRect = new Rectangle(
- checkRect.Right + 2,
- 0,
- Width - checkRect.Right - 4,
- Height);
- }
- else if ((bCheckAlignRight && !bRightToLeft)
- || (bCheckAlignLeft && bRightToLeft))
- {
- switch (CheckAlign)
- {
- case ContentAlignment.TopLeft:
- case ContentAlignment.TopRight:
- checkRect.Y = 2;
- break;
- case ContentAlignment.MiddleLeft:
- case ContentAlignment.MiddleRight:
- checkRect.Y = (Height - CheckRectWidth) / 2;
- break;
- case ContentAlignment.BottomLeft:
- case ContentAlignment.BottomRight:
- checkRect.Y = Height - CheckRectWidth - 2;
- break;
- }
- checkRect.X = Width - CheckRectWidth - 1;
- textRect = new Rectangle(
- 2, 0, Width - CheckRectWidth - 6, Height);
- }
- else
- {
- switch (CheckAlign)
- {
- case ContentAlignment.TopCenter:
- checkRect.Y = 2;
- textRect.Y = checkRect.Bottom + 2;
- textRect.Height = Height - CheckRectWidth - 6;
- break;
- case ContentAlignment.MiddleCenter:
- checkRect.Y = (Height - CheckRectWidth) / 2;
- textRect.Y = 0;
- textRect.Height = Height;
- break;
- case ContentAlignment.BottomCenter:
- checkRect.Y = Height - CheckRectWidth - 2;
- textRect.Y = 0;
- textRect.Height = Height - CheckRectWidth - 6;
- break;
- }
- checkRect.X = (Width - CheckRectWidth) / 2;
- textRect.X = 2;
- textRect.Width = Width - 4;
- }
- }
- private static TextFormatFlags GetTextFormatFlags(ContentAlignment alignment, bool rightToleft)
- {
- TextFormatFlags flags = TextFormatFlags.WordBreak |
- TextFormatFlags.SingleLine;
- if (rightToleft)
- {
- flags |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
- }
- switch (alignment)
- {
- case ContentAlignment.BottomCenter:
- flags |= TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter;
- break;
- case ContentAlignment.BottomLeft:
- flags |= TextFormatFlags.Bottom | TextFormatFlags.Left;
- break;
- case ContentAlignment.BottomRight:
- flags |= TextFormatFlags.Bottom | TextFormatFlags.Right;
- break;
- case ContentAlignment.MiddleCenter:
- flags |= TextFormatFlags.HorizontalCenter |
- TextFormatFlags.VerticalCenter;
- break;
- case ContentAlignment.MiddleLeft:
- flags |= TextFormatFlags.VerticalCenter | TextFormatFlags.Left;
- break;
- case ContentAlignment.MiddleRight:
- flags |= TextFormatFlags.VerticalCenter | TextFormatFlags.Right;
- break;
- case ContentAlignment.TopCenter:
- flags |= TextFormatFlags.Top | TextFormatFlags.HorizontalCenter;
- break;
- case ContentAlignment.TopLeft:
- flags |= TextFormatFlags.Top | TextFormatFlags.Left;
- break;
- case ContentAlignment.TopRight:
- flags |= TextFormatFlags.Top | TextFormatFlags.Right;
- break;
- }
- return flags;
- }
- #endregion
- }
- /// <summary>
- /// CheckBox自定义状态
- /// </summary>
- public enum CheckStateEx
- {
- /// <summary>
- /// 勾选
- /// </summary>
- Checked = CheckState.Checked,
- /// <summary>
- /// 禁用
- /// </summary>
- Indeterminate = CheckState.Indeterminate,
- /// <summary>
- /// 未勾选
- /// </summary>
- Unchecked = CheckState.Unchecked,
- /// <summary>
- /// 特别状态
- /// </summary>
- Specially=100,
- }
- }
|