Added more logging

This commit is contained in:
2021-12-29 20:15:15 +03:00
parent 690f621f5b
commit d1ab2b86e2
3 changed files with 225 additions and 223 deletions

View File

@@ -11,8 +11,15 @@
#define PATH_LEN 1024 #define PATH_LEN 1024
std::wofstream logfile; std::wofstream logfile;
extern LPVOID szBuffer;
namespace fs = std::filesystem; namespace fs = std::filesystem;
std::wstring PrintTitle() {
wchar_t szWinTitle[2048];
StringCchPrintf(szWinTitle, 2048, L"%s", szBuffer);
return szWinTitle;
}
std::wstring GetTimeStamp() { std::wstring GetTimeStamp() {
SYSTEMTIME lt; SYSTEMTIME lt;
GetLocalTime(&lt); GetLocalTime(&lt);
@@ -40,7 +47,9 @@ void OpenLogFile() {
bak_path.replace_extension(L".bak"); bak_path.replace_extension(L".bak");
if (fs::exists(log_path)) fs::rename(log_path, bak_path); if (fs::exists(log_path)) fs::rename(log_path, bak_path);
#ifdef _DEBUG
log_path = L"d:\\test.log";
#endif
logfile.open(log_path); logfile.open(log_path);
if (logfile.is_open()) { if (logfile.is_open()) {
diag_log(L"Starting logging."); diag_log(L"Starting logging.");

View File

@@ -4,6 +4,7 @@
extern std::wofstream logfile; extern std::wofstream logfile;
std::wstring GetTimeStamp(); std::wstring GetTimeStamp();
std::wstring PrintTitle();
template <typename T1> template <typename T1>
void diag_log(T1 arg1) { void diag_log(T1 arg1) {
@@ -26,6 +27,10 @@ void diag_log(T1 arg1, T2 arg2, T3 arg3) {
template <typename T1, typename T2, typename T3, typename T4> template <typename T1, typename T2, typename T3, typename T4>
void diag_log(T1 arg1, T2 arg2, T3 arg3, T4 arg4) { void diag_log(T1 arg1, T2 arg2, T3 arg3, T4 arg4) {
//std::wcout << GetTimeStamp() << arg1 << ' ' << arg2 << ' ' << arg3 << ' ' << arg4 << std::endl; //std::wcout << GetTimeStamp() << arg1 << ' ' << arg2 << ' ' << arg3 << ' ' << arg4 << std::endl;
//if (typeid(T4) == typeid(WCHAR)) {
// logfile << GetTimeStamp() << arg1 << ' ' << arg2 << ' ' << arg3 << ' ' << PrintTitle() << std::endl;
// return;
//}
logfile << GetTimeStamp() << arg1 << ' ' << arg2 << ' ' << arg3 << ' ' << arg4 << std::endl; logfile << GetTimeStamp() << arg1 << ' ' << arg2 << ' ' << arg3 << ' ' << arg4 << std::endl;
} }

View File

@@ -1,8 +1,8 @@
// wCenterWindow, v2.3 // wCenterWindow, v2.3
// //
// TODO: More verbose logs // TODO: More verbose logs - partially done.
// TODO: More verbose error messages // TODO: More verbose error messages
// TODO: Window's title in logs // TODO: Window's title in logs (Impossible?)
#include "framework.h" #include "framework.h"
#include "wCenterWindow.h" #include "wCenterWindow.h"
@@ -12,6 +12,7 @@
#define KEY_C 0x43 #define KEY_C 0x43
#define KEY_V 0x56 #define KEY_V 0x56
#define BUF_LEN 4096
#define MAX_LOADSTRING 50 #define MAX_LOADSTRING 50
#define WM_WCW 0x8F00 #define WM_WCW 0x8F00
@@ -23,6 +24,7 @@ TCHAR szAbout[MAX_LOADSTRING * 12]; // Description text
TCHAR szWinTitle[256]; TCHAR szWinTitle[256];
TCHAR szWinClass[256]; TCHAR szWinClass[256];
TCHAR szWinCore[] = TEXT("Windows.UI.Core.CoreWindow"); TCHAR szWinCore[] = TEXT("Windows.UI.Core.CoreWindow");
HANDLE hHeap = NULL;
HHOOK hMouseHook = NULL, hKbdHook = NULL; // Hook's handles HHOOK hMouseHook = NULL, hKbdHook = NULL; // Hook's handles
HICON hIcon = NULL; HICON hIcon = NULL;
HMENU hMenu = NULL, hPopup = NULL; HMENU hMenu = NULL, hPopup = NULL;
@@ -35,6 +37,8 @@ NOTIFYICONDATA nid = { 0 };
LPKBDLLHOOKSTRUCT pkhs; LPKBDLLHOOKSTRUCT pkhs;
MENUITEMINFO mii; MENUITEMINFO mii;
LPVOID szBuffer;
// Forward declarations of functions included in this code module: // Forward declarations of functions included in this code module:
VOID HandlingTrayIcon(); VOID HandlingTrayIcon();
VOID ShowError(UINT); VOID ShowError(UINT);
@@ -47,8 +51,7 @@ INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
ATOM MyRegisterClass(HINSTANCE hInstance) ATOM MyRegisterClass(HINSTANCE hInstance) {
{
WNDCLASSEX wcex = { 0 }; WNDCLASSEX wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX); wcex.cbSize = sizeof(WNDCLASSEX);
wcex.lpfnWndProc = WndProc; wcex.lpfnWndProc = WndProc;
@@ -61,9 +64,8 @@ ATOM MyRegisterClass(HINSTANCE hInstance)
return RegisterClassEx(&wcex); return RegisterClassEx(&wcex);
} }
VOID MoveWindowToMonitorCenter(HWND hwnd, BOOL bWorkArea, BOOL bResize) VOID MoveWindowToMonitorCenter(HWND hwnd, BOOL bWorkArea, BOOL bResize) {
{ diag_log(L"Entering MoveWindowToMonitorCenter(): hwnd =", hwnd, L"Title:", (LPWSTR)szBuffer);
diag_log(L"Entering MoveWindowToMonitorCenter(): hwnd =", hwnd);
RECT fgwrc; RECT fgwrc;
GetWindowRect(hwnd, &fgwrc); GetWindowRect(hwnd, &fgwrc);
@@ -76,15 +78,12 @@ VOID MoveWindowToMonitorCenter(HWND hwnd, BOOL bWorkArea, BOOL bResize)
mi.cbSize = sizeof(MONITORINFO); mi.cbSize = sizeof(MONITORINFO);
GetMonitorInfo(MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST), &mi); GetMonitorInfo(MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST), &mi);
RECT area; RECT area;
if (bWorkArea) if (bWorkArea) {
{
area.bottom = mi.rcWork.bottom; area.bottom = mi.rcWork.bottom;
area.left = mi.rcWork.left; area.left = mi.rcWork.left;
area.right = mi.rcWork.right; area.right = mi.rcWork.right;
area.top = mi.rcWork.top; area.top = mi.rcWork.top;
} } else {
else
{
area.bottom = mi.rcMonitor.bottom; area.bottom = mi.rcMonitor.bottom;
area.left = mi.rcMonitor.left; area.left = mi.rcMonitor.left;
area.right = mi.rcMonitor.right; area.right = mi.rcMonitor.right;
@@ -110,8 +109,7 @@ VOID MoveWindowToMonitorCenter(HWND hwnd, BOOL bWorkArea, BOOL bResize)
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {
{
UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine); UNREFERENCED_PARAMETER(lpCmdLine);
@@ -123,16 +121,14 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCm
LoadString(hInstance, IDS_APP_TITLE, szTitle, _countof(szTitle)); LoadString(hInstance, IDS_APP_TITLE, szTitle, _countof(szTitle));
LoadString(hInstance, IDS_CLASSNAME, szClass, _countof(szClass)); LoadString(hInstance, IDS_CLASSNAME, szClass, _countof(szClass));
if (FindWindow(szClass, NULL)) if (FindWindow(szClass, NULL)) {
{
ShowError(IDS_RUNNING); ShowError(IDS_RUNNING);
return FALSE; return FALSE;
} }
MyRegisterClass(hInstance); MyRegisterClass(hInstance);
hWnd = CreateWindowEx(0, szClass, szTitle, 0, 0, 0, 0, 0, NULL, NULL, hInstance, NULL); hWnd = CreateWindowEx(0, szClass, szTitle, 0, 0, 0, 0, 0, NULL, NULL, hInstance, NULL);
if (!hWnd) if (!hWnd) {
{
ShowError(IDS_ERR_WND); ShowError(IDS_ERR_WND);
return FALSE; return FALSE;
} }
@@ -146,6 +142,9 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCm
LocalFree(szArglist); LocalFree(szArglist);
HandlingTrayIcon(); HandlingTrayIcon();
hHeap = GetProcessHeap();
szBuffer = HeapAlloc(hHeap, HEAP_ZERO_MEMORY, BUF_LEN);
hTaskBar = FindWindow(TEXT("Shell_TrayWnd"), NULL); hTaskBar = FindWindow(TEXT("Shell_TrayWnd"), NULL);
hProgman = FindWindow(TEXT("Progman"), NULL); hProgman = FindWindow(TEXT("Progman"), NULL);
hDesktop = GetDesktopWindow(); hDesktop = GetDesktopWindow();
@@ -153,15 +152,11 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCm
MSG msg; MSG msg;
BOOL bRet; BOOL bRet;
while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) {
{ if (bRet == -1) {
if (bRet == -1)
{
ShowError(IDS_ERR_MAIN); ShowError(IDS_ERR_MAIN);
return -1; return -1;
} } else {
else
{
TranslateMessage(&msg); TranslateMessage(&msg);
DispatchMessage(&msg); DispatchMessage(&msg);
} }
@@ -174,110 +169,110 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCm
diag_log(L"Quiting WinMain(). msg.wParam =", (int)msg.wParam); diag_log(L"Quiting WinMain(). msg.wParam =", (int)msg.wParam);
CloseLogFile(); CloseLogFile();
HeapFree(hHeap, NULL, szBuffer);
return (int)msg.wParam; return (int)msg.wParam;
} }
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
{ switch (message) {
switch (message) case WM_CREATE:
{ {
case WM_CREATE: hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_MENU));
{ if (!hMenu) {
hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_MENU)); diag_log(L"Loading context menu failed!");
if (!hMenu) ShowError(IDS_ERR_MENU);
{ SendMessage(hWnd, WM_CLOSE, NULL, NULL);
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);
hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, hInst, NULL);
if (!hMouseHook)
{
ShowError(IDS_ERR_HOOK);
SendMessage(hWnd, WM_CLOSE, NULL, NULL);
}
hKbdHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProc, hInst, NULL);
if (!hKbdHook)
{
ShowError(IDS_ERR_HOOK);
SendMessage(hWnd, WM_CLOSE, NULL, NULL);
}
LoadString(hInst, IDS_ABOUT, szAbout, _countof(szAbout));
} }
break; diag_log(L"Context menu successfully loaded");
case WM_WCW: hPopup = GetSubMenu(hMenu, 0);
{ if (!hPopup) {
if ((lParam == WM_RBUTTONDOWN || lParam == WM_LBUTTONDOWN) && wParam == IDI_TRAYICON) diag_log(L"Creating popup menu failed!");
{ ShowError(IDS_ERR_POPUP);
SetForegroundWindow(hWnd); SendMessage(hWnd, WM_CLOSE, NULL, NULL);
POINT pt; }
GetCursorPos(&pt); diag_log(L"Popup menu successfully created");
int idMenu = TrackPopupMenu(hPopup, TPM_RETURNCMD, pt.x, pt.y, 0, hWnd, NULL);
if (idMenu == ID_POPUPMENU_ICON) mii.cbSize = sizeof(MENUITEMINFO);
{ mii.fMask = MIIM_STATE;
bShowIcon = FALSE; bWorkArea ? mii.fState = MFS_CHECKED : mii.fState = MFS_UNCHECKED;
HandlingTrayIcon(); SetMenuItemInfo(hPopup, ID_POPUPMENU_AREA, FALSE, &mii);
}
if (idMenu == ID_POPUPMENU_AREA) nid.cbSize = sizeof(NOTIFYICONDATA);
{ nid.hWnd = hWnd;
bWorkArea = !bWorkArea; nid.uVersion = NOTIFYICON_VERSION;
bWorkArea ? mii.fState = MFS_CHECKED : mii.fState = MFS_UNCHECKED; nid.uCallbackMessage = WM_WCW;
SetMenuItemInfo(hPopup, ID_POPUPMENU_AREA, FALSE, &mii); nid.hIcon = hIcon;
} nid.uID = IDI_TRAYICON;
if (idMenu == ID_POPUPMENU_ABOUT && !bKPressed) nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
{ nid.dwInfoFlags = NIIF_INFO;
bKPressed = TRUE; StringCchCopy(nid.szTip, _countof(nid.szTip), szTitle);
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, (DLGPROC)About);
bKPressed = FALSE; hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, hInst, NULL);
} if (!hMouseHook) {
if (idMenu == ID_POPUPMENU_EXIT) SendMessage(hWnd, WM_CLOSE, NULL, NULL); diag_log(L"Creating mouse hook failed!");
ShowError(IDS_ERR_HOOK);
SendMessage(hWnd, WM_CLOSE, NULL, NULL);
}
diag_log(L"Mouse hook was successfully set");
hKbdHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProc, hInst, NULL);
if (!hKbdHook) {
diag_log(L"Creating keyboard hook failed!");
ShowError(IDS_ERR_HOOK);
SendMessage(hWnd, WM_CLOSE, NULL, NULL);
}
diag_log(L"Keyboard hook was successfully set");
LoadString(hInst, IDS_ABOUT, szAbout, _countof(szAbout));
}
break;
case WM_WCW:
{
if ((lParam == WM_RBUTTONDOWN || lParam == WM_LBUTTONDOWN) && 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);
diag_log(L"Changed 'Use workarea' option to", bWorkArea);
}
if (idMenu == ID_POPUPMENU_ABOUT && !bKPressed) {
bKPressed = TRUE;
diag_log(L"Opening 'About' dialog");
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, (DLGPROC)About);
bKPressed = FALSE;
}
if (idMenu == ID_POPUPMENU_EXIT) SendMessage(hWnd, WM_CLOSE, NULL, NULL);
} }
break; }
break;
case WM_DESTROY: case WM_DESTROY:
{ {
PostQuitMessage(0); PostQuitMessage(0);
} }
break; break;
default: default:
return DefWindowProc(hWnd, message, wParam, lParam); return DefWindowProc(hWnd, message, wParam, lParam);
} }
return 0; return 0;
} }
LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam) LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
{
if (wParam == WM_MBUTTONUP) bMPressed = FALSE; if (wParam == WM_MBUTTONUP) bMPressed = FALSE;
if (wParam == WM_MBUTTONDOWN && bLCTRL && bLWIN && !bMPressed) if (wParam == WM_MBUTTONDOWN && bLCTRL && bLWIN && !bMPressed) {
{ diag_log(L"Pressed LCTRL + LWIN + MMB");
bMPressed = TRUE; bMPressed = TRUE;
hFgWnd = GetForegroundWindow(); hFgWnd = GetForegroundWindow();
BOOL bApproved = CheckWindow(hFgWnd); BOOL bApproved = CheckWindow(hFgWnd);
@@ -287,24 +282,22 @@ LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
return CallNextHookEx(NULL, nCode, wParam, lParam); return CallNextHookEx(NULL, nCode, wParam, lParam);
} }
LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
{
pkhs = (KBDLLHOOKSTRUCT*)lParam; pkhs = (KBDLLHOOKSTRUCT*)lParam;
if (wParam == WM_KEYUP) if (wParam == WM_KEYUP) {
{
if (pkhs->vkCode == VK_LCONTROL) bLCTRL = FALSE; if (pkhs->vkCode == VK_LCONTROL) bLCTRL = FALSE;
if (pkhs->vkCode == VK_LWIN) bLWIN = FALSE; if (pkhs->vkCode == VK_LWIN) bLWIN = FALSE;
bKPressed = FALSE; bKPressed = FALSE;
} }
if (wParam == WM_KEYDOWN) if (wParam == WM_KEYDOWN) {
{
if (pkhs->vkCode == VK_LCONTROL) bLCTRL = TRUE; if (pkhs->vkCode == VK_LCONTROL) bLCTRL = TRUE;
if (pkhs->vkCode == VK_LWIN) bLWIN = TRUE; if (pkhs->vkCode == VK_LWIN) bLWIN = TRUE;
if (bLCTRL && bLWIN && pkhs->vkCode == KEY_I && !bKPressed) // 'I' key if (bLCTRL && bLWIN && pkhs->vkCode == KEY_I && !bKPressed) // 'I' key
{ {
diag_log(L"Pressed LCTRL + LWIN + I");
bKPressed = TRUE; bKPressed = TRUE;
bShowIcon = !bShowIcon; bShowIcon = !bShowIcon;
HandlingTrayIcon(); HandlingTrayIcon();
@@ -313,6 +306,7 @@ LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
if (bLCTRL && bLWIN && pkhs->vkCode == KEY_C && !bKPressed && !bKEYV) // 'C' key if (bLCTRL && bLWIN && pkhs->vkCode == KEY_C && !bKPressed && !bKEYV) // 'C' key
{ {
diag_log(L"Pressed LCTRL + LWIN + C");
bKPressed = TRUE; bKPressed = TRUE;
hFgWnd = GetForegroundWindow(); hFgWnd = GetForegroundWindow();
BOOL bApproved = CheckWindow(hFgWnd); BOOL bApproved = CheckWindow(hFgWnd);
@@ -323,11 +317,14 @@ LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
if (bLCTRL && bLWIN && pkhs->vkCode == KEY_V && !bKPressed && !bKEYV) // 'V' key if (bLCTRL && bLWIN && pkhs->vkCode == KEY_V && !bKPressed && !bKEYV) // 'V' key
{ {
diag_log(L"Pressed LCTRL + LWIN + V");
bKPressed = TRUE; bKEYV = TRUE; bKPressed = TRUE; bKEYV = TRUE;
hFgWnd = GetForegroundWindow(); hFgWnd = GetForegroundWindow();
BOOL bApproved = CheckWindow(hFgWnd); BOOL bApproved = CheckWindow(hFgWnd);
if (bApproved) DialogBox(hInst, MAKEINTRESOURCE(IDD_MANUAL_EDITING), hWnd, (DLGPROC)DlgProc); if (bApproved) {
else hFgWnd = NULL; diag_log(L"Opening 'Manual editing' dialog");
DialogBox(hInst, MAKEINTRESOURCE(IDD_MANUAL_EDITING), hWnd, (DLGPROC)DlgProc);
} else hFgWnd = NULL;
bKEYV = FALSE; bKEYV = FALSE;
return TRUE; return TRUE;
} }
@@ -335,141 +332,132 @@ LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
return CallNextHookEx(NULL, nCode, wParam, lParam); return CallNextHookEx(NULL, nCode, wParam, lParam);
} }
BOOL CALLBACK DlgProc(HWND hDlg, UINT dlgmsg, WPARAM wParam, LPARAM lParam) BOOL CALLBACK DlgProc(HWND hDlg, UINT dlgmsg, WPARAM wParam, LPARAM lParam) {
{
int x, y, w, h; int x, y, w, h;
switch (dlgmsg) switch (dlgmsg) {
case WM_INITDIALOG:
{ {
case WM_INITDIALOG: SetWindowText(hDlg, szTitle);
GetWindowText(hFgWnd, szWinTitle, _countof(szWinTitle));
GetClassName(hFgWnd, szWinClass, _countof(szWinClass));
GetWindowRect(hFgWnd, &rcFW);
x = rcFW.left;
y = rcFW.top;
w = rcFW.right - rcFW.left;
h = rcFW.bottom - rcFW.top;
SetDlgItemInt(hDlg, IDC_EDIT_X, x, TRUE);
SetDlgItemInt(hDlg, IDC_EDIT_Y, y, TRUE);
SetDlgItemInt(hDlg, IDC_EDIT_WIDTH, w, FALSE);
SetDlgItemInt(hDlg, IDC_EDIT_HEIGHT, h, FALSE);
SetDlgItemText(hDlg, IDC_EDIT_TITLE, szWinTitle);
SetDlgItemText(hDlg, IDC_EDIT_CLASS, szWinClass);
UpdateWindow(hDlg);
break;
}
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDC_BUTTON_SET:
{ {
SetWindowText(hDlg, szTitle); x = GetDlgItemInt(hDlg, IDC_EDIT_X, NULL, TRUE);
GetWindowText(hFgWnd, szWinTitle, _countof(szWinTitle)); y = GetDlgItemInt(hDlg, IDC_EDIT_Y, NULL, TRUE);
GetClassName(hFgWnd, szWinClass, _countof(szWinClass)); w = GetDlgItemInt(hDlg, IDC_EDIT_WIDTH, NULL, FALSE);
GetWindowRect(hFgWnd, &rcFW); h = GetDlgItemInt(hDlg, IDC_EDIT_HEIGHT, NULL, FALSE);
x = rcFW.left; SendMessage(hFgWnd, WM_ENTERSIZEMOVE, NULL, NULL);
y = rcFW.top; MoveWindow(hFgWnd, x, y, w, h, TRUE);
w = rcFW.right - rcFW.left; SendMessage(hFgWnd, WM_EXITSIZEMOVE, NULL, NULL);
h = rcFW.bottom - rcFW.top; return TRUE;
SetDlgItemInt(hDlg, IDC_EDIT_X, x, TRUE);
SetDlgItemInt(hDlg, IDC_EDIT_Y, y, TRUE);
SetDlgItemInt(hDlg, IDC_EDIT_WIDTH, w, FALSE);
SetDlgItemInt(hDlg, IDC_EDIT_HEIGHT, h, FALSE);
SetDlgItemText(hDlg, IDC_EDIT_TITLE, szWinTitle);
SetDlgItemText(hDlg, IDC_EDIT_CLASS, szWinClass);
UpdateWindow(hDlg);
break; break;
} }
case WM_COMMAND: case IDCANCEL:
switch (LOWORD(wParam)) case IDC_BUTTON_CLOSE:
{
case IDC_BUTTON_SET:
{
x = GetDlgItemInt(hDlg, IDC_EDIT_X, NULL, TRUE);
y = GetDlgItemInt(hDlg, IDC_EDIT_Y, NULL, TRUE);
w = GetDlgItemInt(hDlg, IDC_EDIT_WIDTH, NULL, FALSE);
h = GetDlgItemInt(hDlg, IDC_EDIT_HEIGHT, NULL, FALSE);
SendMessage(hFgWnd, WM_ENTERSIZEMOVE, NULL, NULL);
MoveWindow(hFgWnd, x, y, w, h, TRUE);
SendMessage(hFgWnd, WM_EXITSIZEMOVE, NULL, NULL);
return TRUE;
break;
}
case IDCANCEL:
case IDC_BUTTON_CLOSE:
{
EndDialog(hDlg, LOWORD(wParam));
break;
}
}
}
return FALSE;
}
BOOL CheckWindow(HWND hFW)
{
GetClassName(hFW, szWinClass, _countof(szWinClass));
if (hFW)
{
if (_tcscmp(szWinClass, szWinCore) != 0)
{ {
if (hFW != hDesktop && hFW != hTaskBar && hFW != hProgman) EndDialog(hDlg, LOWORD(wParam));
{ diag_log(L"Closing 'Manual editing' dialog");
if (!IsIconic(hFW) && !IsZoomed(hFW)) return TRUE; break;
else ShowError(IDS_ERR_MAXMIN); }
}
} }
} }
return FALSE; return FALSE;
} }
VOID HandlingTrayIcon() BOOL CheckWindow(HWND hFW) {
{ diag_log(L"Entering CheckWindow(). hwnd=", hFW);
GetClassName(hFW, szWinClass, _countof(szWinClass));
if (hFW) {
if (_tcscmp(szWinClass, szWinCore) != 0) {
if (hFW != hDesktop && hFW != hTaskBar && hFW != hProgman) {
if (!IsIconic(hFW) && !IsZoomed(hFW)) {
GetWindowText(hFW, (LPTSTR)szBuffer, BUF_LEN - sizeof(TCHAR));
diag_log(L"Window with hwnd=", hFW, L"is approved");
return TRUE;
} else ShowError(IDS_ERR_MAXMIN);
}
}
}
diag_log(L"Window with hwnd=", hFW, L"is not approved!");
diag_log(L"Quiting CheckWindow()");
return FALSE;
}
VOID HandlingTrayIcon() {
diag_log(L"Entering HandlingTrayIcon(). bShowIcon =", bShowIcon); diag_log(L"Entering HandlingTrayIcon(). bShowIcon =", bShowIcon);
if (bShowIcon) if (bShowIcon) {
{ if (!Shell_NotifyIcon(NIM_ADD, &nid)) {
if (!Shell_NotifyIcon(NIM_ADD, &nid))
{
diag_log(L"GetLastError():", GetLastError()); diag_log(L"GetLastError():", GetLastError());
ShowError(IDS_ERR_ICON); ShowError(IDS_ERR_ICON);
bShowIcon = FALSE; bShowIcon = FALSE;
} }
} } else {
else
{
Shell_NotifyIcon(NIM_DELETE, &nid); Shell_NotifyIcon(NIM_DELETE, &nid);
} }
diag_log(L"Quiting HandlingTrayIcon()"); diag_log(L"Quiting HandlingTrayIcon()");
} }
VOID ShowError(UINT uID) VOID ShowError(UINT uID) {
{
TCHAR szErrorText[MAX_LOADSTRING]; // Error's text TCHAR szErrorText[MAX_LOADSTRING]; // Error's text
LoadString(hInst, uID, szErrorText, _countof(szErrorText)); LoadString(hInst, uID, szErrorText, _countof(szErrorText));
MessageBox(hWnd, szErrorText, szTitle, MB_OK | MB_ICONERROR | MB_TOPMOST); MessageBox(hWnd, szErrorText, szTitle, MB_OK | MB_ICONERROR | MB_TOPMOST);
} }
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) {
{
INITCOMMONCONTROLSEX icex; INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX); icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_LINK_CLASS; icex.dwICC = ICC_LINK_CLASS;
InitCommonControlsEx(&icex); InitCommonControlsEx(&icex);
switch (message) switch (message) {
case WM_INITDIALOG:
{ {
case WM_INITDIALOG: SetDlgItemText(hDlg, IDC_ABOUTHELP, szAbout);
{ return (INT_PTR)TRUE;
SetDlgItemText(hDlg, IDC_ABOUTHELP, szAbout); break;
}
case WM_NOTIFY:
{
LPNMHDR pNMHdr = (LPNMHDR)lParam;
switch (pNMHdr->code) {
case NM_CLICK:
case NM_RETURN:
if (pNMHdr->idFrom == IDC_DONATIONLINK) {
PNMLINK pNMLink = (PNMLINK)pNMHdr;
LITEM item = pNMLink->item;
ShellExecute(NULL, L"open", item.szUrl, NULL, NULL, SW_SHOW);
diag_log(L"Pressed donation link");
return (INT_PTR)TRUE;
}
break;
}
break;
}
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
EndDialog(hDlg, LOWORD(wParam));
diag_log(L"Closing 'About' dialog");
return (INT_PTR)TRUE; return (INT_PTR)TRUE;
break; break;
} }
case WM_NOTIFY:
{
LPNMHDR pNMHdr = (LPNMHDR)lParam;
switch (pNMHdr->code)
{
case NM_CLICK:
case NM_RETURN:
if (pNMHdr->idFrom == IDC_DONATIONLINK)
{
PNMLINK pNMLink = (PNMLINK)pNMHdr;
LITEM item = pNMLink->item;
ShellExecute(NULL, L"open", item.szUrl, NULL, NULL, SW_SHOW);
return (INT_PTR)TRUE;
}
break;
}
break;
}
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
break;
}
} }
return (INT_PTR)FALSE; return (INT_PTR)FALSE;
} }