如何创建带水印的文本框?

How to create a watermark TextBox?

我在Watermark TextBox in WinForms but it is C# version, so I use http://converter.telerik.com/中找到代码,将代码转换为VB版本,但仍然报错......

这一行发生错误:-

SendMessage(Me.Handle, &H1501, DirectCast(1, IntPtr), mCue)

我该如何解决?

ERROR MESSAGE: BC30311 Value of type 'Integer' cannot be converted to 'IntPtr'.

Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Class CueTextBox
    Inherits TextBox
    <Localizable(True)> _
    Public Property Cue() As String
        Get
            Return mCue
        End Get
        Set
            mCue = value
            updateCue()
        End Set
    End Property

    Private Sub updateCue()
        If Me.IsHandleCreated AndAlso mCue IsNot Nothing Then
            SendMessage(Me.Handle, &H1501, DirectCast(1, IntPtr), mCue)   'this line get the error msg
        End If
    End Sub
    Protected Overrides Sub OnHandleCreated(e As EventArgs)
        MyBase.OnHandleCreated(e)
        updateCue()
    End Sub
    Private mCue As String

    ' PInvoke
    <DllImport("user32.dll", CharSet := CharSet.Unicode)> _
    Private Shared Function SendMessage(hWnd As IntPtr, msg As Integer, wp As IntPtr, lp As String) As IntPtr
    End Function
End Class

根据 PInvoke.net VB.Net 中 SendMessage 的正确签名是:

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function

无论如何,问题出在这一行:

SendMessage(Me.Handle, &H1501, DirectCast(1, IntPtr), mCue)  

尝试将其替换为:

SendMessage(Me.Handle, &H1501, New IntPtr(1)), mCue)