Created
January 2, 2023 07:25
-
-
Save voidproc/1a37e03d1a0d056d79694cf16c49b5b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SDKDDKVer.h> | |
#define WIN32_LEAN_AND_MEAN | |
#include <windows.h> | |
#include <stdlib.h> | |
#include <malloc.h> | |
#include <memory.h> | |
#include <tchar.h> | |
// メインウィンドウのクラス名 | |
LPCWSTR g_mainWindowClassName = L"WindowsHookExample"; | |
// マウスフックのハンドル | |
HHOOK g_hMouseHook = nullptr; | |
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); | |
ATOM RegisterMainWindowClass(HINSTANCE hInstance) | |
{ | |
WNDCLASSEXW wcex; | |
wcex.cbSize = sizeof(WNDCLASSEX); | |
wcex.style = CS_HREDRAW | CS_VREDRAW; | |
wcex.lpfnWndProc = WndProc; | |
wcex.cbClsExtra = 0; | |
wcex.cbWndExtra = 0; | |
wcex.hInstance = hInstance; | |
wcex.hIcon = nullptr; | |
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); | |
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); | |
wcex.lpszMenuName = nullptr; | |
wcex.lpszClassName = g_mainWindowClassName; | |
wcex.hIconSm = nullptr; | |
return RegisterClassExW(&wcex); | |
} | |
HWND CreateMainWindow(HINSTANCE hInstance, int nCmdShow) | |
{ | |
HWND hWnd = CreateWindowW( | |
g_mainWindowClassName, | |
L"WindowsHookExample", | |
WS_OVERLAPPEDWINDOW, | |
CW_USEDEFAULT, | |
0, | |
CW_USEDEFAULT, | |
0, | |
nullptr, | |
nullptr, | |
hInstance, | |
nullptr); | |
if (!hWnd) | |
{ | |
return FALSE; | |
} | |
ShowWindow(hWnd, nCmdShow); | |
UpdateWindow(hWnd); | |
return hWnd; | |
} | |
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) | |
{ | |
switch (message) | |
{ | |
case WM_DESTROY: | |
// フックを削除 | |
if (g_hMouseHook) | |
{ | |
UnhookWindowsHookEx(g_hMouseHook); | |
} | |
PostQuitMessage(0); | |
break; | |
default: | |
return DefWindowProc(hWnd, message, wParam, lParam); | |
} | |
return 0; | |
} | |
LRESULT LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam) | |
{ | |
// 処理してはいけないメッセージだったら中断 | |
if (nCode < 0 || nCode == HC_NOREMOVE) | |
{ | |
return CallNextHookEx(g_hMouseHook, nCode, wParam, lParam); | |
} | |
// フックしたメッセージをいい感じに処理する部分 | |
switch (wParam) | |
{ | |
case WM_LBUTTONDOWN: | |
OutputDebugStringW(L"WM_LBUTTONDOWN\n"); | |
break; | |
case WM_LBUTTONUP: | |
OutputDebugStringW(L"WM_LBUTTONUP\n"); | |
break; | |
} | |
return CallNextHookEx(g_hMouseHook, nCode, wParam, lParam); | |
} | |
int APIENTRY wWinMain(_In_ HINSTANCE hInstance, | |
_In_opt_ HINSTANCE hPrevInstance, | |
_In_ LPWSTR lpCmdLine, | |
_In_ int nCmdShow) | |
{ | |
// メインウィンドウを作成 | |
if (!RegisterMainWindowClass(hInstance)) | |
{ | |
return FALSE; | |
} | |
HWND hwndMainWindow = CreateMainWindow(hInstance, nCmdShow); | |
if (!hwndMainWindow) | |
{ | |
return FALSE; | |
} | |
// マウスフック(ローカル)をインストール | |
HHOOK hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, (HOOKPROC)LowLevelMouseProc, nullptr, 0); | |
if (!hMouseHook) | |
{ | |
MessageBox(hwndMainWindow, L"フックのインストールに失敗しました。", L"", MB_ICONEXCLAMATION | MB_OK); | |
return FALSE; | |
} | |
g_hMouseHook = hMouseHook; | |
// メッセージループ | |
MSG msg; | |
while (GetMessage(&msg, nullptr, 0, 0)) | |
{ | |
TranslateMessage(&msg); | |
DispatchMessage(&msg); | |
} | |
return (int)msg.wParam; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment