我正在尝试动态地从其他窗口获取文本(如果我在该窗口的文本字段中写了一些东西然后启动我的程序,我必须看到我写的内容)。所以如果我使用 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;
}