1. 程式人生 > >VC獲取滑鼠所在位置視窗

VC獲取滑鼠所在位置視窗

編號:A3GS_TV20100122003

描述:

本文以例項程式碼的形式講述了在VC中下實現滑鼠所在位置視窗的獲取。

例子程式碼

請下載本文附帶例子程式碼。

技術實現:

標頭檔案

#include <winuser.h>

技術說明

實現本功能主要就是一個WindowFromPoint系統API的呼叫,本API的詳細資訊請參見MSDN相關文件。本文以對話方塊視窗為例子一說明如果獲取當前滑鼠所在位置下的視窗資訊。實現步驟如下:

1.編寫WM_MOUSEMOVE訊息響應函式:

void CTestDlg::OnMouseMove(UINT nFlags, CPoint point)

{

CWnd * pWnd = WindowFromPoint(point);

if (AfxIsValidAddress(pWnd,sizeof(CWnd)))

if (::IsChild(m_hWnd,pWnd->m_hWnd))

{

CString str;

pWnd->GetWindowText(str);

SetWindowText(str);

}

CDialog::OnMouseMove(nFlags, point);

}

2.重定PreTranslateMessage函式

在此函式裡當我們發現是WM_MOUSEMOVE訊息時呼叫我們上面寫的OnMouseMove函式。本函式程式碼如下:

BOOL CTestDlg::PreTranslateMessage(MSG* pMsg)

{

if (pMsg->message==WM_MOUSEMOVE)

{

CPoint point(LOWORD(pMsg->lParam),HIWORD(pMsg->lParam));

::ClientToScreen(pMsg->hwnd,&point);

//第一個步驟自己寫的函式

OnMouseMove(0,point);

}

return CDialog::PreTranslateMessage(pMsg);

}