winform设置textbox设置水印

1、新建自定义控件

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

namespace takelinks
{
    public partial class WatermarkTextBox : TextBox//修改为继承自TextBox
    {
        public WatermarkTextBox()
        {
            InitializeComponent();
        }
        private const uint ECM_FIRST = 0x1500;
        private const uint EM_SETCUEBANNER = ECM_FIRST + 1;

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);

        private string watermarkText;
        public string WatermarkText
        {
            get { return watermarkText; }
            set
            {
                watermarkText = value;
                SetWatermark(watermarkText);
            }
        }

        private void SetWatermark(string watermarkText)
        {
            SendMessage(this.Handle, EM_SETCUEBANNER, 0, watermarkText);
        }

    }
}
View Code

2、新建的控件除了textbox的功能外还增添了水印的功能

运用

 textBox1.WatermarkText = "请输入链接,可以拖拽!";


完!

 

posted @ 2017-09-19 16:38  冲天小肥牛  阅读(692)  评论(0编辑  收藏  举报