C++ 如何从EnumChildWindow获取窗口矩形列表并存储它们?
我使用了EnumChildWindows,可以获得所有子窗口的C++ 如何从EnumChildWindow获取窗口矩形列表并存储它们?,c++,vector,rectangles,enumerate,childwindow,C++,Vector,Rectangles,Enumerate,Childwindow,我使用了EnumChildWindows,可以获得所有子窗口的句柄、类名和窗口文本。但是我还想得到所有子窗口的矩形。可能吗 这是我的代码: #include <iostream> #include <windows.h> using namespace std; BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { char class_name[100]; char title[100];
句柄
、类名
和窗口文本
。但是我还想得到所有子窗口的矩形
。可能吗
这是我的代码:
#include <iostream>
#include <windows.h>
using namespace std;
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
char class_name[100];
char title[100];
GetClassNameA(hwnd,class_name, sizeof(class_name));
GetWindowTextA(hwnd,title,sizeof(title));
cout <<"Window name : "<<title<<endl;
cout <<"Class name : "<<class_name<<endl;
cout <<"hwnd : "<<hwnd<<endl<<endl;
return TRUE;
}
int main()
{
POINT pt;
Sleep(3000);
GetCursorPos(&pt);
HWND hwnd = WindowFromPoint(pt);
HWND hwndParent = GetParent(hwnd);
EnumChildWindows(hwndParent,EnumWindowsProc,0);
system("PAUSE");
return 0;
}
#包括
#包括
使用名称空间std;
BOOL回调EnumWindowsProc(HWND-HWND,LPARAM-LPARAM)
{
字符类名称[100];
字符标题[100];
GetClassNameA(hwnd,class_name,sizeof(class_name));
GetWindowTextA(hwnd,title,sizeof(title));
cout关于你的第一个问题,你可以看一下
对于第二个问题,您可以使用所需的字段定义结构,例如
#include <iostream>
#include <windows.h>
#include <vector>
struct MyWindow {
HWND handle_;
std::string name_;
std::string text_;
RECT rect_;
MyWindow(HWND handle, const std::string& name, const std::string& text, RECT rect): handle_{handle}, name_{name}, text_{text}, rect_{rect}{}
};
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
char name[100];
char text[100];
RECT rect{0};
GetClassNameA(hwnd,name, sizeof(name));
GetWindowTextA(hwnd,text,sizeof(text));
GetWindowRect(hwnd, &rect);
std::vector<MyWindow>* myVec = reinterpret_cast<std::vector<MyWindow>*>(lParam);
if (myVec) {
myVec->emplace_back(MyWindow(hwnd, std::string(name), std::string(text), rect));
} else {
// Pointer to vector is NULL, handle accordingly
return FALSE;
}
return TRUE;
}
int main()
{
POINT pt;
Sleep(3000);
std::vector<MyWindow> myChildWindows;
GetCursorPos(&pt);
HWND hwnd = WindowFromPoint(pt);
HWND hwndParent = GetParent(hwnd);
EnumChildWindows(hwndParent, EnumWindowsProc, reinterpret_cast<LPARAM>(&myChildWindows));
for (const auto & childWindow: myChildWindows) {
std::cout <<"Window name : "<< childWindow.name_ << std::endl;
std::cout <<"Class name : "<< childWindow.text_<< std::endl;
std::cout <<"hwnd : "<< childWindow.handle_<< std::endl;
std::cout << "Rect: " << childWindow.rect_.left << "/" << childWindow.rect_.right << std::endl;
std::cout << "---" << std::endl;
}
system("PAUSE");
return 0;
}
#包括
#包括
#包括
结构MyWindow{
HWND手柄;
std::字符串名称;
std::字符串文本;
RECT RECT_u2;;
MyWindow(HWND句柄,const std::string&name,const std::string&text,RECT RECT):句柄{handle},名称{name},文本{text},RECT{RECT}
};
BOOL回调EnumWindowsProc(HWND-HWND,LPARAM-LPARAM)
{
字符名[100];
字符文本[100];
RECT RECT{0};
GetClassNameA(hwnd,name,sizeof(name));
GetWindowTextA(hwnd,text,sizeof(text));
GetWindowRect(hwnd和&rect);
std::vector*myVec=reinterpret_cast(lParam);
如果(myVec){
myVec->emplace_back(MyWindow(hwnd,std::string(name),std::string(text),rect));
}否则{
//指向向量的指针为空,请相应地处理
返回FALSE;
}
返回TRUE;
}
int main()
{
点pt;
睡眠(3000);
std::向量myChildWindows;
GetCursorPos(&pt);
HWND HWND=窗口起点(pt);
HWND hwndpress=GetParent(HWND);
EnumChildWindows(hwndParent、EnumWindowsProc、reinterpret_cast(&myChildWindows));
用于(const auto&childWindow:myChildWindows){
std::cout Hi,对于第一个问题,我能够使用GetWindowRect
获取我只指向的窗口的Rect
。我试图获取并显示它们,作为与其他窗口类似的列表(windowname、classname、hwnd)。对于第二个问题,是否可以让它更详细?很抱歉,我是std::vector
新手,不知道如何实现您的示例。谢谢,我未能编译它,因为MyWindow(HWND handle,const std::string&name,const std::string&text,RECT RECT):handle{handle},name{name},text{text},RECT}}
错误是语法错误:“,”和“{”前面的意外标记;跳过RECT RECT{0}的外观函数体;
错误为“rect”:本地函数定义是非法的。还有其他错误……您如何编译它?我正在使用VisualStudio Win32 Project来编译它?您确定代码中没有输入错误吗?我使用cygwin和Visual Studio代码编译它,两次都可以