From 3f8b848b1e57b0438e5f33b14a22dd3e0e0ed2d6 Mon Sep 17 00:00:00 2001 From: dreamforceinc Date: Wed, 22 Jan 2020 13:40:22 +0300 Subject: [PATCH] Added hotkey for hide tray icon. --- wCenterWindow/wCenterWindow.cpp | 132 ++++++++++++++++++++------------ wCenterWindow/wCenterWindow.rc | Bin 4930 -> 4806 bytes 2 files changed, 85 insertions(+), 47 deletions(-) diff --git a/wCenterWindow/wCenterWindow.cpp b/wCenterWindow/wCenterWindow.cpp index 917aed4..e89d0a9 100644 --- a/wCenterWindow/wCenterWindow.cpp +++ b/wCenterWindow/wCenterWindow.cpp @@ -17,14 +17,16 @@ HMENU hMenu, hPopup; HWND hWnd; NOTIFYICONDATA nid = { 0 }; KBDLLHOOKSTRUCT *pkhs; +BOOL pressed = FALSE, showIcon = TRUE; +BOOL bLCTRL = FALSE, bLWIN = FALSE, bKEYI = FALSE, bKEYC = FALSE; int dtCenterX, dtCenterY; -bool pressed = FALSE; // Прототипы функций ATOM MyRegisterClass(HINSTANCE); VOID ShowError(HINSTANCE, UINT); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK KeyboardHookProc(int, WPARAM, LPARAM); +BOOL CreateTrayIcon(); // Точка входа int APIENTRY wWinMain(_In_ HINSTANCE hInstance, @@ -43,6 +45,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, return FALSE; } MyRegisterClass(hInstance); + hWnd = CreateWindowExW(0, szClass, szTitle, 0, 0, 0, 0, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { @@ -50,19 +53,13 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, return FALSE; } - nid.cbSize = sizeof(NOTIFYICONDATA); - nid.hWnd = hWnd; - nid.uVersion = NOTIFYICON_VERSION; - nid.uCallbackMessage = WM_WCW; - nid.hIcon = hIcon; - nid.uID = IDI_TRAYICON; - nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; - nid.dwInfoFlags = NIIF_INFO; - StringCchCopy(nid.szTip, sizeof(nid.szTip), szTitle); - if (!Shell_NotifyIcon(NIM_ADD, &nid)) + if (showIcon) { - ShowError(hInstance, IDS_ERR_ICON); - SendMessage(hWnd, WM_CLOSE, NULL, NULL); + if (!CreateTrayIcon()) + { + ShowError(hInstance, IDS_ERR_ICON); + SendMessage(hWnd, WM_CLOSE, NULL, NULL); + } } hMenu = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MENU)); @@ -160,50 +157,91 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) { pkhs = (KBDLLHOOKSTRUCT*)lParam; - bool bLCTRL, bLWIN; - GetAsyncKeyState(VK_LCONTROL) < 0 ? bLCTRL = TRUE : bLCTRL = FALSE; - GetAsyncKeyState(VK_LWIN) < 0 ? bLWIN = TRUE : bLWIN = FALSE; - if ((bLCTRL && bLWIN && pkhs->vkCode == 0x43) && !pressed) + if (wParam == WM_KEYUP) { - pressed = TRUE; - HWND fgWindow = GetForegroundWindow(); - if (fgWindow) - { - HWND parentWindow = fgWindow; - while (TRUE) - { - parentWindow = GetParent(fgWindow); - if (parentWindow) fgWindow = parentWindow; - else break; - } - - WINDOWPLACEMENT wp = { 0 }; - wp.length = sizeof(WINDOWPLACEMENT); - GetWindowPlacement(fgWindow, &wp); - if (wp.showCmd == SW_SHOWNORMAL) - { - int fgW = wp.rcNormalPosition.right - wp.rcNormalPosition.left; - int fgH = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top; - int fgX = dtCenterX - (fgW / 2); - int fgY = dtCenterY - (fgH / 2); - wp.rcNormalPosition.left = fgX; - wp.rcNormalPosition.top = fgY; - wp.rcNormalPosition.right = fgX + fgW; - wp.rcNormalPosition.bottom = fgY + fgH; - SendMessage(fgWindow, WM_ENTERSIZEMOVE, NULL, NULL); - SetWindowPlacement(fgWindow, &wp); - SendMessage(fgWindow, WM_EXITSIZEMOVE, NULL, NULL); - } - } + if (pkhs->vkCode == VK_LCONTROL) bLCTRL = FALSE; + if (pkhs->vkCode == VK_LWIN) bLWIN = FALSE; pressed = FALSE; } + + if (wParam == WM_KEYDOWN) + { + if (pkhs->vkCode == VK_LCONTROL) bLCTRL = TRUE; + if (pkhs->vkCode == VK_LWIN) bLWIN = TRUE; + + if (bLCTRL && bLWIN && pkhs->vkCode == 0x49 && !pressed) // 'I' key + { + pressed = TRUE; + showIcon = !showIcon; + if (showIcon) + { + if (!CreateTrayIcon()) + { + ShowError(GetModuleHandle(NULL), IDS_ERR_ICON); + showIcon = FALSE; + } + } + else + { + Shell_NotifyIcon(NIM_DELETE, &nid); + } + } + + if (bLCTRL && bLWIN && pkhs->vkCode == 0x43 && !pressed) // 'C' key + { + pressed = TRUE; + HWND fgWindow = GetForegroundWindow(); + if (fgWindow) + { + HWND parentWindow = fgWindow; + while (TRUE) + { + parentWindow = GetParent(fgWindow); + if (parentWindow) fgWindow = parentWindow; + else break; + } + WINDOWPLACEMENT wp = { 0 }; + wp.length = sizeof(WINDOWPLACEMENT); + GetWindowPlacement(fgWindow, &wp); + if (wp.showCmd == SW_SHOWNORMAL) + { + int fgW = wp.rcNormalPosition.right - wp.rcNormalPosition.left; + int fgH = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top; + int fgX = dtCenterX - (fgW / 2); + int fgY = dtCenterY - (fgH / 2); + wp.rcNormalPosition.left = fgX; + wp.rcNormalPosition.top = fgY; + wp.rcNormalPosition.right = fgX + fgW; + wp.rcNormalPosition.bottom = fgY + fgH; + SendMessage(fgWindow, WM_ENTERSIZEMOVE, NULL, NULL); + SetWindowPlacement(fgWindow, &wp); + SendMessage(fgWindow, WM_EXITSIZEMOVE, NULL, NULL); + } + } + } + } return CallNextHookEx(NULL, nCode, wParam, lParam); } +BOOL CreateTrayIcon() +{ + nid.cbSize = sizeof(NOTIFYICONDATA); + nid.hWnd = hWnd; + nid.uVersion = NOTIFYICON_VERSION; + nid.uCallbackMessage = WM_WCW; + nid.hIcon = hIcon; + nid.uID = IDI_TRAYICON; + nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; + nid.dwInfoFlags = NIIF_INFO; + StringCchCopy(nid.szTip, sizeof(nid.szTip), szTitle); + return Shell_NotifyIcon(NIM_ADD, &nid); +} + VOID ShowError(HINSTANCE hInstance, UINT uID) { WCHAR szErrorText[MAX_LOADSTRING]; // Текст ошибки LoadStringW(hInstance, uID, szErrorText, MAX_LOADSTRING); MessageBox(hWnd, szErrorText, szTitle, MB_OK | MB_ICONERROR); } + diff --git a/wCenterWindow/wCenterWindow.rc b/wCenterWindow/wCenterWindow.rc index 10c50de1704c36d9eeeedb4b92d8ae0fe7dafa62..4907183a3c608fc306e7ff275378b371f7ae9002 100644 GIT binary patch delta 170 zcmX@4c1(4H57Xo>rXps22K~vunF1w!7#tb=7~B~`fwVh=D}w@q0fP>Rob1m0m*0^g zi6Nh%l%a${4+J-RvzW5*8bcI7mGyFH3ZjTG@G@{s7UUG(e2XK2k=p=h9u#hN<*b#MK-VJ zC}-q0VK8K{1S&FNNZM@3rOC)?z@P`@>M@UuTiue1(^fWpa-YpD{TedMNl5035+K1ONa4