123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 |
- 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;
- using System.Runtime.InteropServices;
- namespace LYFZ.ComponentLibrary
- {
- public class TextBoxBasicEx : System.Windows.Forms.TextBox
- {
- public enum QQControlState
- {
- /// <summary>
- /// 正常状态
- /// </summary>
- Normal = 0,
- /// <summary>
- /// /鼠标进入
- /// </summary>
- Highlight = 1,
- /// <summary>
- /// 鼠标按下
- /// </summary>
- Down = 2,
- /// <summary>
- /// 获得焦点
- /// </summary>
- Focus = 3,
- /// <summary>
- /// 控件禁止
- /// </summary>
- Disabled = 4
- }
- //private bool _isCloseQaJiaoChar = true;
- ///// <summary>
- ///// 是否限制全角输入
- ///// </summary>
- //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
- //[Category("控件扩展属性"), Description("是否限制全角输入")]
- //public bool IsCloseQaJiaoChar
- //{
- // get { return _isCloseQaJiaoChar; }
- // set { _isCloseQaJiaoChar = value; }
- //}
- ///// <summary>
- ///// 重写输入限制
- ///// </summary>
- ///// <param name="e"></param>
- //protected override void OnKeyPress(KeyPressEventArgs e)
- //{
- // if (this.IsCloseQaJiaoChar && LYFZ.BLL.OtherCommonModel.IsQaJiaoChar(e.KeyChar))
- // { e.Handled = true; }
- // else
- // { base.OnKeyPress(e); }
- //}
- ///// <summary>
- ///// 重写离开事件
- ///// </summary>
- ///// <param name="e"></param>
- //protected override void OnLeave(EventArgs e)
- //{
- // if (this.IsCloseQaJiaoChar)
- // {
- // if (LYFZ.BLL.OtherCommonModel.StringContainsQaJiao(this.Text.Trim()))
- // { this.Text = ""; }
- // }
- // base.OnLeave(e);
- //}
- internal class ColorTable
- {
- public static Color QQBorderColor = Color.FromArgb(174, 168, 168); // //LightBlue = Color.FromArgb(173, 216, 230)
- public static Color QQHighLightColor = Color.FromArgb(174, 168, 168); //GetColor(QQBorderColor, 255, -63, -11, 23); //Color.FromArgb(110, 205, 253)
- public static Color QQHighLightInnerColor = Color.FromArgb(174, 168, 168); //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 Font _defaultFont = new Font("微软雅黑", 9);
- //当Text属性为空时编辑框内出现的提示文本
- private string _emptyTextTip;
- private Color _emptyTextTipColor = Color.FromArgb(174, 168, 168);
- #endregion
- #region Constructor
- public TextBoxBasicEx()
- {
- SetStyles();
- this.Font = _defaultFont;
- }
- #endregion
- #region Properites
- [Description("当Text属性为空时编辑框内出现的提示文本")]
- public String EmptyTextTip
- {
- get { return _emptyTextTip; }
- set
- {
- if (_emptyTextTip != value)
- {
- _emptyTextTip = value;
- base.Invalidate();
- }
- }
- }
- [Description("获取或设置EmptyTextTip的颜色")]
- public Color EmptyTextTipColor
- {
- get { return _emptyTextTipColor; }
- set
- {
- if (_emptyTextTipColor != value)
- {
- _emptyTextTipColor = value;
- base.Invalidate();
- }
- }
- }
- #endregion
- #region Override
- protected override void OnMouseEnter(EventArgs e)
- {
- _state = QQControlState.Highlight;
- base.OnMouseEnter(e);
- }
- protected override void OnMouseLeave(EventArgs e)
- {
- //if (_state == QQControlState.Highlight && Focused)
- //{
- // _state = QQControlState.Focus;
- //}
- //else if (_state == QQControlState.Focus)
- //{
- // _state = QQControlState.Focus;
- //}
- //else
- //{
- _state = QQControlState.Normal;
- //}
- base.OnMouseLeave(e);
- }
- protected override void OnMouseDown(MouseEventArgs mevent)
- {
- if (mevent.Button == MouseButtons.Left)
- {
- _state = QQControlState.Highlight;
- }
- 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.Focus;
- }
- }
- base.OnMouseUp(mevent);
- }
- protected override void OnLostFocus(EventArgs e)
- {
- _state = QQControlState.Normal;
- base.OnLostFocus(e);
- }
- protected override void OnEnabledChanged(EventArgs e)
- {
- if (Enabled)
- {
- _state = QQControlState.Normal;
- }
- else
- {
- _state = QQControlState.Disabled;
- }
- base.OnEnabledChanged(e);
- }
- protected override void WndProc(ref Message m)
- {//TextBox是由系统进程绘制,重载OnPaint方法将不起作用
- base.WndProc(ref m);
- if (m.Msg == Win32.WM_PAINT || m.Msg == Win32.WM_CTLCOLOREDIT)
- {
- WmPaint(ref m);
- }
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- if (_defaultFont != null)
- {
- _defaultFont.Dispose();
- }
- }
- _defaultFont = null;
- base.Dispose(disposing);
- }
- #endregion
- #region Private
- private void SetStyles()
- {
- // TextBox由系统绘制,不能设置 ControlStyles.UserPaint样式
- SetStyle(ControlStyles.AllPaintingInWmPaint, true);
- SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
- SetStyle(ControlStyles.ResizeRedraw, true);
- SetStyle(ControlStyles.SupportsTransparentBackColor, true);
- UpdateStyles();
- }
- private void WmPaint(ref Message m)
- {
- Graphics g = Graphics.FromHwnd(base.Handle);
- g.SmoothingMode = SmoothingMode.HighQuality;
- if (!Enabled)
- {
- _state = QQControlState.Disabled;
- }
- switch (_state)
- {
- case QQControlState.Normal:
- DrawNormalTextBox(g);
- break;
- case QQControlState.Highlight:
- DrawHighLightTextBox(g);
- break;
- case QQControlState.Focus:
- DrawFocusTextBox(g);
- break;
- case QQControlState.Disabled:
- DrawDisabledTextBox(g);
- break;
- default:
- break;
- }
- if (Text.Length == 0 && !string.IsNullOrEmpty(EmptyTextTip) && !Focused)
- {
- TextRenderer.DrawText(g, EmptyTextTip, Font, ClientRectangle, EmptyTextTipColor, GetTextFormatFlags(TextAlign, RightToLeft == RightToLeft.Yes));
- }
- }
- private void DrawNormalTextBox(Graphics g)
- {
- using (Pen borderPen = new Pen(ColorTable.QQHighLightInnerColor))
- {
- g.DrawRectangle(
- borderPen,
- new Rectangle(
- ClientRectangle.X,
- ClientRectangle.Y,
- ClientRectangle.Width - 1,
- ClientRectangle.Height - 1));
- }
- }
- private void DrawHighLightTextBox(Graphics g)
- {
- using (Pen highLightPen = new Pen(ColorTable.QQHighLightColor))
- {
- Rectangle drawRect = new Rectangle(
- ClientRectangle.X,
- ClientRectangle.Y,
- ClientRectangle.Width - 1,
- ClientRectangle.Height - 1);
- // g.DrawRectangle(highLightPen, drawRect);
- //InnerRect
- drawRect.Inflate(-1, -1);
- highLightPen.Color = ColorTable.QQHighLightInnerColor;
- g.DrawRectangle(highLightPen, drawRect);
- }
- }
- private void DrawFocusTextBox(Graphics g)
- {
- using (Pen focusedBorderPen = new Pen(ColorTable.QQHighLightInnerColor))
- {
- g.DrawRectangle(
- focusedBorderPen,
- new Rectangle(
- ClientRectangle.X,
- ClientRectangle.Y,
- ClientRectangle.Width - 1,
- ClientRectangle.Height - 1));
- }
- }
- private void DrawDisabledTextBox(Graphics g)
- {
- using (Pen disabledPen = new Pen(ColorTable.QQHighLightInnerColor))
- {
- g.DrawRectangle(disabledPen,
- new Rectangle(
- ClientRectangle.X,
- ClientRectangle.Y,
- ClientRectangle.Width - 1,
- ClientRectangle.Height - 1));
- }
- }
- private static TextFormatFlags GetTextFormatFlags(HorizontalAlignment alignment, bool rightToleft)
- {
- TextFormatFlags flags = TextFormatFlags.WordBreak |
- TextFormatFlags.SingleLine;
- if (rightToleft)
- {
- flags |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
- }
- switch (alignment)
- {
- case HorizontalAlignment.Center:
- flags |= TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter;
- break;
- case HorizontalAlignment.Left:
- flags |= TextFormatFlags.VerticalCenter | TextFormatFlags.Left;
- break;
- case HorizontalAlignment.Right:
- flags |= TextFormatFlags.VerticalCenter | TextFormatFlags.Right;
- break;
- }
- return flags;
- }
- #endregion
- public class Win32
- {
- #region Window Const
- public const int WM_KEYDOWN = 0x0100;
- public const int WM_KEYUP = 0x0101;
- public const int WM_CTLCOLOREDIT = 0x133;
- public const int WM_ERASEBKGND = 0x0014;
- public const int WM_LBUTTONDOWN = 0x0201;
- public const int WM_LBUTTONUP = 0x0202;
- public const int WM_LBUTTONDBLCLK = 0x0203;
- public const int WM_WINDOWPOSCHANGING = 0x46;
- public const int WM_PAINT = 0xF;
- public const int WM_CREATE = 0x0001;
- public const int WM_ACTIVATE = 0x0006;
- public const int WM_NCCREATE = 0x0081;
- public const int WM_NCCALCSIZE = 0x0083;
- public const int WM_NCPAINT = 0x0085;
- public const int WM_NCACTIVATE = 0x0086;
- public const int WM_NCLBUTTONDOWN = 0x00A1;
- public const int WM_NCLBUTTONUP = 0x00A2;
- public const int WM_NCLBUTTONDBLCLK = 0x00A3;
- public const int WM_NCMOUSEMOVE = 0x00A0;
- public const int WM_NCHITTEST = 0x0084;
- public const int HTLEFT = 10;
- public const int HTRIGHT = 11;
- public const int HTTOP = 12;
- public const int HTTOPLEFT = 13;
- public const int HTTOPRIGHT = 14;
- public const int HTBOTTOM = 15;
- public const int HTBOTTOMLEFT = 0x10;
- public const int HTBOTTOMRIGHT = 17;
- public const int HTCAPTION = 2;
- public const int HTCLIENT = 1;
- public const int WM_FALSE = 0;
- public const int WM_TRUE = 1;
- #endregion
- #region Public extern methods
- [DllImport("gdi32.dll")]
- public static extern int CreateRoundRectRgn(int x1, int y1, int x2, int y2, int x3, int y3);
- [DllImport("user32.dll")]
- public static extern int SetWindowRgn(IntPtr hwnd, int hRgn, Boolean bRedraw);
- [DllImport("gdi32.dll", EntryPoint = "DeleteObject", CharSet = CharSet.Ansi)]
- public static extern int DeleteObject(int hObject);
- [DllImport("user32.dll")]
- public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
- [DllImport("user32.dll")]
- public static extern bool ReleaseCapture();
- #endregion
- }
- }
- }
|