1.通过spy++获取想要获取控件的句柄id
通过spy++获取另一个软件的文本框的句柄
2.Qt写代码,
根据句柄获取文本框的内容
void getTextFromExternalWindow(HWND hwnd)
{
const int bufferSize = 256;
TCHAR buffer[bufferSize];
// 获取窗口文本内容
int length = GetWindowText(hwnd, buffer, bufferSize);
if (length > 0)
{
QString text = QString::fromWCharArray(buffer);
qDebug() << "Text from external window:" << text;
}
else
{
qDebug() << "Failed to get text from external window.";
}
}
通过WindowsAPI查找窗口,然后查找子窗口,然后输入获取的文本框句柄,需要user32.dll
#include <windows.h>
HWND hwnd = FindWindow(nullptr, L"Control Center Series 30");
if (hwnd != nullptr)
{
HWND hwndChild = FindWindowEx(hwnd, NULL,NULL, L"Devices"); // 获取子窗口句柄
if (hwndChild == NULL) {
qDebug() << "Child window not found!\n";
return 1;
}
// 假设你有一个句柄的数值
intptr_t handleValue = 0x006F044A; // 这里假设句柄值为 00020574,实际情况下会根据具体需求修改auto (250)
// 将句柄数值转换为 HWND 类型
HWND hwnd1 = reinterpret_cast<HWND>(handleValue);
getTextFromExternalWindow(hwnd1);
}
else
{
qDebug() << "External window not found.";
}
3.输出结果