-1

我正在尝试动态地从其他窗口获取文本(如果我在该窗口的文本字段中写了一些东西然后启动我的程序,我必须看到我写的内容)。所以如果我使用 getWindowText,它会给我一个静态初始化的文本框。所以这就是问题所在。它与 spy++ 的作用类似。这是我所做的代码示例:

#include <Windows.h>
#include <vector>
#include <iostream>
#include <string>
#include <conio.h> 
using namespace std;

int main() 
{
HWND hWnd;
MSG msg;
vector<HWND> a;
hWnd = FindWindow( NULL, "SomeList" );
vector<string> phrases;
char p[100];
if( !hWnd )
{
    cout << "Window hasn't been found " << endl;
    _getch();
    exit( 1 );
}

hWnd = GetWindow(hWnd, GW_CHILD);
while (hWnd !=0)
{
     hWnd = GetWindow(hWnd, GW_HWNDNEXT);
     GetClassName( hWnd, p, 10 );
     string k( p );
     if( k == "Edit" )
         a.push_back( hWnd );
     GetWindowText(hWnd,p,100);
      cout << p << endl;
}
phrases.resize( a.size() );

for( auto i = a.begin();i != a.end();i++ )
{
    int index = 0;
    GetWindowText( *i,p, 10 );
    string n( p );
    if( n.size() != 0 )
    {
        phrases[index] =  n;
        index++;
    }
}
_getch();
return 0;
}
4

1 回答 1

1

GetWindowText文档:

要在另一个进程中检索控件的文本,请直接发送 WM_GETTEXT 消息,而不是调用 GetWindowText。

例子:

HWND hWndEdit;

[....]

char szText[ 128 ] = { 0 };
int cbCopied = SendMessage( hWndEdit, WM_GETTEXT, (WPARAM)sizeof( szText ),
                            (LPARAM)szText );
于 2013-12-07T23:17:35.637 回答