VB.NET-GetWindowText()为“0”返回零;编辑";班
我正试图通过应用程序的控制手柄从另一个应用程序中检索文本。如果控件是“静态”的,我没有问题,但我的代码似乎不适用于“编辑”控件。正如MSDN所说,GetWindowText无法从编辑控件检索文本,但也许您知道实现这一点的另一种方法 我目前的代码如下:VB.NET-GetWindowText()为“0”返回零;编辑";班,vb.net,winapi,external,handle,sendmessage,Vb.net,Winapi,External,Handle,Sendmessage,我正试图通过应用程序的控制手柄从另一个应用程序中检索文本。如果控件是“静态”的,我没有问题,但我的代码似乎不适用于“编辑”控件。正如MSDN所说,GetWindowText无法从编辑控件检索文本,但也许您知道实现这一点的另一种方法 我目前的代码如下: Dim newHwnd As IntPtr = Handler.GetClassByPosition(ParentHwnd, cls, classPosition) Dim length As Integer = Handler.GetWi
Dim newHwnd As IntPtr = Handler.GetClassByPosition(ParentHwnd, cls, classPosition)
Dim length As Integer = Handler.GetWindowTextLength(newHwnd)
Dim sb As New String(" ".Chars(0), length + 1)
If cls = "edit" Then
Handler.GetWindowText(newHwnd, sb, sb.Length)
End If
其中GetClassByPosition通过指定父句柄、类名(静态、编辑或按钮)和类位置(在循环中使用-目前不重要)返回控件的句柄
正如我所说,它对静态(标签等)非常有效,但如果我从外部应用程序的编辑(textbox)控件检索文本,它将返回0
更新:
我尝试了以下解决方案,如果数据为整数,则成功返回数据,但如果数据包含任何字母,则结果为0:
Dim tmpstr As IntPtr = Marshal.AllocHGlobal(100)
Dim NumText = API.SendMessage(Hwnd, API.WM_GETTEXT, 200, tmpstr )
NumText = Marshal.PtrToStringAnsi(tmpstr )
Return NumText
提前感谢,,
尼古拉试试这个:
Private Function GetText(ByVal hWnd As IntPtr) As String
Dim ReturnValue As String = Nothing
If hWnd.ToInt32 > 0 Then
Dim Length As Integer = GetWindowTextLength(hWnd)
If Length > 0 Then
Dim sb As New System.Text.StringBuilder("", Length + 1)
GetWindowText(hWnd, sb, sb.Capacity)
ReturnValue = sb.ToString
sb = Nothing
End If
End If
Return ReturnValue
End Function
使用ArgumentException,总是比不可避免的NRE好。@Rob现在不是得到零,我什么也得不到