Advertisement
Combreal

ShavroneCounter

Jul 6th, 2016
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Windows.h>
  2. #include <dwmapi.h>
  3. #include "resource.h"
  4.  
  5. bool showWindowB = false;
  6. bool showWindowC = false;
  7. char hotKey = 'n';
  8. int counter = 0;
  9. HWND hWindowa, hWindowb, hWindowc, hNumber, hText, hTextB;
  10. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  11. LRESULT CALLBACK WndProcB(HWND, UINT, WPARAM, LPARAM);
  12. void AddMenus(HWND);
  13. #define IDM_CHANGE_HOTKEY 1
  14. #define ID_EDIT 2
  15. #define ID_BUTTON 3
  16. #define IDM_ABOUT 4
  17. #define ID_BUTTONB 5
  18. #define IDC_STATICA 101
  19. #define IDC_STATICB 102
  20. #define IDC_STATICC 103
  21.  
  22. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow)
  23. {
  24.     MSG  msg;    
  25.     WNDCLASSW wc = {0};
  26.     wc.lpszClassName = L"ShavrCounter";
  27.     wc.hInstance     = hInstance;
  28.     wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  29.     wc.lpfnWndProc   = WndProc;
  30.     wc.hCursor       = LoadCursor(0, IDC_ARROW);
  31.     wc.hIcon         = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
  32.     RegisterClassW(&wc);
  33.     hWindowa = CreateWindowW(wc.lpszClassName, L"ShavrC", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 400, 300, 235, 200, 0, 0, hInstance, 0);
  34.    
  35.     WNDCLASSW wcB = {0};
  36.     wcB.lpszClassName = L"HotKey";
  37.     wcB.hInstance     = hInstance;
  38.     wcB.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  39.     wcB.lpfnWndProc   = WndProcB;
  40.     wcB.hCursor       = LoadCursor(0, IDC_ARROW);
  41.     wcB.hIcon         = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
  42.     RegisterClassW(&wcB);
  43.     hWindowb = CreateWindowW(wcB.lpszClassName, L"", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 419, 384, 198, 75, 0, 0, hInstance, 0);
  44.     ShowWindow(hWindowb, SW_HIDE);
  45.  
  46.     if(RegisterHotKey(NULL, 1, MOD_NOREPEAT, 0x4E))
  47.     {
  48.         OutputDebugStringW(L"Hotkey 'n' registered, using MOD_NOREPEAT flag\n");
  49.     }
  50.  
  51.     while(GetMessage(&msg, NULL, 0, 0))
  52.     {
  53.         TranslateMessage(&msg);
  54.         DispatchMessage(&msg);
  55.         if(showWindowB)
  56.         {
  57.             ShowWindow(hWindowb, nCmdShow);
  58.         }
  59.         else if(!showWindowB)
  60.         {
  61.             ShowWindow(hWindowb, SW_HIDE);
  62.         }
  63.         if(msg.message == WM_HOTKEY)
  64.         {
  65.             OutputDebugStringW(L"WM_HOTKEY received\n");
  66.             counter++;
  67.             char number[33];
  68.             _itoa_s(counter,number, 33, 10);
  69.             SetWindowText(hNumber, number);
  70.         }
  71.     }
  72.     return (int) msg.wParam;
  73. }
  74.  
  75. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  76. {
  77.     switch(msg)
  78.     {
  79.         case WM_CREATE:
  80.             AddMenus(hwnd);
  81.             hText = CreateWindowEx(0, "STATIC", "Number of times the hotKey was pressed :",
  82.                 WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS,
  83.                 38, 35, 170, 50, hwnd, (HMENU)IDC_STATICA, GetModuleHandle(NULL), 0);
  84.             hNumber = CreateWindowEx(0, "STATIC", "0",
  85.                 WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS,
  86.                 98, 85, 42, 40, hwnd, (HMENU)IDC_STATICB, GetModuleHandle(NULL), 0);
  87.             if(hText == NULL)
  88.                 MessageBox(hwnd, "Could not create textControla.", "Error", MB_OK | MB_ICONERROR);
  89.             if(hNumber == NULL)
  90.                 MessageBox(hwnd, "Could not create textControlb.", "Error", MB_OK | MB_ICONERROR);
  91.             break;
  92.         case WM_COMMAND:
  93.             if(LOWORD(wParam)==IDM_CHANGE_HOTKEY)
  94.             {
  95.                 showWindowB = true;
  96.                 SetActiveWindow(hWindowb);
  97.                 if(UnregisterHotKey(NULL, 1))
  98.                 {
  99.                     OutputDebugStringW(L"Hotkey 'n' removed\n");
  100.                 }
  101.                 break;
  102.             }
  103.             else if(LOWORD(wParam)==IDM_ABOUT)
  104.             {
  105.                 showWindowC = true;
  106.                 SetActiveWindow(hWindowc);
  107.                 MessageBox(hwnd, "Shavrone's counter tracks the number of\n"
  108.                                  "times you pressed a key on your keyboard.\n"
  109.                                  "\n"
  110.                                  "Note that you can change the HotKey to a different one.\n"
  111.                                  "The Hotkeys can only be alphabetical.\n"
  112.                                  "\n"
  113.                                  "07/06/16  It uses a GPL.\n"
  114.                                  "Please contact me at maxime.p.jolly@gmail.com\n"
  115.                                  "if you have any questions or suggestions.\n",
  116.                                  "Informations", MB_ICONQUESTION);
  117.             }
  118.             break;
  119.         case WM_DESTROY:
  120.             PostQuitMessage(0);
  121.             break;
  122.         default:
  123.             break;
  124.     }
  125.     return DefWindowProcW(hwnd, msg, wParam, lParam);
  126. }
  127.  
  128. LRESULT CALLBACK WndProcB(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  129. {
  130.     static HWND hwndEdit;
  131.     HWND hwndButton;
  132.     UINT hexVar;
  133.     switch(msg)
  134.     {
  135.         case WM_CREATE:
  136.             hTextB = CreateWindowEx(0, "STATIC", "Current key :",
  137.                 WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS,
  138.                 5, 10, 84, 25, hwnd, (HMENU)IDC_STATICC, GetModuleHandle(NULL), 0);
  139.             hwndEdit = CreateWindowW(L"EDIT", NULL,
  140.                 WS_CHILD | WS_VISIBLE | WS_BORDER | WS_CLIPSIBLINGS,
  141.                 89, 7, 18, 21, hwnd, (HMENU)ID_EDIT, GetModuleHandle(NULL), 0);
  142.             SetWindowTextW(hwndEdit, L"n");
  143.             hwndButton = CreateWindowW(L"BUTTON", L"Change",
  144.                 WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS,
  145.                 114, 4, 60, 25, hwnd, (HMENU)ID_BUTTON, GetModuleHandle(NULL), 0);
  146.             break;
  147.         case WM_COMMAND:
  148.             if(LOWORD(wParam)==ID_BUTTON)
  149.             {
  150.                 int len = GetWindowTextLengthW(hwndEdit) + 1;
  151.                 char text[33];
  152.                 GetWindowTextA(hwndEdit, text, len);
  153.                 hotKey = toupper(text[0]);
  154.                 hexVar = (int)hotKey;
  155.                 if(RegisterHotKey(NULL, 1, MOD_NOREPEAT, hexVar))
  156.                 {
  157.                     OutputDebugStringW(L"New Hotkey assigned\n");
  158.                 }
  159.                 PostMessage(hwnd, WM_CLOSE, 0, 0);
  160.             }
  161.             break;
  162.         case WM_CLOSE:
  163.             {
  164.                 int len = GetWindowTextLengthW(hwndEdit) + 1;
  165.                 char text[33];
  166.                 GetWindowTextA(hwndEdit, text, len);
  167.                 hotKey = toupper(text[0]);
  168.                 hexVar = (int)hotKey;
  169.                 if(RegisterHotKey(NULL, 1, MOD_NOREPEAT, hexVar))
  170.                 {
  171.                     OutputDebugStringW(L"New Hotkey assigned\n");
  172.                 }
  173.                 showWindowB = false;
  174.                 SetActiveWindow(hWindowa);
  175.                 return 0;
  176.             }
  177.             break;
  178.         default:
  179.             break;
  180.     }
  181.     return DefWindowProcW(hwnd, msg, wParam, lParam);
  182. }
  183.  
  184. void AddMenus(HWND hwnd)
  185. {
  186.     HMENU hMenubar;
  187.     HMENU hMenu;
  188.     HMENU hMenuB;
  189.     hMenubar = CreateMenu();
  190.     hMenu = CreateMenu();
  191.     hMenuB = CreateMenu();
  192.     AppendMenuW(hMenu, MF_STRING, IDM_CHANGE_HOTKEY, L"&Change hotKey");
  193.     AppendMenuW(hMenubar, MF_POPUP, (UINT_PTR) hMenu, L"&Counter");
  194.     AppendMenuW(hMenuB, MF_STRING, IDM_ABOUT, L"About");
  195.     AppendMenuW(hMenubar, MF_POPUP, (UINT_PTR) hMenuB, L"&?");
  196.     SetMenu(hwnd, hMenubar);
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement