1 #include "HotKey.h" 2 using namespace std; 3 4 5 std::vector<HOT_KEY_INFO> CHotKey::m_vecHotKey; 6 extern HINSTANCE g_hInstance; 7 8 BOOL CHotKey::SetHotKey(VOIDFUNC pfnAddr, DWORD dwVK) 9 { 10 HOT_KEY_INFO stHotKey = { 0 }; 11 stHotKey.atHotKey = 0; 12 stHotKey.bRegistered = FALSE; 13 stHotKey.pfnCallBackAddr = pfnAddr; 14 stHotKey.dwVK = dwVK; 15 m_vecHotKey.push_back(stHotKey); 16 return TRUE; 17 } 18 19 DWORD WINAPI CHotKey::CreateMsgWindow(LPVOID lpParameter) 20 { 21 WNDCLASSA wndcls = {0}; 22 23 wndcls.hInstance = (HINSTANCE)g_hInstance; 24 wndcls.lpfnWndProc = WndProc; 25 wndcls.lpszClassName = "Window_"; 26 27 RegisterClassA(&wndcls); 28 CreateWindowA("Window_", "Window", WS_OVERLAPPEDWINDOW, 500, 200, 600, 400, NULL,NULL, (HINSTANCE)g_hInstance, NULL); 29 30 MSG msg; 31 while(GetMessage(&msg, NULL, 0, 0)) 32 { 33 TranslateMessage(&msg); 34 DispatchMessage(&msg); 35 } 36 return 0; 37 } 38 39 40 //替换的窗口过程 41 LRESULT CALLBACK CHotKey::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 42 { 43 vector<HOT_KEY_INFO>::iterator p = m_vecHotKey.begin(); 44 for (;p != m_vecHotKey.end(); p++) 45 { 46 if (p->bRegistered) 47 { 48 continue; 49 } 50 char szAtom[MAX_PATH] = { 0 }; 51 _snprintf_s(szAtom, sizeof(szAtom), "%s_%d", "GameAssist", p->dwVK); 52 p->atHotKey = GlobalAddAtomA(szAtom); 53 p->bRegistered = RegisterHotKey(hwnd, p->atHotKey, 0, p->dwVK); 54 } 55 56 if (uMsg == WM_HOTKEY) 57 { 58 vector<HOT_KEY_INFO>::iterator p = m_vecHotKey.begin(); 59 for (;p != m_vecHotKey.end(); p++) 60 { 61 if (wParam == p->atHotKey && p->pfnCallBackAddr != NULL && p->bRegistered) 62 { 63 (p->pfnCallBackAddr)(); 64 } 65 } 66 } 67 68 return DefWindowProc(hwnd,uMsg,wParam,lParam); 69 70 }
1 #pragma once 2 #include <windows.h> 3 #include <vector> 4 5 typedef void(*VOIDFUNC)(); 6 7 struct HOT_KEY_INFO 8 { 9 DWORD dwVK; 10 VOIDFUNC pfnCallBackAddr; 11 ATOM atHotKey; 12 BOOL bRegistered; 13 }; 14 15 class CHotKey 16 { 17 public: 18 static BOOL SetHotKey(VOIDFUNC pfnAddr, DWORD dwVK); 19 static DWORD WINAPI CreateMsgWindow(LPVOID lpParameter); 20 private: 21 static LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); 22 23 static std::vector<HOT_KEY_INFO> m_vecHotKey; 24 };