diff --git a/wCenterWindow/resource.h b/wCenterWindow/resource.h new file mode 100644 index 0000000..7bff434 --- /dev/null +++ b/wCenterWindow/resource.h @@ -0,0 +1,19 @@ +#pragma once + +#define IDS_APP_TITLE 100 +#define IDS_CLASSNAME 101 +#define IDS_ABOUT 102 +#define IDI_TRAYICON 103 +#define IDR_MENU 104 +#define ID_POPUPMENU_ICON 105 +#define ID_POPUPMENU_AREA 106 +#define ID_POPUPMENU_ABOUT 107 +#define ID_POPUPMENU_EXIT 108 +#define IDS_ERR_MAIN 109 +#define IDS_ERR_WND 110 +#define IDS_ERR_ICON 111 +#define IDS_ERR_MENU 112 +#define IDS_ERR_POPUP 113 +#define IDS_ERR_HOOK 114 +#define IDS_RUNNING 115 +#define IDC_STATIC -1 diff --git a/wCenterWindow/wCenterWindow.cpp b/wCenterWindow/wCenterWindow.cpp index 3ac6f58..dbd28a4 100644 --- a/wCenterWindow/wCenterWindow.cpp +++ b/wCenterWindow/wCenterWindow.cpp @@ -1,256 +1,263 @@ -// wCenterWindow.cpp : Определяет точку входа для приложения. - -#include "framework.h" -#include "wCenterWindow.h" - -#define MAX_LOADSTRING 32 -#define WM_WCW WM_USER + 0x7F00 - -// Глобальные переменные: -HINSTANCE hInst; // Текущий экземпляр -WCHAR szTitle[MAX_LOADSTRING]; // Текст строки заголовка -WCHAR szClass[MAX_LOADSTRING]; // Имя класса главного окна -WCHAR szAbout[MAX_LOADSTRING * 9]; // Текст описания -HHOOK KeyboardHook; -HICON hIcon; -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; - -// Прототипы функций -ATOM MyRegisterClass(HINSTANCE); -VOID HandlingTrayIcon(); -VOID ShowError(UINT); -LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); -LRESULT CALLBACK KeyboardHookProc(int, WPARAM, LPARAM); - -// Точка входа -int APIENTRY wWinMain(_In_ HINSTANCE hInstance, - _In_opt_ HINSTANCE hPrevInstance, - _In_ LPWSTR lpCmdLine, - _In_ int nCmdShow) -{ - UNREFERENCED_PARAMETER(hPrevInstance); - UNREFERENCED_PARAMETER(lpCmdLine); - - hInst = hInstance; - - LoadStringW(hInstance, IDS_APP_TITLE, szTitle, _countof(szTitle)); - LoadStringW(hInstance, IDS_CLASSNAME, szClass, _countof(szClass)); - - if (FindWindow(szClass, NULL)) - { - ShowError(IDS_RUNNING); - return FALSE; - } - - MyRegisterClass(hInstance); - hWnd = CreateWindowExW(0, szClass, szTitle, 0, 0, 0, 0, 0, NULL, NULL, hInstance, NULL); - if (!hWnd) - { - ShowError(IDS_ERR_WND); - 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, _countof(nid.szTip), szTitle); - - int nArgs = 0; - LPWSTR *szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs); - (nArgs >= 2 && 0 == lstrcmpiW(szArglist[1], L"/hide")) ? showIcon = FALSE : showIcon = TRUE; - LocalFree(szArglist); - HandlingTrayIcon(); - - MSG msg; - BOOL bRet; - - while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) - { - if (bRet == -1) - { - ShowError(IDS_ERR_MAIN); - return -1; - } - else - { - TranslateMessage(&msg); - DispatchMessage(&msg); - } - } - return (int)msg.wParam; -} - -ATOM MyRegisterClass(HINSTANCE hInstance) -{ - WNDCLASSEX wcex = { 0 }; - wcex.cbSize = sizeof(WNDCLASSEX); - wcex.lpfnWndProc = WndProc; - wcex.hInstance = hInstance; - wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TRAYICON)); - wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); - wcex.lpszClassName = szClass; - wcex.hIconSm = wcex.hIcon; - hIcon = wcex.hIcon; - return RegisterClassEx(&wcex); -} - -LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - switch (message) - { - case WM_CREATE: - { - hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_MENU)); - if (!hMenu) - { - ShowError(IDS_ERR_MENU); - SendMessage(hWnd, WM_CLOSE, NULL, NULL); - } - - hPopup = GetSubMenu(hMenu, 0); - if (!hPopup) - { - ShowError(IDS_ERR_POPUP); - SendMessage(hWnd, WM_CLOSE, NULL, NULL); - } - - KeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProc, hInst, NULL); - if (!KeyboardHook) - { - ShowError(IDS_ERR_HOOK); - SendMessage(hWnd, WM_CLOSE, NULL, NULL); - } - - LoadStringW(hInst, IDS_ABOUT, szAbout, _countof(szAbout)); - RECT dtrc = { 0 }; - SystemParametersInfo(SPI_GETWORKAREA, NULL, &dtrc, FALSE); - dtCenterX = dtrc.right / 2, dtCenterY = dtrc.bottom / 2; - break; - } - case WM_WCW: - if (lParam == WM_RBUTTONDOWN && wParam == IDI_TRAYICON) - { - SetForegroundWindow(hWnd); - POINT pt; - GetCursorPos(&pt); - int idMenu = TrackPopupMenu(hPopup, TPM_RETURNCMD, pt.x, pt.y, 0, hWnd, NULL); - if (idMenu == ID_POPUPMENU_ICON) - { - showIcon = FALSE; - HandlingTrayIcon(); - } - if (idMenu == ID_POPUPMENU_ABOUT && !pressed) - { - pressed = TRUE; - if (MessageBox(hWnd, szAbout, szTitle, MB_OK | MB_TOPMOST) == IDOK) pressed = FALSE; - } - if (idMenu == ID_POPUPMENU_EXIT) SendMessage(hWnd, WM_CLOSE, NULL, NULL); - } - break; - - case WM_DESTROY: - if (KeyboardHook) UnhookWindowsHookEx(KeyboardHook); - if (hMenu) DestroyMenu(hMenu); - Shell_NotifyIcon(NIM_DELETE, &nid); - PostQuitMessage(0); - break; - - default: - return DefWindowProc(hWnd, message, wParam, lParam); - } - return 0; -} - -LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) -{ - pkhs = (KBDLLHOOKSTRUCT*)lParam; - - if (wParam == WM_KEYUP) - { - 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; - HandlingTrayIcon(); - } - - 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); -} - -VOID HandlingTrayIcon() -{ - if (showIcon) - { - if (!Shell_NotifyIcon(NIM_ADD, &nid)) - { - ShowError(IDS_ERR_ICON); - showIcon = FALSE; - } - } - else - { - Shell_NotifyIcon(NIM_DELETE, &nid); - } - -} - -VOID ShowError(UINT uID) -{ - WCHAR szErrorText[MAX_LOADSTRING]; // Текст ошибки - LoadStringW(hInst, uID, szErrorText, _countof(szErrorText)); - MessageBox(hWnd, szErrorText, szTitle, MB_OK | MB_ICONERROR); -} +// wCenterWindow, v2.1 + +#include +#include +#include "resource.h" +#include "S:\MyFunctions\MoveWindowToMonitorCenter.h" + +#define MAX_LOADSTRING 32 +#define WM_WCW 0x8F00 + +// Global variables: +HINSTANCE hInst; // Instance +CHAR szTitle[MAX_LOADSTRING]; // Window's title +CHAR szClass[MAX_LOADSTRING]; // Window's class +CHAR szAbout[MAX_LOADSTRING * 14]; // Description text +HHOOK KeyboardHook; +HICON hIcon; +HMENU hMenu, hPopup; +HWND hWnd, hTaskBar, hDesktop, hProgman; +BOOL bPressed = FALSE, bShowIcon = TRUE, bWorkArea = TRUE; +BOOL bLCTRL = FALSE, bLWIN = FALSE, bKEYI = FALSE, bKEYC = FALSE; + +NOTIFYICONDATA nid = { 0 }; +LPKBDLLHOOKSTRUCT pkhs; +MENUITEMINFO mii; +POINT ptMousePos; + +// Function's prototypes +ATOM MyRegisterClass(HINSTANCE); +VOID HandlingTrayIcon(); +VOID ShowError(UINT); +LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); +LRESULT CALLBACK KeyboardHookProc(int, WPARAM, LPARAM); + +int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) +{ + UNREFERENCED_PARAMETER(hPrevInstance); + UNREFERENCED_PARAMETER(lpCmdLine); + + hInst = hInstance; + + LoadString(hInstance, IDS_APP_TITLE, szTitle, _countof(szTitle)); + LoadString(hInstance, IDS_CLASSNAME, szClass, _countof(szClass)); + + if (FindWindow(szClass, NULL)) + { + ShowError(IDS_RUNNING); + return FALSE; + } + + MyRegisterClass(hInstance); + hWnd = CreateWindowEx(0, szClass, szTitle, 0, 0, 0, 0, 0, NULL, NULL, hInstance, NULL); + if (!hWnd) + { + ShowError(IDS_ERR_WND); + return FALSE; + } + + int nArgs = 0; + LPWSTR *szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs); + (nArgs >= 2 && 0 == lstrcmpiW(szArglist[1], L"/hide")) ? bShowIcon = FALSE : bShowIcon = TRUE; + LocalFree(szArglist); + HandlingTrayIcon(); + + hTaskBar = FindWindow("Shell_TrayWnd", NULL); + hProgman = FindWindow("Progman", NULL); + hDesktop = GetDesktopWindow(); + + MSG msg; + BOOL bRet; + + while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) + { + if (bRet == -1) + { + ShowError(IDS_ERR_MAIN); + return -1; + } + else + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + } + return (int)msg.wParam; +} + +ATOM MyRegisterClass(HINSTANCE hInstance) +{ + WNDCLASSEX wcex = { 0 }; + wcex.cbSize = sizeof(WNDCLASSEX); + wcex.lpfnWndProc = WndProc; + wcex.hInstance = hInstance; + wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TRAYICON)); + wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); + wcex.lpszClassName = szClass; + wcex.hIconSm = wcex.hIcon; + hIcon = wcex.hIcon; + return RegisterClassEx(&wcex); +} + +LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +{ + switch (message) + { + case WM_CREATE: + { + hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_MENU)); + if (!hMenu) + { + ShowError(IDS_ERR_MENU); + SendMessage(hWnd, WM_CLOSE, NULL, NULL); + } + + hPopup = GetSubMenu(hMenu, 0); + if (!hPopup) + { + ShowError(IDS_ERR_POPUP); + SendMessage(hWnd, WM_CLOSE, NULL, NULL); + } + + mii.cbSize = sizeof(MENUITEMINFO); + mii.fMask = MIIM_STATE; + bWorkArea ? mii.fState = MFS_CHECKED : mii.fState = MFS_UNCHECKED; + SetMenuItemInfo(hPopup, ID_POPUPMENU_AREA, FALSE, &mii); + + 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, _countof(nid.szTip), szTitle); + + KeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProc, hInst, NULL); + if (!KeyboardHook) + { + ShowError(IDS_ERR_HOOK); + SendMessage(hWnd, WM_CLOSE, NULL, NULL); + } + + LoadString(hInst, IDS_ABOUT, szAbout, _countof(szAbout)); + } + break; + + case WM_WCW: + { + if (lParam == WM_RBUTTONDOWN && wParam == IDI_TRAYICON) + { + SetForegroundWindow(hWnd); + POINT pt; + GetCursorPos(&pt); + int idMenu = TrackPopupMenu(hPopup, TPM_RETURNCMD, pt.x, pt.y, 0, hWnd, NULL); + if (idMenu == ID_POPUPMENU_ICON) + { + bShowIcon = FALSE; + HandlingTrayIcon(); + } + if (idMenu == ID_POPUPMENU_AREA) + { + bWorkArea = !bWorkArea; + bWorkArea ? mii.fState = MFS_CHECKED : mii.fState = MFS_UNCHECKED; + SetMenuItemInfo(hPopup, ID_POPUPMENU_AREA, FALSE, &mii); + } + if (idMenu == ID_POPUPMENU_ABOUT && !bPressed) + { + bPressed = TRUE; + if (MessageBox(hWnd, szAbout, szTitle, MB_OK | MB_TOPMOST | MB_ICONINFORMATION) == IDOK) bPressed = FALSE; + } + if (idMenu == ID_POPUPMENU_EXIT) SendMessage(hWnd, WM_CLOSE, NULL, NULL); + } + } + break; + + case WM_DESTROY: + { + if (KeyboardHook) UnhookWindowsHookEx(KeyboardHook); + if (hMenu) DestroyMenu(hMenu); + Shell_NotifyIcon(NIM_DELETE, &nid); + PostQuitMessage(0); + } + break; + + default: + return DefWindowProc(hWnd, message, wParam, lParam); + } + return 0; +} + + +LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) +{ + pkhs = (KBDLLHOOKSTRUCT*)lParam; + + if (wParam == WM_KEYUP) + { + if (pkhs->vkCode == VK_LCONTROL) bLCTRL = FALSE; + if (pkhs->vkCode == VK_LWIN) bLWIN = FALSE; + bPressed = 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 && !bPressed) // 'I' key + { + bPressed = TRUE; + bShowIcon = !bShowIcon; + HandlingTrayIcon(); + return TRUE; + } + + if (bLCTRL && bLWIN && pkhs->vkCode == 0x43 && !bPressed) // 'C' key + { + bPressed = TRUE; + HWND hFgWnd = GetForegroundWindow(); + + if (hFgWnd) + { + if (hFgWnd != hDesktop && hFgWnd != hTaskBar && hFgWnd != hProgman) + { + if (!IsIconic(hFgWnd) && !IsZoomed(hFgWnd)) + { + RECT fgwrc; + GetWindowRect(hFgWnd, &fgwrc); + LONG fgWidth = fgwrc.right - fgwrc.left; + LONG fgHeight = fgwrc.bottom - fgwrc.top; + MoveWindowToMonitorCenter(hFgWnd, fgWidth, fgHeight, bWorkArea, FALSE); + } + } + } + return TRUE; + } + } + return CallNextHookEx(NULL, nCode, wParam, lParam); +} + +VOID HandlingTrayIcon() +{ + if (bShowIcon) + { + if (!Shell_NotifyIcon(NIM_ADD, &nid)) + { + ShowError(IDS_ERR_ICON); + bShowIcon = FALSE; + } + } + else + { + Shell_NotifyIcon(NIM_DELETE, &nid); + } +} + +VOID ShowError(UINT uID) +{ + CHAR szErrorText[MAX_LOADSTRING]; // Error's text + LoadString(hInst, uID, szErrorText, _countof(szErrorText)); + MessageBox(hWnd, szErrorText, szTitle, MB_OK | MB_ICONERROR); +} diff --git a/wCenterWindow/wCenterWindow.rc b/wCenterWindow/wCenterWindow.rc index 284237f..75288f2 100644 Binary files a/wCenterWindow/wCenterWindow.rc and b/wCenterWindow/wCenterWindow.rc differ diff --git a/wCenterWindow/wCenterWindow.vcxproj b/wCenterWindow/wCenterWindow.vcxproj index 566c43f..542bf70 100644 --- a/wCenterWindow/wCenterWindow.vcxproj +++ b/wCenterWindow/wCenterWindow.vcxproj @@ -1,173 +1,143 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - 15.0 - {6A775901-6C34-4E96-B359-C11C76A520BB} - Win32Proj - wCenterWindow - 10.0.17763.0 - - - - Application - true - v141 - Unicode - - - Application - false - v141 - true - Unicode - - - Application - true - v141 - Unicode - - - Application - false - v141 - true - Unicode - - - - - - - - - - - - - - - - - - - - - true - - - true - - - false - - - false - - - - - - Level3 - Disabled - true - WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) - true - - - Windows - true - - - - - - - Level3 - Disabled - true - _DEBUG;_WINDOWS;%(PreprocessorDefinitions) - true - - - Windows - true - - - - - - - Level3 - MaxSpeed - true - true - true - WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) - true - None - - - Windows - true - true - false - false - - - - - - - Level3 - MaxSpeed - true - true - true - NDEBUG;_WINDOWS;%(PreprocessorDefinitions) - true - - - Windows - true - true - true - - - - - - - - - - - - - - - - - - - - + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {2494B3F2-585E-4A2E-A013-20A15AE25B9E} + wCenterWindow + 10.0.17763.0 + + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + + + Level3 + Disabled + true + true + + + Windows + false + + + + + Level3 + Disabled + true + true + + + Console + + + + + Level3 + MaxSpeed + true + true + true + true + MultiThreaded + + + Windows + true + true + HighestAvailable + false + + + + + Level3 + MaxSpeed + true + true + true + true + + + Console + true + true + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/wCenterWindow/wCenterWindow.vcxproj.filters b/wCenterWindow/wCenterWindow.vcxproj.filters index 88303ad..04f26e0 100644 --- a/wCenterWindow/wCenterWindow.vcxproj.filters +++ b/wCenterWindow/wCenterWindow.vcxproj.filters @@ -1,46 +1,38 @@ - - - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx - - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hh;hpp;hxx;hm;inl;inc;ipp;xsd - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - - - - - Файлы заголовков - - - Файлы заголовков - - - Файлы заголовков - - - Файлы заголовков - - - - - Исходные файлы - - - - - Файлы ресурсов - - - - - Файлы ресурсов - - + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + + + Resource Files + + \ No newline at end of file diff --git a/wCenterWindow/wCenterWindow.vcxproj.user b/wCenterWindow/wCenterWindow.vcxproj.user new file mode 100644 index 0000000..6e2aec7 --- /dev/null +++ b/wCenterWindow/wCenterWindow.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file