Windows程序获取鼠标下窗口句柄的例子

来源:互联网 发布:php框架的架构思路 编辑:程序博客网 时间:2024/05/13 02:49

#include <windows.h>
#include <iostream.h>
int main()
{
 while (1)
 {
  POINT pNow = {0,0};
  if (GetCursorPos(&pNow))  // 获取鼠标当前位置
  {
   HWND hwndPointNow = NULL;
   hwndPointNow = WindowFromPoint(pNow);  // 获取鼠标所在窗口的句柄
   if (hwndPointNow)
   {
    //cout << "Success!!" << endl;
    char szWindowTitle[50];
    ::GetWindowTextA(hwndPointNow, szWindowTitle, sizeof(szWindowTitle));  // 获取窗口标题
    cout << hex << (int)hwndPointNow << endl;  // 鼠标所在窗口的句柄
    cout << szWindowTitle << endl;  // 鼠标所在窗口的标题
    CWnd *pWnd=CWnd::FromHandle(hwndPointNow);
    pWnd->MoveWindow(CRect(100,100,600,600));


   }
   else
    cout << "Error!!" << endl;
  }
  else
   cout << "Error!!" << endl;
  Sleep(1500);
 }
 return 0;
}

原创粉丝点击