2 Commits
v1.4 ... v1.5

Author SHA1 Message Date
f8b925d0f8 Update Readme.md 2020-01-24 15:06:19 +03:00
35f791d7a0 Added 'Hide Icon' option to menu. 2020-01-24 15:03:52 +03:00
4 changed files with 97 additions and 95 deletions

View File

@@ -1,6 +1,6 @@
# wCenterWindow # wCenterWindow
This program centers the current active window by a `LCTRL + LWIN + C` hotkey. This program centers the current active window by a 'LCTRL + LWIN + C' hotkey.
`LCTRL + LWIN + I` hotkey - hide tray icon. 'LCTRL + LWIN + I' hotkey - hide tray icon.
You can add `/hide` option to command line for hiding trayicon at startup. You can also use commandline option '/hide' for hide trayicon at startup.

View File

@@ -1,18 +1,19 @@
// resource.h // resource.h
#define IDS_APP_TITLE 100 #define IDS_APP_TITLE 100
#define IDS_CLASSNAME 101 #define IDS_CLASSNAME 101
#define IDS_HELPTEXT 102 #define IDS_ABOUT 102
#define IDI_TRAYICON 103 #define IDI_TRAYICON 103
#define IDR_MENU 104 #define IDR_MENU 104
#define ID_POPUPMENU_HELP 105 #define ID_POPUPMENU_ICON 105
#define ID_POPUPMENU_EXIT 106 #define ID_POPUPMENU_ABOUT 106
#define IDS_ERR_MAIN 107 #define ID_POPUPMENU_EXIT 107
#define IDS_ERR_WND 108 #define IDS_ERR_MAIN 108
#define IDS_ERR_ICON 109 #define IDS_ERR_WND 109
#define IDS_ERR_MENU 110 #define IDS_ERR_ICON 110
#define IDS_ERR_POPUP 111 #define IDS_ERR_MENU 111
#define IDS_ERR_HOOK 112 #define IDS_ERR_POPUP 112
#define IDS_RUNNING 113 #define IDS_ERR_HOOK 113
#define IDS_RUNNING 114
#define IDC_STATIC -1 #define IDC_STATIC -1
// Next default values for new objects // Next default values for new objects

View File

