我尝试使用API函数,SendMessage和PostMessage以及使用Window Messages WM_SETTEXT WM_GETTEXTLENGTH和WM_GETTEXT自动化一些外部窗口的组合框控件。
当我运行代码时,我可以让它工作,例如按F11通过每一行或在某个点放入断点并运行它,但是当我正常运行它时,WM_SETTEXT消息不会更新组合框。
这似乎是代码运行速度的问题所以我的第一个解决方案是进行一段验证,确保文本在组合框中,但由于某种原因,WM_GETTEXT检索应该是的文本在文本框中,即使它不可见。
下面的代码是我尝试过的那种验证的片段,我的问题就是有任何等待,直到我可以用来确保文本实际输入的方法,我知道Sleep API但我理想的是喜欢能够确保文本存在并且不加载fx秒并且希望文本存在的东西。
任何想法都是适用的。
非常感谢
VB.net代码示例:
Private Enum WindowMessages
WM_SETTEXT = &HC
WM_GETTEXT = &HD
WM_GETTEXTLENGTH = &HE
End Enum
Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hWnd As Int32, ByVal wMsg As Int32, ByVal _
wParam As Int32, lParam As String) As Int32
Public Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hWnd
As Int32, _
ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As String) As Int32
Private Sub TestExample(ByVal hWndTextBox as Int32, ByVal TextString as String)
'####Using the hWnd of a Combo box control and a string to enter in the combo box.####
Dim TextLength as Int32
Dim ItemText as string
Do until ItemText = TextString
SendMessage(hWndTextBox, WM_SETTEXT, 0, TextString) > 0
TextLength = SendMessage(hWndTextBox, WM_GETTEXTLENGTH, 0, 0)
ItemText = Space(TextLength)
SendMessage(hWndTextBox, WM_GETTEXT, TextLen + 1, ItemText)
Loop
End Sub