如何根据父窗口句柄,加上POINT位置,能够获取到指定位置的窗口句柄?如某窗口:
 我已经根据窗口标题获取了窗口句柄hParent,// 父窗口句柄,
我如何才能获得红线框的控件句柄呢,只有获得他,我才能操作数据哦。
HWND CMouseHook::GetDesWinFromPoint(HWND hParent,// 父窗口句柄
POINT ppoint, // 当前鼠标指针结构
UINT Flags // 忽略操作
)
{
// if(hwndParent != NULL)
// return ::ChildWindowFromPointEx(hParent, point, uFlags);
// //返回光标(point)所在点的子窗口句柄

// Find the smallest "window" still containing the point
// Doing this prevents us from stopping at the first window containing the point
RECT rect, rectSearch;
HWND pWnd, hWnd, hSearchWnd;

// ::SetCursorPos(ppoint.x,ppoint.y);
// hWnd = ::WindowFromPoint(ppoint);//得到光标(point)所在点的窗口句柄
hWnd = hParent;
if(hWnd != NULL)
{
// Get the size and parent for compare later
::GetWindowRect(hWnd, &rect); //得到整个窗口在屏幕上的矩形框位置
pWnd = ::GetParent(hWnd);       //得到父窗口句柄

// We only search further if the window has a parent
if(pWnd != NULL)
{
// Search from the window down in the Z-Order
hSearchWnd = hWnd;
do{
//如果再也找不到这样的窗口,该函数就会返回NULL
hSearchWnd = ::GetWindow(hSearchWnd, GW_CHILD);//GW_HWNDNEXT
// GetWindow得到和句柄为hSearchWnd(即首次循环为hWnd)的窗口相关的窗口,
// 其关系由GW_HWNDNEXT决定,这里是寻找兄弟窗口

// Does the search window also contain the point, have the same parent, and is visible?
::GetWindowRect(hSearchWnd, &rectSearch);
if(::PtInRect(&rectSearch, ppoint) && ::GetParent(hSearchWnd) == pWnd && ::IsWindowVisible(hSearchWnd))
{
// 比较看谁的面积最小?
if(((rectSearch.right - rectSearch.left) * (rectSearch.bottom - rectSearch.top)) < ((rect.right - rect.left) * (rect.bottom - rect.top)))
{
// 找到更小的新窗口, 更新比较窗口
hWnd = hSearchWnd;
::GetWindowRect(hWnd, &rect);
}
}
}
while(hSearchWnd != NULL);
}
}
return hWnd;
}以上函数结果不正确,请大家帮忙看看。
QQ:57582406

解决方案 »

  1.   

    使用
    HWND ChildWindowFromPoint(          HWND hWndParent,
        POINT Point
    );hWndParent 父窗口句柄
    Point 相对于父窗口的相对坐标
      

  2.   

    如下这段代码用于获取最里层的控件的光标坐标:POINT pt,ptScreen;
    ::GetCursorPos(&ptScreen);
    pt=ptScreen;
    HWND hParent,hChild;

    hParent=::WindowFromPoint(ptScreen);
    ::ScreenToClient(hParent,&pt);
    hChild=::ChildWindowFromPoint(hParent,pt); while (hParent!=hChild)
    {
    pt=ptScreen;
    ::ScreenToClient(hChild,&pt);
    hParent=hChild;

    hChild=::ChildWindowFromPoint(hParent,pt);
    }
    TCHAR szMessage[1024];
    _stprintf(szMessage,_T("坐标:%d %d\n"),pt.x,pt.y);
    OutputDebugString(szMessage);