#include <Windows.h>
#include <iostream>
using namespace std;
DWORD GetPIDFromCursor(void)
{
POINT CursorPos;
//获取当前鼠标的位置
if (!GetCursorPos(&CursorPos))
{
cout << "GetCursorPos Error: " << GetLastError() << endl;
return 0;
}
//从鼠标位置获取当前窗体的句柄
HWND hWnd = WindowFromPoint(CursorPos);
if (NULL == hWnd)
{
cout << "No window exists at the given point!" << endl;
return 0;
}
//获取窗体句柄的pid
DWORD dwProcId;
GetWindowThreadProcessId(hWnd, &dwProcId);
return dwProcId;
}
int main()
{
int pid=GetPIDFromCursor();
cout<<pid<<endl;
return 0;
}