HWND WindowFromPoint(
POINT Point
);
功能:返回包含點的窗口句柄,Point參數指屏幕坐標。
如果不存在窗口包含這個點,則返回NULL。如果窗口無效或者隱藏,則返回NULL。
通過測試,發現獲取部分控件句柄時,返回的都是父窗口的句柄。包括Static Text, GroupBox等等。
實例:

1 void CTestDlg::OnTest() 2 { 3 POINT pt; 4 GetCursorPos(&pt);//獲取坐標 5 6 HWND hHandle = ::WindowFromPoint(pt); 7 8 if (hHandle == m_hWnd) 9 { 10 MessageBox("OK"); 11 } 12 }
HWND ChildWindowFromPoint(
HWND hWndParent, //handle to parent window
POINT Point //the coordinates(relative to hWndParent) of the point to be checked
);
功能:返回包含這個點的窗口句柄,即使窗口隱藏或者處於無效狀態。(需要指定某個容器窗體,返回該容器窗體中包含點的窗口句柄。)
如果點不在父窗口內,則返回NULL,如果點在父窗口內,但不在任何子窗口上,則返回父窗口的句柄。
另外,特別要注意的是:參數Point不是屏幕坐標,而是相對於容器窗口的坐標。
實例:
當鼠標放在m_button上時,返回OK。

1 void CTestDlg::OnOK() 2 { 3 POINT pt; 4 GetCursorPos(&pt); 5 6 BOOL bOK = ::ScreenToClient(m_hWnd, &pt); 7 if (!bOK) 8 { 9 return; 10 } 11 12 HWND hHandle = ::ChildWindowFromPoint(m_hWnd, pt); 13 if (hHandle == m_button.m_hWnd) 14 { 15 MessageBox("OK"); 16 } 17 }