#include #include #include "resource.h" bool showWindowB = false; bool showWindowC = false; char hotKey = 'n'; int counter = 0; HWND hWindowa, hWindowb, hWindowc, hNumber, hText, hTextB; LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK WndProcB(HWND, UINT, WPARAM, LPARAM); void AddMenus(HWND); #define IDM_CHANGE_HOTKEY 1 #define ID_EDIT 2 #define ID_BUTTON 3 #define IDM_ABOUT 4 #define ID_BUTTONB 5 #define IDC_STATICA 101 #define IDC_STATICB 102 #define IDC_STATICC 103 int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASSW wc = {0}; wc.lpszClassName = L"ShavrCounter"; wc.hInstance = hInstance; wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wc.lpfnWndProc = WndProc; wc.hCursor = LoadCursor(0, IDC_ARROW); wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON)); RegisterClassW(&wc); hWindowa = CreateWindowW(wc.lpszClassName, L"ShavrC", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 400, 300, 235, 200, 0, 0, hInstance, 0); WNDCLASSW wcB = {0}; wcB.lpszClassName = L"HotKey"; wcB.hInstance = hInstance; wcB.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wcB.lpfnWndProc = WndProcB; wcB.hCursor = LoadCursor(0, IDC_ARROW); wcB.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON)); RegisterClassW(&wcB); hWindowb = CreateWindowW(wcB.lpszClassName, L"", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 419, 384, 198, 75, 0, 0, hInstance, 0); ShowWindow(hWindowb, SW_HIDE); if(RegisterHotKey(NULL, 1, MOD_NOREPEAT, 0x4E)) { OutputDebugStringW(L"Hotkey 'n' registered, using MOD_NOREPEAT flag\n"); } while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); if(showWindowB) { ShowWindow(hWindowb, nCmdShow); } else if(!showWindowB) { ShowWindow(hWindowb, SW_HIDE); } if(msg.message == WM_HOTKEY) { OutputDebugStringW(L"WM_HOTKEY received\n"); counter++; char number[33]; _itoa_s(counter,number, 33, 10); SetWindowText(hNumber, number); } } return (int) msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: AddMenus(hwnd); hText = CreateWindowEx(0, "STATIC", "Number of times the hotKey was pressed :", WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 38, 35, 170, 50, hwnd, (HMENU)IDC_STATICA, GetModuleHandle(NULL), 0); hNumber = CreateWindowEx(0, "STATIC", "0", WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 98, 85, 42, 40, hwnd, (HMENU)IDC_STATICB, GetModuleHandle(NULL), 0); if(hText == NULL) MessageBox(hwnd, "Could not create textControla.", "Error", MB_OK | MB_ICONERROR); if(hNumber == NULL) MessageBox(hwnd, "Could not create textControlb.", "Error", MB_OK | MB_ICONERROR); break; case WM_COMMAND: if(LOWORD(wParam)==IDM_CHANGE_HOTKEY) { showWindowB = true; SetActiveWindow(hWindowb); if(UnregisterHotKey(NULL, 1)) { OutputDebugStringW(L"Hotkey 'n' removed\n"); } break; } else if(LOWORD(wParam)==IDM_ABOUT) { showWindowC = true; SetActiveWindow(hWindowc); MessageBox(hwnd, "Shavrone's counter tracks the number of\n" "times you pressed a key on your keyboard.\n" "\n" "Note that you can change the HotKey to a different one.\n" "The Hotkeys can only be alphabetical.\n" "\n" "07/06/16 It uses a GPL.\n" "Please contact me at maxime.p.jolly@gmail.com\n" "if you have any questions or suggestions.\n", "Informations", MB_ICONQUESTION); } break; case WM_DESTROY: PostQuitMessage(0); break; default: break; } return DefWindowProcW(hwnd, msg, wParam, lParam); } LRESULT CALLBACK WndProcB(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static HWND hwndEdit; HWND hwndButton; UINT hexVar; switch(msg) { case WM_CREATE: hTextB = CreateWindowEx(0, "STATIC", "Current key :", WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 5, 10, 84, 25, hwnd, (HMENU)IDC_STATICC, GetModuleHandle(NULL), 0); hwndEdit = CreateWindowW(L"EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | WS_CLIPSIBLINGS, 89, 7, 18, 21, hwnd, (HMENU)ID_EDIT, GetModuleHandle(NULL), 0); SetWindowTextW(hwndEdit, L"n"); hwndButton = CreateWindowW(L"BUTTON", L"Change", WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS, 114, 4, 60, 25, hwnd, (HMENU)ID_BUTTON, GetModuleHandle(NULL), 0); break; case WM_COMMAND: if(LOWORD(wParam)==ID_BUTTON) { int len = GetWindowTextLengthW(hwndEdit) + 1; char text[33]; GetWindowTextA(hwndEdit, text, len); hotKey = toupper(text[0]); hexVar = (int)hotKey; if(RegisterHotKey(NULL, 1, MOD_NOREPEAT, hexVar)) { OutputDebugStringW(L"New Hotkey assigned\n"); } PostMessage(hwnd, WM_CLOSE, 0, 0); } break; case WM_CLOSE: { int len = GetWindowTextLengthW(hwndEdit) + 1; char text[33]; GetWindowTextA(hwndEdit, text, len); hotKey = toupper(text[0]); hexVar = (int)hotKey; if(RegisterHotKey(NULL, 1, MOD_NOREPEAT, hexVar)) { OutputDebugStringW(L"New Hotkey assigned\n"); } showWindowB = false; SetActiveWindow(hWindowa); return 0; } break; default: break; } return DefWindowProcW(hwnd, msg, wParam, lParam); } void AddMenus(HWND hwnd) { HMENU hMenubar; HMENU hMenu; HMENU hMenuB; hMenubar = CreateMenu(); hMenu = CreateMenu(); hMenuB = CreateMenu(); AppendMenuW(hMenu, MF_STRING, IDM_CHANGE_HOTKEY, L"&Change hotKey"); AppendMenuW(hMenubar, MF_POPUP, (UINT_PTR) hMenu, L"&Counter"); AppendMenuW(hMenuB, MF_STRING, IDM_ABOUT, L"About"); AppendMenuW(hMenubar, MF_POPUP, (UINT_PTR) hMenuB, L"&?"); SetMenu(hwnd, hMenubar); }