When the automatic recognition window theme

These two days to do a screenshot of automatic identification window. The final requirement effect: the window of the current cursor location with a color frame up, select the region and click.
Implementation steps:
use EnumWindows function through all windows on the desktop, use this function traversed get hundreds of windows, own a total of only fired three, four windows, even to traverse so many, can only be screened, will not window needs to be filtered.

//在主函数中调用该函数
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, NULL, WindowTitle);//根据窗体名字找到对应句柄,这里重复了,只是熟悉一下这个函数
        if (hq == NULL) { return TRUE; }
        RECT rect;
        GetWindowRect(hq, &rect);//获取该句柄所掌控的区域
        //这里可以做一些自己需要的操作
        return true;
        }
        //这里也可以做一些操作
        return true;
        }

EnumWindows function will traverse all the windows on the desktop, EnumWindowsProc when the callback function returns false, terminate traversal, returns true continue to traverse the rest. When you find the window you want, you can return false, reduce the complexity of the function, incidentally, reduce operational point of time. Because I need to get all the windows, and finally to match the area where the handle according to the mouse position, so I always use the return true.
After we get to see the see the desktop window handle and the area corresponding to get the current mouse position, traversing the handle, the handle to determine the current mouse in which control of the region, the use of GetCursorPos (p), p here is the type POINT . Then call the following function:

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 is a structure of its own definition:

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;

It has been able to return to the area where, according to the mouse position, then start drawing can be friends ~

Guess you like

Origin www.cnblogs.com/ymd12103410/p/11240772.html