1

如果我在 vb.net winform 上使用下面显示的代码,则会出现提示横幅/水印并按预期运行(Win7 Pro 32 位 VS2008 和 64 位 VS2010)。但是,当在 vb.net 用户控件中使用相同样式的代码时,不会显示水印。有人有任何线索吗?

几个小时后......这看起来像PEBKAC。在测试应用程序中工作。与用户控件。在设计时创建的和在运行时加载的,仍然不能在主应用程序中工作。尽管。还是很疑惑。还在寻找线索。

' Call from form / usercontrol load event handler.
Userhint.WatermarkSet(textbox1, "Some arbitrary text.")

' Noddy library class.
Friend Class Userhint

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

Public Shared Sub WatermarkSet(ByVal ctl As Control, _
                               byval hintText as string)

  const EM_SETCUEBANNER as int32 = &h1501
  dim retainOnFocus As IntPtr = new IntPtr(1)

  SendMessage(New HandleRef(ctl, ctl.Handle), _
              EM_SETCUEBANNER, _
              retainOnFocus, _
              hintText)

End sub

End Class
4

1 回答 1

2

与其说是 PEBKAC,不如说是另一个 M$ 没有像他们可能的那样记录事物的实例。

简短的回答是在 Run 方法之前调用 Application.EnableVisualStyles() 。

Application.EnableVisualStyles()
Application.Run()

有关更多信息,请参阅本网站上的 questions/7518894/sendmessage-doesnt-work-in-one-project。

于 2012-12-01T07:51:47.783 回答