[WinForm]为TextBox设置水印文字

2020-12-13 06:07

阅读:450

标签:winform   style   blog   http   color   os   for   2014   

关键代码:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WinFormUtilHelpV2
{
    /// 
    /// 基于.NET 2.0的TextBox工具类
    /// 
    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);

        /// 
        /// 为TextBox设置水印文字
        /// 
        /// TextBox
        /// 水印文字
        public static void SetWatermark(this TextBox textBox, string watermark)
        {
            SendMessage(textBox.Handle, EM_SETCUEBANNER, 0, watermark);
        }
        /// 
        /// 清除水印文字
        /// 
        /// TextBox
        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();
        }
    }
}

测试效果:

soscw.com,搜素材

希望有所帮助!

[WinForm]为TextBox设置水印文字,搜素材,soscw.com

[WinForm]为TextBox设置水印文字

标签:winform   style   blog   http   color   os   for   2014   

原文地址:http://www.cnblogs.com/Yan-Zhiwei/p/3896299.html


评论


亲,登录后才可以留言!