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);

}

你可能感兴趣的:(VC获取鼠标所在位置窗口)