如何使用C#水印System.Windows.Forms.TextBox?

Use*_*404 0 c# watermark winforms

我想用c#在我的Windows窗体的文本框中使用水印?

我在stackoverflow中找到了这个链接.但我真的无法弄清楚如何在我的Windows应用程序中使用.

class WatermarkTextBox : TextBox
{
    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);
    }       

}
Run Code Online (Sandbox Code Playgroud)

请帮助如何使用SendMessage方法或建议我使用水印的任何其他(简单)方法.

小智 5

您必须在项目中创建一个新类TextBoxWatermarkExtensionMethod,然后才能SetWatermark(string watermarkText)文本框中使用该方法