TextBox 文本框水印文字

 1     #region TextBox 文本框水印文字
 2     /// <summary>
 3     /// 基于.NET 2.0的TextBox工具类
 4     /// </summary>
 5     public static class TextBoxToolV2 {
 6         private const int EM_SETCUEBANNER = 0x1501;
 7         [DllImport("user32.dll", CharSet = CharSet.Auto)]
 8 
 9         private static extern Int32 SendMessage
10          (IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
11 
12         /// <summary>
13         /// 为TextBox设置水印文字
14         /// </summary>
15         /// <param name="textBox">TextBox</param>
16         /// <param name="watermark">水印文字</param>
17         public static void SetWatermark(this TextBox textBox, string watermark) {
18             SendMessage(textBox.Handle, EM_SETCUEBANNER, 0, watermark);
19         }
20         /// <summary>
21         /// 清除水印文字
22         /// </summary>
23         /// <param name="textBox">TextBox</param>
24         public static void ClearWatermark(this TextBox textBox) {
25             SendMessage(textBox.Handle, EM_SETCUEBANNER, 0, string.Empty);
26         }
27     }
28     
29     #endregion

 

你可能感兴趣的:(TextBox 文本框水印文字)