首页 > 代码库 > [WinForm]为TextBox设置水印文字

[WinForm]为TextBox设置水印文字

关键代码:

using System;using System.Runtime.InteropServices;using System.Windows.Forms;namespace WinFormUtilHelpV2{    /// <summary>    /// 基于.NET 2.0的TextBox工具类    /// </summary>    public static class TextBoxToolV2    {        private const int EM_SETCUEBANNER = 0x1501;        [DllImport("user32.dll", CharSet = CharSet.Auto)]        private static extern Int32 SendMessage          (IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);        /// <summary>        /// 为TextBox设置水印文字        /// </summary>        /// <param name="textBox">TextBox</param>        /// <param name="watermark">水印文字</param>        public static void SetWatermark(this TextBox textBox, string watermark)        {            SendMessage(textBox.Handle, EM_SETCUEBANNER, 0, watermark);        }        /// <summary>        /// 清除水印文字        /// </summary>        /// <param name="textBox">TextBox</param>        public static void ClearWatermark(this TextBox textBox)        {            SendMessage(textBox.Handle, EM_SETCUEBANNER, 0, string.Empty);        }    }}

<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>测试代码:

using System;using System.Windows.Forms;using WinFormUtilHelpV2;namespace WinFormUtilHelpV2Test{    public partial class WinTextBoxToolV2Test : Form    {        public WinTextBoxToolV2Test()        {            InitializeComponent();        }        private void WinTextBoxToolV2Test_Load(object sender, EventArgs e)        {            textBox1.SetWatermark("请输入用户名称....");            textBox2.SetWatermark("请输入用户密码....");        }        private void button1_Click(object sender, EventArgs e)        {            textBox1.ClearWatermark();            textBox2.ClearWatermark();        }    }}

<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>测试效果:

image

希望有所帮助!