您的位置:首页 > 编程语言 > C#

为textbox控件添加水印

2015-09-30 10:09 302 查看
在工程中新建一个类内容如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Runtime.InteropServices;

using System.Windows.Forms;

namespace LLSbasicdlg

{

   public static class textwater

    {

        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);

        }

    }

}

工程中其他项目都可以使用该方法进行设置,要求.net框架为4.0;

引用方法为this.textbox1.SetWatermark(“测试成功");

另一种适用于非.net4.0

控件初始化添加事件

在textbox上面添加一个标签控件

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# 水印 textbox