NO IMAGE

這段程式碼測試程式,實現功能動態獲取視窗大小,獲取滑鼠按鍵訊息,獲取滑鼠所在視窗的位置,大小,視窗控制代碼
實現原理:

具體解析如下:

GetCursorPos(&pNow) // 獲取滑鼠當前位置
hwndPointNow = WindowFromPoint(pNow);  // 獲取滑鼠所在視窗的控制代碼
::GetWindowThreadProcessId(hwndPointNow, &dwProcessID);//獲取視窗PID
GetWindowRect(hwndPointNow, &rect);//獲取視窗位置
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0) 獲取鍵值
/************************************************************************************
* filename : main.cpp
* comment : The left mouse button, right pulley, message capture, access to the mouse window handle, window position.
* author : liangqidong <[email protected]>
* History : create by liangqidong @ 2017/09/18
*************************************************************************************/
#include <windows.h>
#include <tlhelp32.h>    //CreateToolhelp32Snapshot
#include <string>
#include <iostream>
using namespace std;
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0) //必要的,我是背下來的   
HWND GetWindowHandleByPID(DWORD dwProcessID)
{
HWND h = GetTopWindow(0);
while (h)
{
DWORD pid = 0;
DWORD dwTheardId = GetWindowThreadProcessId(h, &pid);
if (dwTheardId != 0)
{
if (pid == dwProcessID/*your process id*/)
{
// here h is the handle to the window  
if (GetTopWindow(h))
{
return h;
}
// return h;   
}
}
h = ::GetNextWindow(h, GW_HWNDNEXT);
}
return NULL;
}
int main()
{
HWND hwndNow = GetWindowHandleByPID(13956);
while (1)
{
POINT pNow = { 0, 0 };
if (GetCursorPos(&pNow))  // 獲取滑鼠當前位置
{
HWND hwndPointNow = NULL;
DWORD  dwProcessID = 0;
RECT rect;
hwndPointNow = WindowFromPoint(pNow);  // 獲取滑鼠所在視窗的控制代碼
::GetWindowThreadProcessId(hwndPointNow, &dwProcessID);
if (13956 == dwProcessID)
{ 
GetWindowRect(hwndPointNow, &rect);
std::cout << "視窗的控制代碼:" << (int)hwndNow<< std::endl;  // 滑鼠所在視窗的控制代碼
std::cout << "滑鼠所在視窗的控制代碼:" << (int)hwndPointNow << "ID:" << dwProcessID << std::endl;  // 滑鼠所在視窗的控制代碼
std::cout << "滑鼠所在視窗的top:" << rect.top << std::endl;  // 滑鼠所在視窗的控制代碼
std::cout << "滑鼠所在視窗的bottom:" << rect.bottom << std::endl;  // 滑鼠所在視窗的控制代碼
std::cout << "滑鼠所在視窗的left:" << rect.left << std::endl;  // 滑鼠所在視窗的控制代碼
std::cout << "滑鼠所在視窗的right:" << rect.right << std::endl;  // 滑鼠所在視窗的控制代碼
std::cout << "滑鼠所在視窗的寬:" << rect.right - rect.left << std::endl;  // 滑鼠所在視窗的控制代碼
std::cout << "滑鼠所在視窗的高:" << rect.bottom - rect.top << std::endl;  // 滑鼠所在視窗的控制代碼
printf("滑鼠左鍵是否按下:");
if (KEY_DOWN(MOUSE_MOVED))printf("是");
else printf("否");
printf("\n");
printf("滑鼠右鍵是否按下:");
if (KEY_DOWN(MOUSE_EVENT))printf("是");
else printf("否");
printf("\n");
printf("滑鼠滾輪鍵是否按下:");
if (KEY_DOWN(MOUSE_WHEELED))printf("是");
else printf("否");
printf("\n");
}
Sleep(1000);
system("cls");//清屏   
}
}
return 0;
}

有什麼缺點請大家指教

具體程式碼請參考git [email protected]:snippets/2580563.git 點選開啟連結