首页 > 代码库 > WindowFromPoint   ChildWindowFromPoint   ChildWindowFromPointEx

WindowFromPoint   ChildWindowFromPoint   ChildWindowFromPointEx

HWND WindowFromPoint(
    POINT Point
);
功能:返回包含点的窗口句柄,Point参数指屏幕坐标。
如果不存在窗口包含这个点,则返回NULL。如果窗口无效或者隐藏,则返回NULL。

备注:WindowFromPoint函数不获取隐藏或禁止的窗口句柄,即使点在该窗口内。应用程序应该使用ChildWindowFromPoint函数进行无限制查询,这样就可以获得静态文本控件的句柄。

实例:
void CTestDlg::OnTest() 
{
    POINT pt;
    GetCursorPos(&pt);//获取坐标

    HWND hHandle = ::WindowFromPoint(pt);

        if (hHandle == m_hWnd)
    {
        MessageBox("OK");
    }
}


HWND ChildWindowFromPoint(
  HWND hWndParent,   //handle to parent window
  POINT Point        //the coordinates(relative to hWndParent) of the point to be checked
);

功能:返回包含这个点的窗口句柄,即使窗口隐藏或者处于无效状态。(需要指定某个容器窗体,返回该容器窗体中包含点的窗口句柄。)
如果点不在父窗口内,则返回NULL,如果点在父窗口内,但不在任何子窗口上,则返回父窗口的句柄。

另外,特别要注意的是:参数Point不是屏幕坐标,而是相对于容器窗口的坐标

实例:

void CTestDlg::OnOK() 
{
    POINT pt;
    GetCursorPos(&pt);

    BOOL bOK = ::ScreenToClient(m_hWnd, &pt);//屏幕坐标转换为客户区坐标
    if (!bOK)
    {
        return;
    }

    HWND hHandle = ::ChildWindowFromPoint(m_hWnd, pt);//pt经ScreenToClient转换成相对于容器窗口的坐标。
    if (hHandle == m_button.m_hWnd)
    {
        MessageBox("OK");
    }
}


HWND ChildWindowFromPointEx(
  HWND hwndParent,   // handle to parent window  POINT pt,          // structure with point coordinates  UINT uFlags        // skip options);

Parameters

  • hwndParent

  • [in] Handle to the parent window.

  • pt

  • [in] Specifies a POINT structure that defines the client coordinates (relative to hWndParent) of the point to be checked.

  • uFlags

  • [in] Specifies which child windows to skip. This parameter can be one or more of the following values.

    ValueMeaning
    CWP_ALLDoes not skip any child windows
    CWP_SKIPINVISIBLESkips invisible child windows
    CWP_SKIPDISABLEDSkips disabled child windows
    CWP_SKIPTRANSPARENTSkips transparent child windows


Return Values

The return value is a handle to the first child window that contains the point and meets the criteria specified by uFlags. If the point is within the parent window but not within any child window that meets the criteria, the return value is a handle to the parent window. If the point lies outside the parent window or if the function fails, the return value is NULL.

Remarks

The system maintains an internal list that contains the handles of the child windows associated with a parent window. The order of the handles in the list depends on the Z order of the child windows. If more than one child window contains the specified point, the system returns a handle to the first window in the list that contains the point and meets the criteria specified by uFlags.


本文出自 “whatever957” 博客,请务必保留此出处http://whatever957.blog.51cto.com/6835003/1599111

WindowFromPoint   ChildWindowFromPoint   ChildWindowFromPointEx