TextBoxBasicEx.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.IO;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.ComponentModel;
  9. using System.Runtime.InteropServices;
  10. namespace LYFZ.ComponentLibrary
  11. {
  12. public class TextBoxBasicEx : System.Windows.Forms.TextBox
  13. {
  14. public enum QQControlState
  15. {
  16. /// <summary>
  17. /// 正常状态
  18. /// </summary>
  19. Normal = 0,
  20. /// <summary>
  21. /// /鼠标进入
  22. /// </summary>
  23. Highlight = 1,
  24. /// <summary>
  25. /// 鼠标按下
  26. /// </summary>
  27. Down = 2,
  28. /// <summary>
  29. /// 获得焦点
  30. /// </summary>
  31. Focus = 3,
  32. /// <summary>
  33. /// 控件禁止
  34. /// </summary>
  35. Disabled = 4
  36. }
  37. //private bool _isCloseQaJiaoChar = true;
  38. ///// <summary>
  39. ///// 是否限制全角输入
  40. ///// </summary>
  41. //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
  42. //[Category("控件扩展属性"), Description("是否限制全角输入")]
  43. //public bool IsCloseQaJiaoChar
  44. //{
  45. // get { return _isCloseQaJiaoChar; }
  46. // set { _isCloseQaJiaoChar = value; }
  47. //}
  48. ///// <summary>
  49. ///// 重写输入限制
  50. ///// </summary>
  51. ///// <param name="e"></param>
  52. //protected override void OnKeyPress(KeyPressEventArgs e)
  53. //{
  54. // if (this.IsCloseQaJiaoChar && LYFZ.BLL.OtherCommonModel.IsQaJiaoChar(e.KeyChar))
  55. // { e.Handled = true; }
  56. // else
  57. // { base.OnKeyPress(e); }
  58. //}
  59. ///// <summary>
  60. ///// 重写离开事件
  61. ///// </summary>
  62. ///// <param name="e"></param>
  63. //protected override void OnLeave(EventArgs e)
  64. //{
  65. // if (this.IsCloseQaJiaoChar)
  66. // {
  67. // if (LYFZ.BLL.OtherCommonModel.StringContainsQaJiao(this.Text.Trim()))
  68. // { this.Text = ""; }
  69. // }
  70. // base.OnLeave(e);
  71. //}
  72. internal class ColorTable
  73. {
  74. public static Color QQBorderColor = Color.FromArgb(174, 168, 168); // //LightBlue = Color.FromArgb(173, 216, 230)
  75. public static Color QQHighLightColor = Color.FromArgb(174, 168, 168); //GetColor(QQBorderColor, 255, -63, -11, 23); //Color.FromArgb(110, 205, 253)
  76. public static Color QQHighLightInnerColor = Color.FromArgb(174, 168, 168); //GetColor(QQBorderColor, 255, -100, -44, 1); //Color.FromArgb(73, 172, 231);
  77. }
  78. /// <summary>
  79. /// 返回给定的颜色的ARGB的分量差值的颜色
  80. /// </summary>
  81. /// <param name="colorBase"></param>
  82. /// <param name="a">A</param>
  83. /// <param name="r">R</param>
  84. /// <param name="g">G</param>
  85. /// <param name="b">B</param>
  86. /// <returns></returns>
  87. public static Color GetColor(Color colorBase, int a, int r, int g, int b)
  88. {
  89. int a0 = colorBase.A;
  90. int r0 = colorBase.R;
  91. int g0 = colorBase.G;
  92. int b0 = colorBase.B;
  93. if (a + a0 > 255) { a = 255; } else { a = Math.Max(a + a0, 0); }
  94. if (r + r0 > 255) { r = 255; } else { r = Math.Max(r + r0, 0); }
  95. if (g + g0 > 255) { g = 255; } else { g = Math.Max(g + g0, 0); }
  96. if (b + b0 > 255) { b = 255; } else { b = Math.Max(b + b0, 0); }
  97. return Color.FromArgb(a, r, g, b);
  98. }
  99. #region Field
  100. private QQControlState _state = QQControlState.Normal;
  101. private Font _defaultFont = new Font("微软雅黑", 9);
  102. //当Text属性为空时编辑框内出现的提示文本
  103. private string _emptyTextTip;
  104. private Color _emptyTextTipColor = Color.FromArgb(174, 168, 168);
  105. #endregion
  106. #region Constructor
  107. public TextBoxBasicEx()
  108. {
  109. SetStyles();
  110. this.Font = _defaultFont;
  111. }
  112. #endregion
  113. #region Properites
  114. [Description("当Text属性为空时编辑框内出现的提示文本")]
  115. public String EmptyTextTip
  116. {
  117. get { return _emptyTextTip; }
  118. set
  119. {
  120. if (_emptyTextTip != value)
  121. {
  122. _emptyTextTip = value;
  123. base.Invalidate();
  124. }
  125. }
  126. }
  127. [Description("获取或设置EmptyTextTip的颜色")]
  128. public Color EmptyTextTipColor
  129. {
  130. get { return _emptyTextTipColor; }
  131. set
  132. {
  133. if (_emptyTextTipColor != value)
  134. {
  135. _emptyTextTipColor = value;
  136. base.Invalidate();
  137. }
  138. }
  139. }
  140. #endregion
  141. #region Override
  142. protected override void OnMouseEnter(EventArgs e)
  143. {
  144. _state = QQControlState.Highlight;
  145. base.OnMouseEnter(e);
  146. }
  147. protected override void OnMouseLeave(EventArgs e)
  148. {
  149. //if (_state == QQControlState.Highlight && Focused)
  150. //{
  151. // _state = QQControlState.Focus;
  152. //}
  153. //else if (_state == QQControlState.Focus)
  154. //{
  155. // _state = QQControlState.Focus;
  156. //}
  157. //else
  158. //{
  159. _state = QQControlState.Normal;
  160. //}
  161. base.OnMouseLeave(e);
  162. }
  163. protected override void OnMouseDown(MouseEventArgs mevent)
  164. {
  165. if (mevent.Button == MouseButtons.Left)
  166. {
  167. _state = QQControlState.Highlight;
  168. }
  169. base.OnMouseDown(mevent);
  170. }
  171. protected override void OnMouseUp(MouseEventArgs mevent)
  172. {
  173. if (mevent.Button == MouseButtons.Left)
  174. {
  175. if (ClientRectangle.Contains(mevent.Location))
  176. {
  177. _state = QQControlState.Highlight;
  178. }
  179. else
  180. {
  181. _state = QQControlState.Focus;
  182. }
  183. }
  184. base.OnMouseUp(mevent);
  185. }
  186. protected override void OnLostFocus(EventArgs e)
  187. {
  188. _state = QQControlState.Normal;
  189. base.OnLostFocus(e);
  190. }
  191. protected override void OnEnabledChanged(EventArgs e)
  192. {
  193. if (Enabled)
  194. {
  195. _state = QQControlState.Normal;
  196. }
  197. else
  198. {
  199. _state = QQControlState.Disabled;
  200. }
  201. base.OnEnabledChanged(e);
  202. }
  203. protected override void WndProc(ref Message m)
  204. {//TextBox是由系统进程绘制,重载OnPaint方法将不起作用
  205. base.WndProc(ref m);
  206. if (m.Msg == Win32.WM_PAINT || m.Msg == Win32.WM_CTLCOLOREDIT)
  207. {
  208. WmPaint(ref m);
  209. }
  210. }
  211. protected override void Dispose(bool disposing)
  212. {
  213. if (disposing)
  214. {
  215. if (_defaultFont != null)
  216. {
  217. _defaultFont.Dispose();
  218. }
  219. }
  220. _defaultFont = null;
  221. base.Dispose(disposing);
  222. }
  223. #endregion
  224. #region Private
  225. private void SetStyles()
  226. {
  227. // TextBox由系统绘制,不能设置 ControlStyles.UserPaint样式
  228. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  229. SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  230. SetStyle(ControlStyles.ResizeRedraw, true);
  231. SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  232. UpdateStyles();
  233. }
  234. private void WmPaint(ref Message m)
  235. {
  236. Graphics g = Graphics.FromHwnd(base.Handle);
  237. g.SmoothingMode = SmoothingMode.HighQuality;
  238. if (!Enabled)
  239. {
  240. _state = QQControlState.Disabled;
  241. }
  242. switch (_state)
  243. {
  244. case QQControlState.Normal:
  245. DrawNormalTextBox(g);
  246. break;
  247. case QQControlState.Highlight:
  248. DrawHighLightTextBox(g);
  249. break;
  250. case QQControlState.Focus:
  251. DrawFocusTextBox(g);
  252. break;
  253. case QQControlState.Disabled:
  254. DrawDisabledTextBox(g);
  255. break;
  256. default:
  257. break;
  258. }
  259. if (Text.Length == 0 && !string.IsNullOrEmpty(EmptyTextTip) && !Focused)
  260. {
  261. TextRenderer.DrawText(g, EmptyTextTip, Font, ClientRectangle, EmptyTextTipColor, GetTextFormatFlags(TextAlign, RightToLeft == RightToLeft.Yes));
  262. }
  263. }
  264. private void DrawNormalTextBox(Graphics g)
  265. {
  266. using (Pen borderPen = new Pen(ColorTable.QQHighLightInnerColor))
  267. {
  268. g.DrawRectangle(
  269. borderPen,
  270. new Rectangle(
  271. ClientRectangle.X,
  272. ClientRectangle.Y,
  273. ClientRectangle.Width - 1,
  274. ClientRectangle.Height - 1));
  275. }
  276. }
  277. private void DrawHighLightTextBox(Graphics g)
  278. {
  279. using (Pen highLightPen = new Pen(ColorTable.QQHighLightColor))
  280. {
  281. Rectangle drawRect = new Rectangle(
  282. ClientRectangle.X,
  283. ClientRectangle.Y,
  284. ClientRectangle.Width - 1,
  285. ClientRectangle.Height - 1);
  286. // g.DrawRectangle(highLightPen, drawRect);
  287. //InnerRect
  288. drawRect.Inflate(-1, -1);
  289. highLightPen.Color = ColorTable.QQHighLightInnerColor;
  290. g.DrawRectangle(highLightPen, drawRect);
  291. }
  292. }
  293. private void DrawFocusTextBox(Graphics g)
  294. {
  295. using (Pen focusedBorderPen = new Pen(ColorTable.QQHighLightInnerColor))
  296. {
  297. g.DrawRectangle(
  298. focusedBorderPen,
  299. new Rectangle(
  300. ClientRectangle.X,
  301. ClientRectangle.Y,
  302. ClientRectangle.Width - 1,
  303. ClientRectangle.Height - 1));
  304. }
  305. }
  306. private void DrawDisabledTextBox(Graphics g)
  307. {
  308. using (Pen disabledPen = new Pen(ColorTable.QQHighLightInnerColor))
  309. {
  310. g.DrawRectangle(disabledPen,
  311. new Rectangle(
  312. ClientRectangle.X,
  313. ClientRectangle.Y,
  314. ClientRectangle.Width - 1,
  315. ClientRectangle.Height - 1));
  316. }
  317. }
  318. private static TextFormatFlags GetTextFormatFlags(HorizontalAlignment alignment, bool rightToleft)
  319. {
  320. TextFormatFlags flags = TextFormatFlags.WordBreak |
  321. TextFormatFlags.SingleLine;
  322. if (rightToleft)
  323. {
  324. flags |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
  325. }
  326. switch (alignment)
  327. {
  328. case HorizontalAlignment.Center:
  329. flags |= TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter;
  330. break;
  331. case HorizontalAlignment.Left:
  332. flags |= TextFormatFlags.VerticalCenter | TextFormatFlags.Left;
  333. break;
  334. case HorizontalAlignment.Right:
  335. flags |= TextFormatFlags.VerticalCenter | TextFormatFlags.Right;
  336. break;
  337. }
  338. return flags;
  339. }
  340. #endregion
  341. public class Win32
  342. {
  343. #region Window Const
  344. public const int WM_KEYDOWN = 0x0100;
  345. public const int WM_KEYUP = 0x0101;
  346. public const int WM_CTLCOLOREDIT = 0x133;
  347. public const int WM_ERASEBKGND = 0x0014;
  348. public const int WM_LBUTTONDOWN = 0x0201;
  349. public const int WM_LBUTTONUP = 0x0202;
  350. public const int WM_LBUTTONDBLCLK = 0x0203;
  351. public const int WM_WINDOWPOSCHANGING = 0x46;
  352. public const int WM_PAINT = 0xF;
  353. public const int WM_CREATE = 0x0001;
  354. public const int WM_ACTIVATE = 0x0006;
  355. public const int WM_NCCREATE = 0x0081;
  356. public const int WM_NCCALCSIZE = 0x0083;
  357. public const int WM_NCPAINT = 0x0085;
  358. public const int WM_NCACTIVATE = 0x0086;
  359. public const int WM_NCLBUTTONDOWN = 0x00A1;
  360. public const int WM_NCLBUTTONUP = 0x00A2;
  361. public const int WM_NCLBUTTONDBLCLK = 0x00A3;
  362. public const int WM_NCMOUSEMOVE = 0x00A0;
  363. public const int WM_NCHITTEST = 0x0084;
  364. public const int HTLEFT = 10;
  365. public const int HTRIGHT = 11;
  366. public const int HTTOP = 12;
  367. public const int HTTOPLEFT = 13;
  368. public const int HTTOPRIGHT = 14;
  369. public const int HTBOTTOM = 15;
  370. public const int HTBOTTOMLEFT = 0x10;
  371. public const int HTBOTTOMRIGHT = 17;
  372. public const int HTCAPTION = 2;
  373. public const int HTCLIENT = 1;
  374. public const int WM_FALSE = 0;
  375. public const int WM_TRUE = 1;
  376. #endregion
  377. #region Public extern methods
  378. [DllImport("gdi32.dll")]
  379. public static extern int CreateRoundRectRgn(int x1, int y1, int x2, int y2, int x3, int y3);
  380. [DllImport("user32.dll")]
  381. public static extern int SetWindowRgn(IntPtr hwnd, int hRgn, Boolean bRedraw);
  382. [DllImport("gdi32.dll", EntryPoint = "DeleteObject", CharSet = CharSet.Ansi)]
  383. public static extern int DeleteObject(int hObject);
  384. [DllImport("user32.dll")]
  385. public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
  386. [DllImport("user32.dll")]
  387. public static extern bool ReleaseCapture();
  388. #endregion
  389. }
  390. }
  391. }