C++如何获取当前鼠标定向的程序的PID?
在C++中,可以使用以下方法获取当前鼠标定向的程序的PID:
- 使用Windows API函数
GetCursorPos获取当前鼠标坐标。 - 使用Windows API函数
WindowFromPoint根据鼠标坐标获取窗口句柄。 - 使用Windows API函数
GetWindowThreadProcessId根据窗口句柄获取进程ID(PID)。
以下是一个示例代码:
#include <iostream>
#include <windows.h>
int main() {
POINT cursorPos;
HWND hWnd;
// 获取当前鼠标坐标
GetCursorPos(&cursorPos);
// 根据鼠标坐标获取窗口句柄
hWnd = WindowFromPoint(cursorPos);
// 根据窗口句柄获取进程ID(PID)
DWORD pid;
GetWindowThreadProcessId(hWnd, &pid);
std::cout << "当前鼠标定向的程序的PID:" << pid << std::endl;
return 0;
}
请注意,此方法只能获取到鼠标所在位置的程序的PID,如果鼠标没有悬停在任何窗口上,则无法获取到PID。
原文地址: https://www.cveoy.top/t/topic/jaqv 著作权归作者所有。请勿转载和采集!