截图时窗口自动识别
发布时间:2020-12-14 05:37:32 所属栏目:Windows 来源:网络整理
导读:这两天做的截图自动识别窗口。最终要求效果:将当前鼠标所在的窗口用颜色框框起来,单击后选中该区域。 实现步骤: 利用EnumWindows函数遍历了桌面上的所有窗口,用这个函数遍历完得到上百个窗口,自己总共才开了三、四个窗口,竟然能遍历到这么多,只能进行
这两天做的截图自动识别窗口。最终要求效果:将当前鼠标所在的窗口用颜色框框起来,单击后选中该区域。 //在主函数中调用该函数 EnumWindows(EnumWindowsProc,NULL); BOOL CaptureWindow::EnumWindowsProc(HWND hWnd,LPARAM lParam) { LONG gwl_style = GetWindowLong(hWnd,GWL_STYLE); if (!(gwl_style & WS_POPUP)&&GetParent(hWnd) == NULL && IsWindowVisible(hWnd)&&IsWindowEnabled(hWnd)) { //可以对过滤后所得到的窗口进行处理 //、、、 // EnumChildWindows(hWnd,EnumChildWindowsProc,NULL); //获取父窗口的所有子窗口 } return true; } BOOL CaptureWindow::EnumChildWindowsProc(HWND hWnd,LPARAM lParam) { LONG gwl_style = GetWindowLong(hWnd,GWL_STYLE); if (!(gwl_style & WS_POPUP) && GetParent(hWnd) == NULL && IsWindowVisible(hWnd) && IsWindowEnabled(hWnd)) { WCHAR WindowTitle[100] = { 0 }; ::GetWindowText(hWnd,WindowTitle,100);//获取句柄对应的窗体名字 HWND hq = FindWindowEx(hWnd,NULL,WindowTitle);//根据窗体名字找到对应句柄,这里重复了,只是熟悉一下这个函数 if (hq == NULL) { return TRUE; } RECT rect; GetWindowRect(hq,&rect);//获取该句柄所掌控的区域 //这里可以做一些自己需要的操作 return true; } //这里也可以做一些操作 return true; } EnumWindows函数会遍历桌面上所有的窗口,当回调函数EnumWindowsProc返回false时,终止遍历,返回true继续遍历剩下的。当找到你需要的窗口,可以返回false,降低函数复杂度,顺带减少点运行时间。因为我需要获取所有的窗口,最后根据鼠标位置来匹配句柄所在的区域,所以返回我都是用的true。 HCR CaptureWindow::getRect(const POINT pt) { HCR rect = 0; vector<Handle_Correspondence_Region>::iterator it = Handle_Correspondence_Region_vector.begin(); for (it; it != Handle_Correspondence_Region_vector.end(); it++) { if (it->left <= pt.x&&it->right >= pt.x&&it->top <= pt.y&&it->bottom >= pt.y) { return *it; } } return rect; } HCR是自己定义的一个结构体: typedef struct Handle_Correspondence_Region { Handle_Correspondence_Region(int p = 0) { hwd = NULL; }; HWND hwd; long int left; long int right; long int top; long int bottom; long int wide; long int high; bool operator <(const Handle_Correspondence_Region &m)const { return (wide*high) < (m.wide*m.high); } bool operator == (const Handle_Correspondence_Region &eq) const { return ((left == eq.left) && (right == eq.right) && (top == eq.top) && (bottom == eq.bottom)); } }HCR; 到这就已经能根据鼠标位置返回所在区域了,接下来开始绘制就可以啦~ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |