WinForm完成為TextBox設置水印文字功效。本站提示廣大學習愛好者:(WinForm完成為TextBox設置水印文字功效)文章只能為提供參考,不一定能成為您想要的結果。以下是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); } } }
測試代碼以下:
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(); } } }
測試後果以下圖所示:
願望本文所述的為TextBox設置水印文字功效示例對年夜家C#法式設計有所贊助!