我試圖從其他窗口動態獲取文本(如果我在該窗口的文本字段中寫入內容,然後啓動我的程序,我必須看看我寫了什麼)。所以,如果我使用getWindowText ,它給了我一個靜態的初始化文本框。所以這就是問題所在。它與間諜++相似。 下面的代碼示例中,我做了什麼:從文本框動態獲取文本
#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;
}
有什麼實際問題? –
當我啓動我的程序時,它不會讀取我在文本框中寫入的文本,它會讀取已靜態初始化的文本。我需要從文本框中讀取文本,這些文本不是由SetWindowText初始化的,而是從我在窗口中寫入的內容中獲得的 – Psychoman
您正在爲窗口的第一個子項瞄準編輯控件嗎?如果是這樣,你會直接跳過它。 –