TextBoxTransparent.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="TextBoxTransparent.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections;
  18. using System.ComponentModel;
  19. using System.Drawing;
  20. using System.Data;
  21. using System.Windows.Forms;
  22. using System.Drawing.Imaging;
  23. namespace HZH_Controls.Controls
  24. {
  25. /// <summary>
  26. /// Class TextBoxTransparent.
  27. /// Implements the <see cref="HZH_Controls.Controls.TextBoxEx" />
  28. /// </summary>
  29. /// <seealso cref="HZH_Controls.Controls.TextBoxEx" />
  30. public class TextBoxTransparent : TextBoxEx
  31. {
  32. #region private variables
  33. /// <summary>
  34. /// My PictureBox
  35. /// </summary>
  36. private uPictureBox myPictureBox;
  37. /// <summary>
  38. /// My up to date
  39. /// </summary>
  40. private bool myUpToDate = false;
  41. /// <summary>
  42. /// My caret up to date
  43. /// </summary>
  44. private bool myCaretUpToDate = false;
  45. /// <summary>
  46. /// My bitmap
  47. /// </summary>
  48. private Bitmap myBitmap;
  49. /// <summary>
  50. /// My alpha bitmap
  51. /// </summary>
  52. private Bitmap myAlphaBitmap;
  53. /// <summary>
  54. /// My font height
  55. /// </summary>
  56. private int myFontHeight = 10;
  57. /// <summary>
  58. /// My timer1
  59. /// </summary>
  60. private System.Windows.Forms.Timer myTimer1;
  61. /// <summary>
  62. /// My caret state
  63. /// </summary>
  64. private bool myCaretState = true;
  65. /// <summary>
  66. /// My painted first time
  67. /// </summary>
  68. private bool myPaintedFirstTime = false;
  69. /// <summary>
  70. /// My back color
  71. /// </summary>
  72. private Color myBackColor = Color.White;
  73. /// <summary>
  74. /// My back alpha
  75. /// </summary>
  76. private int myBackAlpha = 10;
  77. /// <summary>
  78. /// Required designer variable.
  79. /// </summary>
  80. private System.ComponentModel.Container components = null;
  81. #endregion // end private variables
  82. #region public methods and overrides
  83. /// <summary>
  84. /// Initializes a new instance of the <see cref="TextBoxTransparent" /> class.
  85. /// </summary>
  86. public TextBoxTransparent()
  87. {
  88. // This call is required by the Windows.Forms Form Designer.
  89. InitializeComponent();
  90. // TODO: Add any initialization after the InitializeComponent call
  91. this.BackColor = myBackColor;
  92. this.SetStyle(ControlStyles.UserPaint, false);
  93. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  94. this.SetStyle(ControlStyles.DoubleBuffer, true);
  95. myPictureBox = new uPictureBox();
  96. this.Controls.Add(myPictureBox);
  97. myPictureBox.Dock = DockStyle.Fill;
  98. }
  99. /// <summary>
  100. /// 引发 <see cref="E:System.Windows.Forms.Control.Resize" /> 事件。
  101. /// </summary>
  102. /// <param name="e">包含事件数据的 <see cref="T:System.EventArgs" />。</param>
  103. protected override void OnResize(EventArgs e)
  104. {
  105. base.OnResize(e);
  106. this.myBitmap = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);//(this.Width,this.Height);
  107. this.myAlphaBitmap = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);//(this.Width,this.Height);
  108. myUpToDate = false;
  109. this.Invalidate();
  110. }
  111. //Some of these should be moved to the WndProc later
  112. /// <summary>
  113. /// 引发 <see cref="E:System.Windows.Forms.Control.KeyDown" /> 事件。
  114. /// </summary>
  115. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.KeyEventArgs" />。</param>
  116. protected override void OnKeyDown(KeyEventArgs e)
  117. {
  118. base.OnKeyDown(e);
  119. myUpToDate = false;
  120. this.Invalidate();
  121. }
  122. /// <summary>
  123. /// 引发 <see cref="E:System.Windows.Forms.Control.KeyUp" /> 事件。
  124. /// </summary>
  125. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.KeyEventArgs" />。</param>
  126. protected override void OnKeyUp(KeyEventArgs e)
  127. {
  128. base.OnKeyUp(e);
  129. myUpToDate = false;
  130. this.Invalidate();
  131. }
  132. /// <summary>
  133. /// 引发 <see cref="E:System.Windows.Forms.Control.KeyPress" /> 事件。
  134. /// </summary>
  135. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.KeyPressEventArgs" />。</param>
  136. protected override void OnKeyPress(KeyPressEventArgs e)
  137. {
  138. base.OnKeyPress(e);
  139. myUpToDate = false;
  140. this.Invalidate();
  141. }
  142. /// <summary>
  143. /// 引发 <see cref="E:System.Windows.Forms.Control.MouseUp" /> 事件。
  144. /// </summary>
  145. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.MouseEventArgs" />。</param>
  146. protected override void OnMouseUp(MouseEventArgs e)
  147. {
  148. base.OnMouseUp(e);
  149. this.Invalidate();
  150. }
  151. /// <summary>
  152. /// 引发 <see cref="E:System.Windows.Forms.Control.GiveFeedback" /> 事件。
  153. /// </summary>
  154. /// <param name="gfbevent">包含事件数据的 <see cref="T:System.Windows.Forms.GiveFeedbackEventArgs" />。</param>
  155. protected override void OnGiveFeedback(GiveFeedbackEventArgs gfbevent)
  156. {
  157. base.OnGiveFeedback(gfbevent);
  158. myUpToDate = false;
  159. this.Invalidate();
  160. }
  161. /// <summary>
  162. /// 引发 <see cref="E:System.Windows.Forms.Control.MouseLeave" /> 事件。
  163. /// </summary>
  164. /// <param name="e">包含事件数据的 <see cref="T:System.EventArgs" />。</param>
  165. protected override void OnMouseLeave(EventArgs e)
  166. {
  167. //found this code to find the current cursor location
  168. //at http://www.syncfusion.com/FAQ/WinForms/FAQ_c50c.asp#q597q
  169. Point ptCursor = Cursor.Position;
  170. Form f = this.FindForm();
  171. ptCursor = f.PointToClient(ptCursor);
  172. if (!this.Bounds.Contains(ptCursor))
  173. base.OnMouseLeave(e);
  174. }
  175. /// <summary>
  176. /// 引发 <see cref="E:System.Windows.Forms.Control.ChangeUICues" /> 事件。
  177. /// </summary>
  178. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.UICuesEventArgs" />。</param>
  179. protected override void OnChangeUICues(UICuesEventArgs e)
  180. {
  181. base.OnChangeUICues(e);
  182. myUpToDate = false;
  183. this.Invalidate();
  184. }
  185. //--
  186. /// <summary>
  187. /// 引发 <see cref="E:System.Windows.Forms.Control.GotFocus" /> 事件。
  188. /// </summary>
  189. /// <param name="e">包含事件数据的 <see cref="T:System.EventArgs" />。</param>
  190. protected override void OnGotFocus(EventArgs e)
  191. {
  192. base.OnGotFocus(e);
  193. myCaretUpToDate = false;
  194. myUpToDate = false;
  195. this.Invalidate();
  196. myTimer1 = new System.Windows.Forms.Timer(this.components);
  197. myTimer1.Interval = (int)win32.GetCaretBlinkTime(); // usually around 500;
  198. myTimer1.Tick += new EventHandler(myTimer1_Tick);
  199. myTimer1.Enabled = true;
  200. }
  201. /// <summary>
  202. /// 引发 <see cref="E:System.Windows.Forms.Control.LostFocus" /> 事件。
  203. /// </summary>
  204. /// <param name="e">包含事件数据的 <see cref="T:System.EventArgs" />。</param>
  205. protected override void OnLostFocus(EventArgs e)
  206. {
  207. base.OnLostFocus(e);
  208. myCaretUpToDate = false;
  209. myUpToDate = false;
  210. this.Invalidate();
  211. myTimer1.Dispose();
  212. }
  213. //--
  214. /// <summary>
  215. /// 引发 <see cref="E:System.Windows.Forms.Control.FontChanged" /> 事件。
  216. /// </summary>
  217. /// <param name="e">包含事件数据的 <see cref="T:System.EventArgs" />。</param>
  218. protected override void OnFontChanged(EventArgs e)
  219. {
  220. if (this.myPaintedFirstTime)
  221. this.SetStyle(ControlStyles.UserPaint, false);
  222. base.OnFontChanged(e);
  223. if (this.myPaintedFirstTime)
  224. this.SetStyle(ControlStyles.UserPaint, true);
  225. myFontHeight = GetFontHeight();
  226. myUpToDate = false;
  227. this.Invalidate();
  228. }
  229. /// <summary>
  230. /// Handles the <see cref="E:TextChanged" /> event.
  231. /// </summary>
  232. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  233. protected override void OnTextChanged(EventArgs e)
  234. {
  235. base.OnTextChanged(e);
  236. myUpToDate = false;
  237. this.Invalidate();
  238. }
  239. /// <summary>
  240. /// 处理 Windows 消息。
  241. /// </summary>
  242. /// <param name="m">一个 Windows 消息对象。</param>
  243. protected override void WndProc(ref Message m)
  244. {
  245. base.WndProc(ref m);
  246. // need to rewrite as a big switch
  247. if (m.Msg == win32.WM_PAINT)
  248. {
  249. myPaintedFirstTime = true;
  250. if (!myUpToDate || !myCaretUpToDate)
  251. GetBitmaps();
  252. myUpToDate = true;
  253. myCaretUpToDate = true;
  254. if (myPictureBox.Image != null) myPictureBox.Image.Dispose();
  255. if (string.IsNullOrEmpty(this.Text) && !string.IsNullOrEmpty(this.PromptText))
  256. {
  257. Bitmap bit = (Bitmap)myAlphaBitmap.Clone();
  258. Graphics g = Graphics.FromImage(bit);
  259. SizeF sizet1 = g.MeasureString(this.PromptText, this.PromptFont);
  260. g.DrawString(this.PromptText, this.PromptFont, new SolidBrush(PromptColor), new PointF(3, (bit.Height - sizet1.Height) / 2));
  261. g.Dispose();
  262. myPictureBox.Image = bit;
  263. }
  264. else
  265. {
  266. myPictureBox.Image = (Image)myAlphaBitmap.Clone();
  267. }
  268. }
  269. else if (m.Msg == win32.WM_HSCROLL || m.Msg == win32.WM_VSCROLL)
  270. {
  271. myUpToDate = false;
  272. this.Invalidate();
  273. }
  274. else if (m.Msg == win32.WM_LBUTTONDOWN
  275. || m.Msg == win32.WM_RBUTTONDOWN
  276. || m.Msg == win32.WM_LBUTTONDBLCLK
  277. // || m.Msg == win32.WM_MOUSELEAVE ///****
  278. )
  279. {
  280. myUpToDate = false;
  281. this.Invalidate();
  282. }
  283. else if (m.Msg == win32.WM_MOUSEMOVE)
  284. {
  285. if (m.WParam.ToInt32() != 0) //shift key or other buttons
  286. {
  287. myUpToDate = false;
  288. this.Invalidate();
  289. }
  290. }
  291. if (m.Msg == 15 || m.Msg == 7 || m.Msg == 8)
  292. {
  293. base.OnPaint(null);
  294. }
  295. }
  296. /// <summary>
  297. /// Clean up any resources being used.
  298. /// </summary>
  299. /// <param name="disposing">为 true 则释放托管资源和非托管资源;为 false 则仅释放非托管资源。</param>
  300. protected override void Dispose(bool disposing)
  301. {
  302. if (disposing)
  303. {
  304. //this.BackColor = Color.Pink;
  305. if (components != null)
  306. {
  307. components.Dispose();
  308. }
  309. }
  310. base.Dispose(disposing);
  311. }
  312. #endregion //end public method and overrides
  313. #region public property overrides
  314. /// <summary>
  315. /// 获取或设置文本框控件的边框类型。
  316. /// </summary>
  317. /// <value>The border style.</value>
  318. /// <PermissionSet>
  319. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  320. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  321. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  322. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  323. /// </PermissionSet>
  324. public new BorderStyle BorderStyle
  325. {
  326. get { return base.BorderStyle; }
  327. set
  328. {
  329. if (this.myPaintedFirstTime)
  330. this.SetStyle(ControlStyles.UserPaint, false);
  331. base.BorderStyle = value;
  332. if (this.myPaintedFirstTime)
  333. this.SetStyle(ControlStyles.UserPaint, true);
  334. this.myBitmap = null;
  335. this.myAlphaBitmap = null;
  336. myUpToDate = false;
  337. this.Invalidate();
  338. }
  339. }
  340. /// <summary>
  341. /// 获取或设置控件的背景色。
  342. /// </summary>
  343. /// <value>The color of the back.</value>
  344. /// <PermissionSet>
  345. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  346. /// </PermissionSet>
  347. public new Color BackColor
  348. {
  349. get
  350. {
  351. return Color.FromArgb(base.BackColor.R, base.BackColor.G, base.BackColor.B);
  352. }
  353. set
  354. {
  355. myBackColor = value;
  356. base.BackColor = value;
  357. myUpToDate = false;
  358. }
  359. }
  360. /// <summary>
  361. /// 获取或设置一个值,该值指示此控件是否为多行 <see cref="T:System.Windows.Forms.TextBox" /> 控件。
  362. /// </summary>
  363. /// <value><c>true</c> if multiline; otherwise, <c>false</c>.</value>
  364. /// <PermissionSet>
  365. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  366. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  367. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  368. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  369. /// </PermissionSet>
  370. public override bool Multiline
  371. {
  372. get { return base.Multiline; }
  373. set
  374. {
  375. if (this.myPaintedFirstTime)
  376. this.SetStyle(ControlStyles.UserPaint, false);
  377. base.Multiline = value;
  378. if (this.myPaintedFirstTime)
  379. this.SetStyle(ControlStyles.UserPaint, true);
  380. this.myBitmap = null;
  381. this.myAlphaBitmap = null;
  382. myUpToDate = false;
  383. this.Invalidate();
  384. }
  385. }
  386. #endregion //end public property overrides
  387. #region private functions and classes
  388. /// <summary>
  389. /// Gets the height of the font.
  390. /// </summary>
  391. /// <returns>System.Int32.</returns>
  392. private int GetFontHeight()
  393. {
  394. Graphics g = this.CreateGraphics();
  395. SizeF sf_font = g.MeasureString("X", this.Font);
  396. g.Dispose();
  397. return (int)sf_font.Height;
  398. }
  399. /// <summary>
  400. /// Gets the bitmaps.
  401. /// </summary>
  402. private void GetBitmaps()
  403. {
  404. if (myBitmap == null
  405. || myAlphaBitmap == null
  406. || myBitmap.Width != Width
  407. || myBitmap.Height != Height
  408. || myAlphaBitmap.Width != Width
  409. || myAlphaBitmap.Height != Height)
  410. {
  411. myBitmap = null;
  412. myAlphaBitmap = null;
  413. }
  414. if (myBitmap == null)
  415. {
  416. myBitmap = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);//(Width,Height);
  417. myUpToDate = false;
  418. }
  419. if (!myUpToDate)
  420. {
  421. //Capture the TextBox control window
  422. this.SetStyle(ControlStyles.UserPaint, false);
  423. win32.CaptureWindow(this, ref myBitmap);
  424. this.SetStyle(ControlStyles.UserPaint, true);
  425. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  426. this.BackColor = Color.FromArgb(myBackAlpha, myBackColor);
  427. }
  428. //--
  429. Rectangle r2 = new Rectangle(0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height);
  430. ImageAttributes tempImageAttr = new ImageAttributes();
  431. //Found the color map code in the MS Help
  432. ColorMap[] tempColorMap = new ColorMap[1];
  433. tempColorMap[0] = new ColorMap();
  434. tempColorMap[0].OldColor = Color.FromArgb(255, myBackColor);
  435. tempColorMap[0].NewColor = Color.FromArgb(myBackAlpha, myBackColor);
  436. tempImageAttr.SetRemapTable(tempColorMap);
  437. if (myAlphaBitmap != null)
  438. myAlphaBitmap.Dispose();
  439. myAlphaBitmap = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);//(Width,Height);
  440. Graphics tempGraphics1 = Graphics.FromImage(myAlphaBitmap);
  441. tempGraphics1.DrawImage(myBitmap, r2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, GraphicsUnit.Pixel, tempImageAttr);
  442. tempGraphics1.Dispose();
  443. //----
  444. if (this.Focused && (this.SelectionLength == 0))
  445. {
  446. Graphics tempGraphics2 = Graphics.FromImage(myAlphaBitmap);
  447. if (myCaretState)
  448. {
  449. //Draw the caret
  450. Point caret = this.findCaret();
  451. Pen p = new Pen(this.ForeColor, 3);
  452. tempGraphics2.DrawLine(p, caret.X + 2, caret.Y + 0, caret.X + 2, caret.Y + myFontHeight);
  453. tempGraphics2.Dispose();
  454. }
  455. }
  456. }
  457. /// <summary>
  458. /// Finds the caret.
  459. /// </summary>
  460. /// <returns>Point.</returns>
  461. private Point findCaret()
  462. {
  463. /* Find the caret translated from code at
  464. * http://www.vb-helper.com/howto_track_textbox_caret.html
  465. *
  466. * and
  467. *
  468. * http://www.microbion.co.uk/developers/csharp/textpos2.htm
  469. *
  470. * Changed to EM_POSFROMCHAR
  471. *
  472. * This code still needs to be cleaned up and debugged
  473. * */
  474. Point pointCaret = new Point(0);
  475. int i_char_loc = this.SelectionStart;
  476. IntPtr pi_char_loc = new IntPtr(i_char_loc);
  477. int i_point = win32.SendMessage(this.Handle, win32.EM_POSFROMCHAR, pi_char_loc, IntPtr.Zero);
  478. pointCaret = new Point(i_point);
  479. if (i_char_loc == 0)
  480. {
  481. pointCaret = new Point(0);
  482. }
  483. else if (i_char_loc >= this.Text.Length)
  484. {
  485. pi_char_loc = new IntPtr(i_char_loc - 1);
  486. i_point = win32.SendMessage(this.Handle, win32.EM_POSFROMCHAR, pi_char_loc, IntPtr.Zero);
  487. pointCaret = new Point(i_point);
  488. Graphics g = this.CreateGraphics();
  489. String t1 = this.Text.Substring(this.Text.Length - 1, 1) + "X";
  490. SizeF sizet1 = g.MeasureString(t1, this.Font);
  491. SizeF sizex = g.MeasureString("X", this.Font);
  492. g.Dispose();
  493. int xoffset = (int)(sizet1.Width - sizex.Width);
  494. pointCaret.X = pointCaret.X + xoffset;
  495. if (i_char_loc == this.Text.Length)
  496. {
  497. String slast = this.Text.Substring(Text.Length - 1, 1);
  498. if (slast == "\n")
  499. {
  500. pointCaret.X = 1;
  501. pointCaret.Y = pointCaret.Y + myFontHeight;
  502. }
  503. }
  504. }
  505. return pointCaret;
  506. }
  507. /// <summary>
  508. /// Handles the Tick event of the myTimer1 control.
  509. /// </summary>
  510. /// <param name="sender">The source of the event.</param>
  511. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  512. private void myTimer1_Tick(object sender, EventArgs e)
  513. {
  514. //Timer used to turn caret on and off for focused control
  515. myCaretState = !myCaretState;
  516. myCaretUpToDate = false;
  517. this.Invalidate();
  518. }
  519. /// <summary>
  520. /// Class uPictureBox.
  521. /// Implements the <see cref="System.Windows.Forms.PictureBox" />
  522. /// </summary>
  523. /// <seealso cref="System.Windows.Forms.PictureBox" />
  524. private class uPictureBox : PictureBox
  525. {
  526. /// <summary>
  527. /// Initializes a new instance of the <see cref="uPictureBox" /> class.
  528. /// </summary>
  529. public uPictureBox()
  530. {
  531. this.SetStyle(ControlStyles.Selectable, false);
  532. this.SetStyle(ControlStyles.UserPaint, true);
  533. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  534. this.SetStyle(ControlStyles.DoubleBuffer, true);
  535. this.Cursor = null;
  536. this.Enabled = true;
  537. this.SizeMode = PictureBoxSizeMode.Normal;
  538. }
  539. //uPictureBox
  540. /// <summary>
  541. /// 处理 Windows 消息。
  542. /// </summary>
  543. /// <param name="m">要处理的 Windows<see cref="T:System.Windows.Forms.Message" />。</param>
  544. protected override void WndProc(ref Message m)
  545. {
  546. if (m.Msg == win32.WM_LBUTTONDOWN
  547. || m.Msg == win32.WM_RBUTTONDOWN
  548. || m.Msg == win32.WM_LBUTTONDBLCLK
  549. || m.Msg == win32.WM_MOUSELEAVE
  550. || m.Msg == win32.WM_MOUSEMOVE)
  551. {
  552. //Send the above messages back to the parent control
  553. win32.PostMessage(this.Parent.Handle, (uint)m.Msg, m.WParam, m.LParam);
  554. }
  555. else if (m.Msg == win32.WM_LBUTTONUP)
  556. {
  557. //?? for selects and such
  558. this.Parent.Invalidate();
  559. }
  560. base.WndProc(ref m);
  561. }
  562. } // End uPictureBox Class
  563. #endregion // end private functions and classes
  564. #region Component Designer generated code
  565. /// <summary>
  566. /// Required method for Designer support - do not modify
  567. /// the contents of this method with the code editor.
  568. /// </summary>
  569. private void InitializeComponent()
  570. {
  571. components = new System.ComponentModel.Container();
  572. }
  573. #endregion
  574. #region New Public Properties
  575. /// <summary>
  576. /// Gets or sets the back alpha.
  577. /// </summary>
  578. /// <value>The back alpha.</value>
  579. [
  580. Category("Appearance"),
  581. Description("The alpha value used to blend the control's background. Valid values are 0 through 255."),
  582. Browsable(true),
  583. DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)
  584. ]
  585. public int BackAlpha
  586. {
  587. get { return myBackAlpha; }
  588. set
  589. {
  590. int v = value;
  591. if (v > 255)
  592. v = 255;
  593. myBackAlpha = v;
  594. myUpToDate = false;
  595. Invalidate();
  596. }
  597. }
  598. #endregion
  599. } // End AlphaTextBox Class
  600. }