@@ -3,14 +3,14 @@
#include "framework.h" #include "framework.h"
#include "wCenterWindow.h" #include "wCenterWindow.h"
#define MAX_LOADSTRING 80 #define MAX_LOADSTRING 32
#define WM_WCW WM_USER + 0x7F00 #define WM_WCW WM_USER + 0x7F00
// Глобальные переменные: // Глобальные переменные:
HINSTANCE hInst; // Текущий экземпляр HINSTANCE hInst; // Текущий экземпляр
WCHAR szTitle[MAX_LOADSTRING]; // Текст строки заголовка WCHAR szTitle[MAX_LOADSTRING]; // Текст строки заголовка
WCHAR szClass[MAX_LOADSTRING]; // Имя класса главного окна WCHAR szClass[MAX_LOADSTRING]; // Имя класса главного окна
WCHAR szHelp[MAX_LOADSTRING]; // Текст описания WCHAR szAbout[MAX_LOADSTRING * 9]; // Текст описания
HHOOK KeyboardHook; HHOOK KeyboardHook;
HICON hIcon; HICON hIcon;
HMENU hMenu, hPopup; HMENU hMenu, hPopup;
@@ -23,18 +23,10 @@ int dtCenterX, dtCenterY;
// Прототипы функций // Прототипы функций
ATOM MyRegisterClass(HINSTANCE); ATOM MyRegisterClass(HINSTANCE);
VOID ShowError(HINSTANCE, UINT); VOID HandlingTrayIcon();
VOID ShowError(UINT);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK KeyboardHookProc(int, WPARAM, LPARAM); LRESULT CALLBACK KeyboardHookProc(int, WPARAM, LPARAM);
BOOL CreateTrayIcon();
VOID parseCmdLine()
{
int nArgs = 0;
LPWSTR *szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
(nArgs >= 2 && 0 == lstrcmpiW(szArglist[1], L"/hide")) ? showIcon = FALSE : showIcon = TRUE;
LocalFree(szArglist);
}
// Точка входа // Точка входа
int APIENTRY wWinMain(_In_ HINSTANCE hInstance, int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
@@ -45,57 +37,40 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine); UNREFERENCED_PARAMETER(lpCmdLine);
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); hInst = hInstance;
LoadStringW(hInstance, IDS_CLASSNAME, szClass, MAX_LOADSTRING);
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, _countof(szTitle));
LoadStringW(hInstance, IDS_CLASSNAME, szClass, _countof(szClass));
if (FindWindow(szClass, NULL)) if (FindWindow(szClass, NULL))
{ {
ShowError(hInstance, IDS_RUNNING); ShowError(IDS_RUNNING);
return FALSE; return FALSE;
} }
MyRegisterClass(hInstance);
MyRegisterClass(hInstance);
hWnd = CreateWindowExW(0, szClass, szTitle, 0, 0, 0, 0, 0, NULL, NULL, hInstance, NULL); hWnd = CreateWindowExW(0, szClass, szTitle, 0, 0, 0, 0, 0, NULL, NULL, hInstance, NULL);
if (!hWnd) if (!hWnd)
{ {
ShowError(hInstance, IDS_ERR_WND); ShowError(IDS_ERR_WND);
return FALSE; return FALSE;
} }
parseCmdLine(); nid.cbSize = sizeof(NOTIFYICONDATA);
if (showIcon) nid.hWnd = hWnd;
{ nid.uVersion = NOTIFYICON_VERSION;
if (!CreateTrayIcon()) nid.uCallbackMessage = WM_WCW;
{ nid.hIcon = hIcon;
ShowError(hInstance, IDS_ERR_ICON); nid.uID = IDI_TRAYICON;
SendMessage(hWnd, WM_CLOSE, NULL, NULL); nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
} nid.dwInfoFlags = NIIF_INFO;
} StringCchCopy(nid.szTip, _countof(nid.szTip), szTitle);
hMenu = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MENU)); int nArgs = 0;
if (!hMenu) LPWSTR *szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
{ (nArgs >= 2 && 0 == lstrcmpiW(szArglist[1], L"/hide")) ? showIcon = FALSE : showIcon = TRUE;
ShowError(hInstance, IDS_ERR_MENU); LocalFree(szArglist);
SendMessage(hWnd, WM_CLOSE, NULL, NULL); HandlingTrayIcon();
}
hPopup = GetSubMenu(hMenu, 0);
if (!hPopup)
{
ShowError(hInstance, IDS_ERR_POPUP);
SendMessage(hWnd, WM_CLOSE, NULL, NULL);
}
KeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProc, hInstance, NULL);
if (!KeyboardHook)
{
ShowError(hInstance, IDS_ERR_HOOK);
SendMessage(hWnd, WM_CLOSE, NULL, NULL);
}
LoadStringW(hInstance, IDS_HELPTEXT, szHelp, MAX_LOADSTRING);
RECT dtrc = { 0 };
SystemParametersInfo(SPI_GETWORKAREA, NULL, &dtrc, FALSE);
dtCenterX = dtrc.right / 2, dtCenterY = dtrc.bottom / 2;
MSG msg; MSG msg;
BOOL bRet; BOOL bRet;
@@ -104,7 +79,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
{ {
if (bRet == -1) if (bRet == -1)
{ {
ShowError(hInstance, IDS_ERR_MAIN); ShowError(IDS_ERR_MAIN);
return -1; return -1;
} }
else else
@@ -134,6 +109,35 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{ {
switch (message) 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: case WM_WCW:
if (lParam == WM_RBUTTONDOWN && wParam == IDI_TRAYICON) if (lParam == WM_RBUTTONDOWN && wParam == IDI_TRAYICON)
{ {
@@ -141,10 +145,15 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
POINT pt; POINT pt;
GetCursorPos(&pt); GetCursorPos(&pt);
int idMenu = TrackPopupMenu(hPopup, TPM_RETURNCMD, pt.x, pt.y, 0, hWnd, NULL); int idMenu = TrackPopupMenu(hPopup, TPM_RETURNCMD, pt.x, pt.y, 0, hWnd, NULL);
if (idMenu == ID_POPUPMENU_HELP && !pressed) if (idMenu == ID_POPUPMENU_ICON)
{
showIcon = FALSE;
HandlingTrayIcon();
}
if (idMenu == ID_POPUPMENU_ABOUT && !pressed)
{ {
pressed = TRUE; pressed = TRUE;
if (MessageBox(hWnd, szHelp, szTitle, MB_OK | MB_TOPMOST) == IDOK) pressed = FALSE; if (MessageBox(hWnd, szAbout, szTitle, MB_OK | MB_TOPMOST) == IDOK) pressed = FALSE;
} }
if (idMenu == ID_POPUPMENU_EXIT) SendMessage(hWnd, WM_CLOSE, NULL, NULL); if (idMenu == ID_POPUPMENU_EXIT) SendMessage(hWnd, WM_CLOSE, NULL, NULL);
} }
@@ -183,18 +192,7 @@ LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{ {
pressed = TRUE; pressed = TRUE;
showIcon = !showIcon; showIcon = !showIcon;
if (showIcon) HandlingTrayIcon();
{
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 if (bLCTRL && bLWIN && pkhs->vkCode == 0x43 && !pressed) // 'C' key
@@ -233,23 +231,26 @@ LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
return CallNextHookEx(NULL, nCode, wParam, lParam); return CallNextHookEx(NULL, nCode, wParam, lParam);
} }
BOOL CreateTrayIcon() VOID HandlingTrayIcon()
{ {
nid.cbSize = sizeof(NOTIFYICONDATA); if (showIcon)
nid.hWnd = hWnd; {
nid.uVersion = NOTIFYICON_VERSION; if (!Shell_NotifyIcon(NIM_ADD, &nid))
nid.uCallbackMessage = WM_WCW; {
nid.hIcon = hIcon; ShowError(IDS_ERR_ICON);
nid.uID = IDI_TRAYICON; showIcon = FALSE;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; }
nid.dwInfoFlags = NIIF_INFO; }
StringCchCopy(nid.szTip, sizeof(nid.szTip), szTitle); else
return Shell_NotifyIcon(NIM_ADD, &nid); {
Shell_NotifyIcon(NIM_DELETE, &nid);
}
} }
VOID ShowError(HINSTANCE hInstance, UINT uID) VOID ShowError(UINT uID)
{ {
WCHAR szErrorText[MAX_LOADSTRING]; // Текст ошибки WCHAR szErrorText[MAX_LOADSTRING]; // Текст ошибки
LoadStringW(hInstance, uID, szErrorText, MAX_LOADSTRING); LoadStringW(hInst, uID, szErrorText, _countof(szErrorText));
MessageBox(hWnd, szErrorText, szTitle, MB_OK | MB_ICONERROR); MessageBox(hWnd, szErrorText, szTitle, MB_OK | MB_ICONERROR);
} }

Binary file not shown.