WaterTextBox.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. namespace LYFZ.OtherExpansion.SkinControl
  6. {
  7. [ToolboxBitmap(typeof(TextBox))]
  8. public class WaterTextBox : TextBox
  9. {
  10. private string _waterText = string.Empty;
  11. private Color _waterColor = Color.FromArgb(127, 127, 127);
  12. [Category("Skin"), Description("水印文字")]
  13. public string WaterText
  14. {
  15. get
  16. {
  17. return this._waterText;
  18. }
  19. set
  20. {
  21. this._waterText = value;
  22. base.Invalidate();
  23. }
  24. }
  25. [Category("Skin"), Description("水印的颜色")]
  26. public Color WaterColor
  27. {
  28. get
  29. {
  30. return this._waterColor;
  31. }
  32. set
  33. {
  34. this._waterColor = value;
  35. base.Invalidate();
  36. }
  37. }
  38. private void WmPaintWater(ref Message m)
  39. {
  40. using (Graphics g = Graphics.FromHwnd(base.Handle))
  41. {
  42. //if (this.Text.Length == 0 && !string.IsNullOrEmpty(this._waterText) && !this.Focused)
  43. if (this.Text.Length == 0 && !string.IsNullOrEmpty(this._waterText))
  44. {
  45. TextFormatFlags flags = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter;
  46. if (this.RightToLeft == RightToLeft.Yes)
  47. {
  48. flags |= (TextFormatFlags.Right | TextFormatFlags.RightToLeft);
  49. }
  50. TextRenderer.DrawText(g, this._waterText, new Font("微软雅黑", 8.5f), base.ClientRectangle, this._waterColor, flags);
  51. }
  52. }
  53. }
  54. protected override void WndProc(ref Message m)
  55. {
  56. base.WndProc(ref m);
  57. if (m.Msg == 15)
  58. {
  59. this.WmPaintWater(ref m);
  60. }
  61. }
  62. }
  63. }