Winform自定义控件-新建一个带水印的输入框(WatermarkTextBox)
public partial class WatermarkTextBox : 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);
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
}
|