GetWindowText不起作用

时间:2015-11-06 14:02:22

标签: winapi vb6

我尝试使用GetWindowText和GetWindowTextLength从EDIT控件中检索文本。应用程序从光标下的窗口中检索文本,它可以在带有标题或文本的所有窗口上工作,但EDIT控件除外。 EDIT控件是Windows XP Calculator,calc.exe上的结果窗口。

Dim S As String
Dim L As Long

L = GetWindowTextLength(handle) + 1

Receiving string = GetWindowText(handle, S, L)

编辑:

根据SPY ++,Edit类控件不会收到EM_GETSELTEXT或WM_GETTEXT消息。每次按下UI上的按钮时,下面的代码都会从Windows XP calc.exe计算器上的Edit类控件中检索文本。这不是我希望使用的方法,但是,它完成了我的任务。

    Const EM_SETSEL = &HB1
    Const ES_READONLY = &H800
    Const WM_COPY = &H301
    Const EM_GETSELTEXT = &H43E
    Const WM_GETTEXTLENGTH = &HE
    Const WM_SETFOCUS As Long = &H7

    Dim L As Long

    L = SendMessage(EditHwnd, WM_GETTEXTLENGTH, 0&, 0)

    SendMessage EditHwnd, WM_SETFOCUS, 0&, 0

    SendMessage EditHwnd, EM_SETSEL, 0&, L

    SendMessage EditHwnd, ES_READONLY, 0&, 0 ' read only = false

    Clipboard.Clear

    SendMessage EditHwnd, WM_COPY, 0&, 0

    SendMessage EditHwnd, ES_READONLY, 1&, 0 ' read only = true

    Receiving string = Clipboard.GetText

    Clipboard.Clear

2 个答案:

答案 0 :(得分:0)

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Function GetText(handle As Long) As String
    Dim S As String, L As Integer, cch As Long
    L = GetWindowTextLength(handle) + 1
    S = String(L, 0)
    GetText = Mid(S, 1, GetWindowText(handle, S, L))
End Function


Private Sub Form_Load()
    Dim hw As Long
    hw = Me.hwnd
    MsgBox GetText(hw)
End Sub

但它不适用于像EDIT这样的控件,因为它是在帮助中编写的。 ;(为了获取子窗口(控件)的文本,尝试使用API​​ EnumWindows / EnumThreadWindows / GetWindowThreadProcessId获取所有窗口的列表,以找到所需的控件。

您需要编辑的课程名称是什么?它的宽度和高度是多少?我可以专门为搜索这个控件编写代码。

答案 1 :(得分:0)

此代码在Windows XP(虚拟机)中运行良好

Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As Any, ByVal lpsz2 As Any) As Long

Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long

Private Declare Function AttachThreadInput Lib "user32.dll" _
(ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long

Private Const WM_GETTEXT As Long = &HD&
Private Const WM_GETTEXTLENGTH As Long = &HE&

Function WindowText(ByVal hWnd As Long) As String
    Dim ret As Long
    ret = SendMessage(hWnd, WM_GETTEXTLENGTH, 0, ByVal 0&)
    WindowText = String(ret, 0)
    ret = SendMessage(hWnd, WM_GETTEXT, ret + 1, ByVal WindowText)
End Function

Private Sub Command1_Click()
    Dim hCalc As Long, hEdit As Long
    hCalc = FindWindow("SciCalc", vbNullString)
    hEdit = FindWindowEx(hCalc, 0&, "Edit", vbNullString)
    MsgBox WindowText(hEdit)
End Sub