VB.NET - GetWindowText() 为"edit"类返回零



我正试图通过应用程序的控制句柄从另一个应用程序检索文本。如果控件是"静态"的,我没有问题,但我的代码似乎不适用于"编辑"控件。正如MSDN所说,GetWindowText无法从EDIT控件中检索文本,但也许您知道实现这一点的另一种方法?

我当前的代码在这里:

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通过指定父句柄、类名(静态、编辑或按钮)和classPosition(在循环中使用-目前不重要)返回控件的句柄

正如我所说,它与STATIC(标签等)配合得很好,但如果我从外部应用程序的EDIT(文本框)控件中检索文本,它将返回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

最新更新