Changed Logger.

This commit is contained in:
2023-04-11 19:02:53 +03:00
parent 710d9d9d3e
commit 582cce5b20
6 changed files with 166 additions and 197 deletions

View File

@@ -1,70 +0,0 @@
// wCenterWindow
// Logger.cpp
//
#include "framework.h"
#define TS_LEN 30
std::ofstream logfile;
extern WCHAR szTitle[];
extern LPVOID szBuffer;
std::string GetTimeStamp()
{
SYSTEMTIME lt;
GetLocalTime(&lt);
CHAR ts[TS_LEN];
StringCchPrintfA(ts, TS_LEN, "%d-%02d-%02d %02d:%02d:%02d.%03d - ", lt.wYear, lt.wMonth, lt.wDay, lt.wHour, lt.wMinute, lt.wSecond, lt.wMilliseconds);
return ts;
}
void OpenLogFile()
{
WCHAR lpszPath[MAX_PATH + 1] = { 0 };
DWORD dwPathLength = GetModuleFileNameW(NULL, lpszPath, MAX_PATH);
DWORD dwError = GetLastError();
if (ERROR_INSUFFICIENT_BUFFER == dwError)
{
MessageBoxW(NULL, L"Path to logfile is too long! Working without logging", (LPCWSTR)szTitle, MB_OK | MB_ICONWARNING);
return;
}
if (NULL == dwPathLength)
{
MessageBoxW(NULL, L"Can't get module filename! Working without logging", (LPCWSTR)szTitle, MB_OK | MB_ICONWARNING);
return;
}
std::filesystem::path log_path = lpszPath;
log_path.replace_extension(L".log");
std::filesystem::path bak_path = log_path;
bak_path.replace_extension(L".bak");
if (std::filesystem::exists(log_path)) std::filesystem::rename(log_path, bak_path);
#ifdef _DEBUG
log_path = L"d:\\test.log";
#endif
logfile.open(log_path);
if (logfile.is_open())
{
logfile << std::boolalpha;
diag_log("Start logging");
diag_log("Logfile: ", log_path);
diag_log("Logfile was successfully opened");
}
else
{
MessageBoxW(NULL, L"Can't open logfile! Working without logging", (LPCWSTR)szTitle, MB_OK | MB_ICONWARNING);
}
return;
}
void CloseLogFile()
{
if (logfile)
{
diag_log("End logging");
logfile.close();
}
}

View File

@@ -1,59 +1,71 @@
// wCenterWindow // wLogger v3.0 (Edited version from RBTray project [https://github.com/benbuck/rbtray])
// Logger.h // logger.h
// // Usage: LOG_TO_FILE(L"%s(%d): Log message", TEXT(__FUNCTION__), __LINE__);
#pragma once #pragma once
#include "framework.h" #include "framework.h"
extern std::ofstream logfile; #define DBUFLEN 256
std::string GetTimeStamp(); #define LOG_TO_FILE(fmt, ...) do { StringCchPrintfW(debugBuffer, DBUFLEN, fmt, ##__VA_ARGS__); logfile << GetTimeStamp() << debugBuffer << std::endl; } while (0)
template <typename T1> SYSTEMTIME lt;
void diag_log(T1 arg1) wchar_t debugTimeBuffer[32];
wchar_t debugBuffer[DBUFLEN];
std::wofstream logfile;
extern wchar_t szTitle[];
wchar_t* GetTimeStamp()
{ {
logfile << GetTimeStamp() << arg1 << std::endl; GetLocalTime(&lt);
StringCchPrintfW(debugTimeBuffer, 32, L"%d-%02d-%02d %02d:%02d:%02d.%03d | ", lt.wYear, lt.wMonth, lt.wDay, lt.wHour, lt.wMinute, lt.wSecond, lt.wMilliseconds);
return debugTimeBuffer;
} }
template <typename T1, typename T2> void OpenLogFile()
void diag_log(T1 arg1, T2 arg2)
{ {
logfile << GetTimeStamp() << arg1 << arg2 << std::endl; WCHAR lpszPath[MAX_PATH + 1] = { 0 };
DWORD dwPathLength = GetModuleFileNameW(NULL, lpszPath, MAX_PATH);
DWORD dwError = GetLastError();
if (ERROR_INSUFFICIENT_BUFFER == dwError)
{
MessageBoxW(NULL, L"Path to logfile is too long! Working without logging", szTitle, MB_OK | MB_ICONWARNING);
return;
}
if (NULL == dwPathLength)
{
MessageBoxW(NULL, L"Can't get module filename! Working without logging", szTitle, MB_OK | MB_ICONWARNING);
return;
}
std::filesystem::path log_path = lpszPath;
log_path.replace_extension(L".log");
std::filesystem::path bak_path = log_path;
bak_path.replace_extension(L".bak");
if (std::filesystem::exists(log_path)) std::filesystem::rename(log_path, bak_path);
#ifdef _DEBUG
log_path = L"D:\\test.log";
#endif
logfile.open(log_path, std::ios::trunc);
if (logfile.is_open())
{
logfile << "\xEF\xBB\xBF"; // (0xEF, 0xBB, 0xBF) - UTF-8 BOM
logfile.imbue(std::locale("en-US.utf8"));
logfile << GetTimeStamp() << "[ " << szTitle << " ] Start log." << std::endl;
logfile << GetTimeStamp() << "Logfile: \"" << log_path.native() << "\"" << std::endl;
}
else
{
MessageBoxW(NULL, L"Can't open logfile! Working without logging", szTitle, MB_OK | MB_ICONWARNING);
}
return;
} }
template <typename T1, typename T2, typename T3> void CloseLogFile()
void diag_log(T1 arg1, T2 arg2, T3 arg3)
{ {
logfile << GetTimeStamp() << arg1 << arg2 << arg3 << std::endl; if (logfile)
{
logfile << GetTimeStamp() << "[ " << szTitle << " ] Stop log." << std::endl;
logfile.close();
}
} }
template <typename T1, typename T2, typename T3, typename T4>
void diag_log(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
{
logfile << GetTimeStamp() << arg1 << arg2 << arg3 << arg4 << std::endl;
}
template <typename T1, typename T2, typename T3, typename T4, typename T5>
void diag_log(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
{
logfile << GetTimeStamp() << arg1 << arg2 << arg3 << arg4 << arg5 << std::endl;
}
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
void diag_log(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)
{
logfile << GetTimeStamp() << arg1 << arg2 << arg3 << arg4 << arg5 << arg6 << std::endl;
}
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
void diag_log(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7)
{
logfile << GetTimeStamp() << arg1 << arg2 << arg3 << arg4 << arg5 << arg6 << arg7 << std::endl;
}
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
void diag_log(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8)
{
logfile << GetTimeStamp() << arg1 << arg2 << arg3 << arg4 << arg5 << arg6 << arg7 << arg8 << std::endl;
}
void OpenLogFile();
void CloseLogFile();

View File

@@ -15,5 +15,9 @@
#include <CommCtrl.h> #include <CommCtrl.h>
// Project Specific Header Files // Project Specific Header Files
#include "Logger.h" #include "logger.h"
#include "VersionInfo.h" #include "VersionInfo.h"
// Extern variables
#define MAX_LOADSTRING 50
WCHAR szTitle[MAX_LOADSTRING]; // wCenterWindow's title

View File

@@ -2,6 +2,7 @@
// wCenterWindow.cpp // wCenterWindow.cpp
// //
#include "framework.h" #include "framework.h"
#include "logger.h"
#include "wCenterWindow.h" #include "wCenterWindow.h"
#define NO_DONATION #define NO_DONATION
@@ -10,12 +11,11 @@
#define KEY_V 0x56 #define KEY_V 0x56
#define BUF_LEN 1024 #define BUF_LEN 1024
#define MAX_LOADSTRING 50
#define WM_WCW 0x8F00 #define WM_WCW 0x8F00
// Global variables: // Global variables:
HINSTANCE hInst; // Instance HINSTANCE hInst; // Instance
WCHAR szTitle[MAX_LOADSTRING]; // Window's title extern WCHAR szTitle[];
WCHAR szClass[MAX_LOADSTRING]; // Window's class WCHAR szClass[MAX_LOADSTRING]; // Window's class
WCHAR szWinTitle[256]; WCHAR szWinTitle[256];
WCHAR szWinClass[256]; WCHAR szWinClass[256];
@@ -23,7 +23,7 @@ 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;
HWND hWnd = NULL, hFgWnd = NULL; //, hTaskBar = NULL, hDesktop = NULL, hProgman = NULL; HWND hWnd = NULL, hFgWnd = NULL;
BOOL bKPressed = FALSE, bMPressed = FALSE, bShowIcon = TRUE, bWorkArea = TRUE; BOOL bKPressed = FALSE, bMPressed = FALSE, bShowIcon = TRUE, bWorkArea = TRUE;
BOOL bLCTRL = FALSE, bLWIN = FALSE, bKEYV = FALSE; BOOL bLCTRL = FALSE, bLWIN = FALSE, bKEYV = FALSE;
@@ -46,20 +46,17 @@ LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK KeyboardHookProc(int, WPARAM, LPARAM); LRESULT CALLBACK KeyboardHookProc(int, WPARAM, LPARAM);
LRESULT CALLBACK MouseHookProc(int, WPARAM, LPARAM); LRESULT CALLBACK MouseHookProc(int, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
std::string ConvertWideToUtf8(const std::wstring&);
VOID MoveWindowToMonitorCenter(HWND hwnd, BOOL bWorkArea, BOOL bResize) VOID MoveWindowToMonitorCenter(HWND hwnd, BOOL bWorkArea, BOOL bResize)
{ {
diag_log("Entering MoveWindowToMonitorCenter(), handle = 0x", hwnd); LOG_TO_FILE(L"Entering the %s() function", TEXT(__FUNCTION__));
RECT fgwrc = { 0 }; RECT fgwrc = { 0 };
GetWindowRect(hwnd, &fgwrc); GetWindowRect(hwnd, &fgwrc);
LONG nWidth = fgwrc.right - fgwrc.left; LONG nWidth = fgwrc.right - fgwrc.left;
LONG nHeight = fgwrc.bottom - fgwrc.top; LONG nHeight = fgwrc.bottom - fgwrc.top;
diag_log("Moving window from x = ", fgwrc.left, ", y = ", fgwrc.top); LOG_TO_FILE(L"%s(%d): Moving the window from %d, %d", TEXT(__FUNCTION__), __LINE__, fgwrc.left, fgwrc.top);
MONITORINFO mi = { 0 }; MONITORINFO mi = { 0 };
mi.cbSize = sizeof(MONITORINFO); mi.cbSize = sizeof(MONITORINFO);
@@ -93,12 +90,10 @@ VOID MoveWindowToMonitorCenter(HWND hwnd, BOOL bWorkArea, BOOL bResize)
MoveWindow(hwnd, x, y, nWidth, nHeight, TRUE); MoveWindow(hwnd, x, y, nWidth, nHeight, TRUE);
SendMessageW(hwnd, WM_EXITSIZEMOVE, NULL, NULL); SendMessageW(hwnd, WM_EXITSIZEMOVE, NULL, NULL);
diag_log("Moving window to x = ", x, ", y = ", y); LOG_TO_FILE(L"%s(%d): Moving the window to %d, %d", TEXT(__FUNCTION__), __LINE__, x, y);
diag_log("Quiting MoveWindowToMonitorCenter()"); LOG_TO_FILE(L"Exit from the %s() function", TEXT(__FUNCTION__));
} }
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{ {
hInst = hInstance; hInst = hInstance;
@@ -113,7 +108,7 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
} }
OpenLogFile(); OpenLogFile();
diag_log("Entering WinMain()"); LOG_TO_FILE(L"Entering the %s() function", TEXT(__FUNCTION__));
WNDCLASSEX wcex = { 0 }; WNDCLASSEX wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX); wcex.cbSize = sizeof(WNDCLASSEX);
@@ -140,13 +135,10 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
int nArgs = 0; int nArgs = 0;
LPWSTR* szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs); LPWSTR* szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
std::string arg; LOG_TO_FILE(L"Arguments count: %d", nArgs - 1);
diag_log("Arguments: ", nArgs - 1);
for (int i = 1; i < nArgs; i++) for (int i = 1; i < nArgs; i++)
{ {
arg = ConvertWideToUtf8(szArglist[i]); LOG_TO_FILE(L"Argument %d: %s", i, szArglist[i]);
diag_log("Argument #", i, ": ", arg);
} }
(nArgs >= 2 && 0 == lstrcmpiW(szArglist[1], L"/hide")) ? bShowIcon = FALSE : bShowIcon = TRUE; (nArgs >= 2 && 0 == lstrcmpiW(szArglist[1], L"/hide")) ? bShowIcon = FALSE : bShowIcon = TRUE;
@@ -178,7 +170,7 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
if (hMenu) DestroyMenu(hMenu); if (hMenu) DestroyMenu(hMenu);
Shell_NotifyIconW(NIM_DELETE, &nid); Shell_NotifyIconW(NIM_DELETE, &nid);
diag_log("Quiting WinMain(), msg.wParam = ", (int)msg.wParam); LOG_TO_FILE(L"Exit from the %s() function, msg.wParam = %d", TEXT(__FUNCTION__), (int)msg.wParam);
CloseLogFile(); CloseLogFile();
HeapFree(hHeap, NULL, szBuffer); HeapFree(hHeap, NULL, szBuffer);
@@ -191,24 +183,25 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{ {
case WM_CREATE: case WM_CREATE:
{ {
diag_log("Recived WM_CREATE message"); LOG_TO_FILE(L"%s(%d): Recived WM_CREATE message", TEXT(__FUNCTION__), __LINE__);
hMenu = LoadMenuW(hInst, MAKEINTRESOURCE(IDR_MENU)); hMenu = LoadMenuW(hInst, MAKEINTRESOURCE(IDR_MENU));
if (!hMenu) if (!hMenu)
{ {
diag_log("Loading context menu failed!"); LOG_TO_FILE(L"%s(%d): Loading context menu failed!", TEXT(__FUNCTION__), __LINE__);
ShowError(IDS_ERR_MENU); ShowError(IDS_ERR_MENU);
PostMessageW(hWnd, WM_CLOSE, NULL, NULL); PostMessageW(hWnd, WM_CLOSE, NULL, NULL);
} }
diag_log("Context menu successfully loaded"); LOG_TO_FILE(L"%s(%d): Context menu successfully loaded", TEXT(__FUNCTION__), __LINE__);
hPopup = GetSubMenu(hMenu, 0); hPopup = GetSubMenu(hMenu, 0);
if (!hPopup) if (!hPopup)
{ {
diag_log("Creating popup menu failed!"); LOG_TO_FILE(L"%s(%d): Creating popup menu failed!", TEXT(__FUNCTION__), __LINE__);
ShowError(IDS_ERR_POPUP); ShowError(IDS_ERR_POPUP);
PostMessageW(hWnd, WM_CLOSE, NULL, NULL); PostMessageW(hWnd, WM_CLOSE, NULL, NULL);
} }
diag_log("Popup menu successfully created"); LOG_TO_FILE(L"%s(%d): Popup menu successfully created", TEXT(__FUNCTION__), __LINE__);
mii.cbSize = sizeof(MENUITEMINFO); mii.cbSize = sizeof(MENUITEMINFO);
mii.fMask = MIIM_STATE; mii.fMask = MIIM_STATE;
@@ -228,67 +221,69 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
hMouseHook = SetWindowsHookExW(WH_MOUSE_LL, MouseHookProc, hInst, NULL); hMouseHook = SetWindowsHookExW(WH_MOUSE_LL, MouseHookProc, hInst, NULL);
if (!hMouseHook) if (!hMouseHook)
{ {
diag_log("Creating mouse hook failed!"); LOG_TO_FILE(L"%s(%d): Mouse hook creation failed!", TEXT(__FUNCTION__), __LINE__);
ShowError(IDS_ERR_HOOK); ShowError(IDS_ERR_HOOK);
PostMessageW(hWnd, WM_CLOSE, NULL, NULL); PostMessageW(hWnd, WM_CLOSE, NULL, NULL);
} }
diag_log("Mouse hook was successfully set"); LOG_TO_FILE(L"%s(%d): The mouse hook was successfully installed", TEXT(__FUNCTION__), __LINE__);
#endif // !_DEBUG #endif // !_DEBUG
hKbdHook = SetWindowsHookExW(WH_KEYBOARD_LL, KeyboardHookProc, hInst, NULL); hKbdHook = SetWindowsHookExW(WH_KEYBOARD_LL, KeyboardHookProc, hInst, NULL);
if (!hKbdHook) if (!hKbdHook)
{ {
diag_log("Creating keyboard hook failed!"); LOG_TO_FILE(L"%s(%d): Keyboard hook creation failed!", TEXT(__FUNCTION__), __LINE__);
ShowError(IDS_ERR_HOOK); ShowError(IDS_ERR_HOOK);
PostMessageW(hWnd, WM_CLOSE, NULL, NULL); PostMessageW(hWnd, WM_CLOSE, NULL, NULL);
} }
diag_log("Keyboard hook was successfully set"); LOG_TO_FILE(L"%s(%d): The keyboard hook was successfully installed", TEXT(__FUNCTION__), __LINE__);
break; break;
} }
case WM_WCW: case WM_WCW: // Popup menu handler
{ {
if (IDI_TRAYICON == wParam && (WM_RBUTTONDOWN == lParam || WM_LBUTTONDOWN == lParam)) if (IDI_TRAYICON == wParam && (WM_RBUTTONDOWN == lParam || WM_LBUTTONDOWN == lParam))
{ {
diag_log("Entering menu handler"); LOG_TO_FILE(L"%s(%d): Entering the WM_WCW message handler", TEXT(__FUNCTION__), __LINE__);
SetForegroundWindow(hWnd); SetForegroundWindow(hWnd);
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 (ID_POPUPMENU_ICON == idMenu) if (ID_POPUPMENU_ICON == idMenu)
{ {
diag_log("Pressed 'Hide icon' menuitem"); LOG_TO_FILE(L"%s(%d): Pressed the 'Hide icon' menuitem", TEXT(__FUNCTION__), __LINE__);
bShowIcon = FALSE; bShowIcon = FALSE;
HandlingTrayIcon(); HandlingTrayIcon();
} }
if (ID_POPUPMENU_AREA == idMenu) if (ID_POPUPMENU_AREA == idMenu)
{ {
diag_log("Pressed 'Use workarea' menuitem"); LOG_TO_FILE(L"%s(%d): Pressed the 'Use workarea' menuitem", TEXT(__FUNCTION__), __LINE__);
bWorkArea = !bWorkArea; bWorkArea = !bWorkArea;
bWorkArea ? mii.fState = MFS_CHECKED : mii.fState = MFS_UNCHECKED; bWorkArea ? mii.fState = MFS_CHECKED : mii.fState = MFS_UNCHECKED;
SetMenuItemInfoW(hPopup, ID_POPUPMENU_AREA, FALSE, &mii); SetMenuItemInfoW(hPopup, ID_POPUPMENU_AREA, FALSE, &mii);
diag_log("Changed 'Use workarea' option to ", bWorkArea); LOG_TO_FILE(L"%s(%d): Changed 'Use workarea' option to %s", TEXT(__FUNCTION__), __LINE__, bWorkArea ? L"True" : L"False");
} }
if (ID_POPUPMENU_ABOUT == idMenu && !bKPressed) if (ID_POPUPMENU_ABOUT == idMenu && !bKPressed)
{ {
diag_log("Pressed 'About' menuitem"); LOG_TO_FILE(L"%s(%d): Pressed the 'About' menuitem", TEXT(__FUNCTION__), __LINE__);
bKPressed = TRUE; bKPressed = TRUE;
DialogBoxW(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, (DLGPROC)About); DialogBoxW(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, (DLGPROC)About);
bKPressed = FALSE; bKPressed = FALSE;
} }
if (ID_POPUPMENU_EXIT == idMenu) if (ID_POPUPMENU_EXIT == idMenu)
{ {
diag_log("Pressed 'Exit' menuitem"); LOG_TO_FILE(L"%s(%d): Pressed the 'Exit' menuitem", TEXT(__FUNCTION__), __LINE__);
PostMessageW(hWnd, WM_CLOSE, NULL, NULL); PostMessageW(hWnd, WM_CLOSE, NULL, NULL);
} }
diag_log("Quiting menu handler");
LOG_TO_FILE(L"%s(%d): Exit from the WM_WCW message handler", TEXT(__FUNCTION__), __LINE__);
} }
break; break;
} }
case WM_QUERYENDSESSION: case WM_QUERYENDSESSION:
{ {
diag_log("Recieved WM_QUERYENDSESSION message, lParam = ", lParam); LOG_TO_FILE(L"%s(%d): Recieved the WM_QUERYENDSESSION message, lParam = 0x%08X", TEXT(__FUNCTION__), __LINE__, (long)lParam);
CloseLogFile(); CloseLogFile();
return TRUE; return TRUE;
break; break;
@@ -296,7 +291,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
case WM_DESTROY: case WM_DESTROY:
{ {
diag_log("Recived WM_DESTROY message"); LOG_TO_FILE(L"%s(%d): Recieved the WM_DESTROY message", TEXT(__FUNCTION__), __LINE__);
PostQuitMessage(0); PostQuitMessage(0);
break; break;
} }
@@ -312,7 +307,7 @@ LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
if (WM_MBUTTONUP == wParam) bMPressed = FALSE; if (WM_MBUTTONUP == wParam) bMPressed = FALSE;
if (WM_MBUTTONDOWN == wParam && bLCTRL && bLWIN && !bMPressed) if (WM_MBUTTONDOWN == wParam && bLCTRL && bLWIN && !bMPressed)
{ {
diag_log("Pressed LCTRL + LWIN + MMB"); LOG_TO_FILE(L"%s(%d): Pressed LCTRL + LWIN + MMB", TEXT(__FUNCTION__), __LINE__);
bMPressed = TRUE; bMPressed = TRUE;
hFgWnd = GetForegroundWindow(); hFgWnd = GetForegroundWindow();
if (IsWindowApprooved(hFgWnd)) MoveWindowToMonitorCenter(hFgWnd, bWorkArea, FALSE); if (IsWindowApprooved(hFgWnd)) MoveWindowToMonitorCenter(hFgWnd, bWorkArea, FALSE);
@@ -338,7 +333,7 @@ LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
if (KEY_I == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed) // 'I' key if (KEY_I == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed) // 'I' key
{ {
diag_log("Pressed LCTRL + LWIN + I"); LOG_TO_FILE(L"%s(%d): Pressed LCTRL + LWIN + I", TEXT(__FUNCTION__), __LINE__);
bKPressed = TRUE; bKPressed = TRUE;
bShowIcon = !bShowIcon; bShowIcon = !bShowIcon;
HandlingTrayIcon(); HandlingTrayIcon();
@@ -347,7 +342,7 @@ LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
if (KEY_C == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed && !bKEYV) // 'C' key if (KEY_C == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed && !bKEYV) // 'C' key
{ {
diag_log("Pressed LCTRL + LWIN + C"); LOG_TO_FILE(L"%s(%d): Pressed LCTRL + LWIN + C", TEXT(__FUNCTION__), __LINE__);
bKPressed = TRUE; bKPressed = TRUE;
hFgWnd = GetForegroundWindow(); hFgWnd = GetForegroundWindow();
if (IsWindowApprooved(hFgWnd)) MoveWindowToMonitorCenter(hFgWnd, bWorkArea, FALSE); if (IsWindowApprooved(hFgWnd)) MoveWindowToMonitorCenter(hFgWnd, bWorkArea, FALSE);
@@ -357,12 +352,12 @@ LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
if (KEY_V == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed && !bKEYV) // 'V' key if (KEY_V == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed && !bKEYV) // 'V' key
{ {
diag_log("Pressed LCTRL + LWIN + V"); LOG_TO_FILE(L"%s(%d): Pressed LCTRL + LWIN + V", TEXT(__FUNCTION__), __LINE__);
bKPressed = TRUE; bKEYV = TRUE; bKPressed = TRUE; bKEYV = TRUE;
hFgWnd = GetForegroundWindow(); hFgWnd = GetForegroundWindow();
if (IsWindowApprooved(hFgWnd)) if (IsWindowApprooved(hFgWnd))
{ {
diag_log("Opening 'Manual editing' dialog"); LOG_TO_FILE(L"%s(%d): Opening the 'Manual editing' dialog", TEXT(__FUNCTION__), __LINE__);
DialogBoxW(hInst, MAKEINTRESOURCE(IDD_MANUAL_EDITING), hFgWnd, (DLGPROC)DlgProc); DialogBoxW(hInst, MAKEINTRESOURCE(IDD_MANUAL_EDITING), hFgWnd, (DLGPROC)DlgProc);
SetForegroundWindow(hFgWnd); SetForegroundWindow(hFgWnd);
} }
@@ -381,7 +376,8 @@ BOOL CALLBACK DlgProc(HWND hDlg, UINT dlgmsg, WPARAM wParam, LPARAM lParam)
{ {
case WM_INITDIALOG: case WM_INITDIALOG:
{ {
diag_log("Initializing 'Manual editing' dialog"); LOG_TO_FILE(L"%s(%d): Initializing the 'Manual editing' dialog", TEXT(__FUNCTION__), __LINE__);
SetWindowTextW(hDlg, szTitle); SetWindowTextW(hDlg, szTitle);
GetWindowTextW(hFgWnd, szWinTitle, _countof(szWinTitle)); GetWindowTextW(hFgWnd, szWinTitle, _countof(szWinTitle));
GetClassNameW(hFgWnd, szWinClass, _countof(szWinClass)); GetClassNameW(hFgWnd, szWinClass, _countof(szWinClass));
@@ -405,7 +401,8 @@ BOOL CALLBACK DlgProc(HWND hDlg, UINT dlgmsg, WPARAM wParam, LPARAM lParam)
{ {
case IDC_BUTTON_SET: case IDC_BUTTON_SET:
{ {
diag_log("Pressed 'Set' button"); LOG_TO_FILE(L"%s(%d): Pressed the 'Set' button", TEXT(__FUNCTION__), __LINE__);
x = GetDlgItemInt(hDlg, IDC_EDIT_X, NULL, TRUE); x = GetDlgItemInt(hDlg, IDC_EDIT_X, NULL, TRUE);
y = GetDlgItemInt(hDlg, IDC_EDIT_Y, NULL, TRUE); y = GetDlgItemInt(hDlg, IDC_EDIT_Y, NULL, TRUE);
w = GetDlgItemInt(hDlg, IDC_EDIT_WIDTH, NULL, FALSE); w = GetDlgItemInt(hDlg, IDC_EDIT_WIDTH, NULL, FALSE);
@@ -413,14 +410,16 @@ BOOL CALLBACK DlgProc(HWND hDlg, UINT dlgmsg, WPARAM wParam, LPARAM lParam)
SendMessageW(hFgWnd, WM_ENTERSIZEMOVE, NULL, NULL); SendMessageW(hFgWnd, WM_ENTERSIZEMOVE, NULL, NULL);
MoveWindow(hFgWnd, x, y, w, h, TRUE); MoveWindow(hFgWnd, x, y, w, h, TRUE);
SendMessageW(hFgWnd, WM_EXITSIZEMOVE, NULL, NULL); SendMessageW(hFgWnd, WM_EXITSIZEMOVE, NULL, NULL);
diag_log("Window with handle 0x", hFgWnd, " was moved to x = ", x, ", y = ", y);
LOG_TO_FILE(L"%s(%d): Window with handle 0x%08X was moved to %d, %d", TEXT(__FUNCTION__), __LINE__, hFgWnd, x, y);
return TRUE; return TRUE;
break; break;
} }
case IDCANCEL: case IDCANCEL:
case IDC_BUTTON_CLOSE: case IDC_BUTTON_CLOSE:
{ {
diag_log("Closing 'Manual editing' dialog"); LOG_TO_FILE(L"%s(%d): Closing the 'Manual editing' dialog", TEXT(__FUNCTION__), __LINE__);
EndDialog(hDlg, LOWORD(wParam)); EndDialog(hDlg, LOWORD(wParam));
break; break;
} }
@@ -431,53 +430,82 @@ BOOL CALLBACK DlgProc(HWND hDlg, UINT dlgmsg, WPARAM wParam, LPARAM lParam)
BOOL IsWindowApprooved(HWND hFW) BOOL IsWindowApprooved(HWND hFW)
{ {
diag_log("Entering IsWindowApprooved(), handle = 0x", hFW); LOG_TO_FILE(L"Entering the %s() function, handle = 0x%08X", TEXT(__FUNCTION__), hFW);
bool bApprooved = FALSE; bool bApprooved = FALSE;
if (hFW) if (hFW)
{ {
if (GetWindowTextW(hFW, (LPWSTR)szBuffer, BUF_LEN - sizeof(WCHAR))) diag_log("Title: '", ConvertWideToUtf8((LPWSTR)szBuffer), "'"); if (GetWindowTextW(hFW, (LPWSTR)szBuffer, BUF_LEN - sizeof(WCHAR)))
if (IsIconic(hFW)) diag_log("Window is iconic"); {
if (IsZoomed(hFW)) diag_log("Window is maximized"); LOG_TO_FILE(L"%s(%d): Window title: '%s'", TEXT(__FUNCTION__), __LINE__, (LPWSTR)szBuffer);
LONG_PTR wlp = GetWindowLongPtr(hFW, GWL_STYLE); }
if (IsIconic(hFW))
{
LOG_TO_FILE(L"%s(%d): The window is iconified", TEXT(__FUNCTION__), __LINE__);
}
if (IsZoomed(hFW))
{
LOG_TO_FILE(L"%s(%d): The window is maximized", TEXT(__FUNCTION__), __LINE__);
}
LONG_PTR wlp = GetWindowLongPtrW(hFW, GWL_STYLE);
if (wlp & WS_CAPTION) if (wlp & WS_CAPTION)
{ {
if (!IsIconic(hFW) && !IsZoomed(hFW)) if (!IsIconic(hFW) && !IsZoomed(hFW))
{ {
diag_log("Window is approved"); LOG_TO_FILE(L"%s(%d): The window is approved!", TEXT(__FUNCTION__), __LINE__);
bApprooved = TRUE; bApprooved = TRUE;
} }
else ShowError(IDS_ERR_MAXMIN); else ShowError(IDS_ERR_MAXMIN);
} }
else diag_log("The window has no caption"); else
{
LOG_TO_FILE(L"%s(%d): The window has no caption!", TEXT(__FUNCTION__), __LINE__);
}
} }
if (!bApprooved) diag_log("Window is not approved!");
diag_log("Quiting IsWindowApprooved()"); if (!bApprooved)
{
LOG_TO_FILE(L"%s(%d): The window is not approved!", TEXT(__FUNCTION__), __LINE__);
}
LOG_TO_FILE(L"Exit from the %s() function", TEXT(__FUNCTION__));
return bApprooved; return bApprooved;
} }
VOID HandlingTrayIcon() VOID HandlingTrayIcon()
{ {
diag_log("Entering HandlingTrayIcon(), bShowIcon = ", bShowIcon); LOG_TO_FILE(L"Entering the %s() function, bShowIcon = %s", TEXT(__FUNCTION__), bShowIcon ? L"True" : L"False");
if (bShowIcon) if (bShowIcon)
{ {
bool bResult1 = Shell_NotifyIconW(NIM_ADD, &nid); bool bResult1 = Shell_NotifyIconW(NIM_ADD, &nid);
diag_log("Shell_NotifyIconW(NIM_ADD): ", bResult1); LOG_TO_FILE(L"%s(%d): Shell_NotifyIconW(NIM_ADD): %s", TEXT(__FUNCTION__), __LINE__, bResult1 ? L"True" : L"False");
bool bResult2 = Shell_NotifyIconW(NIM_SETVERSION, &nid); bool bResult2 = Shell_NotifyIconW(NIM_SETVERSION, &nid);
diag_log("Shell_NotifyIconW(NIM_SETVERSION): ", bResult2); LOG_TO_FILE(L"%s(%d): Shell_NotifyIconW(NIM_SETVERSION): %s", TEXT(__FUNCTION__), __LINE__, bResult2 ? L"True" : L"False");
Shell_NotifyIconW(NIM_MODIFY, &nid);
if (!bResult1 || !bResult2) if (!bResult1 || !bResult2)
{ {
diag_log("Error creating trayicon!"); LOG_TO_FILE(L"%s(%d): Error creating trayicon!", TEXT(__FUNCTION__), __LINE__);
ShowError(IDS_ERR_ICON); ShowError(IDS_ERR_ICON);
Shell_NotifyIconW(NIM_DELETE, &nid); Shell_NotifyIconW(NIM_DELETE, &nid);
bShowIcon = FALSE; bShowIcon = FALSE;
} }
else
{
Shell_NotifyIconW(NIM_MODIFY, &nid);
}
} }
else else
{ {
Shell_NotifyIconW(NIM_DELETE, &nid); Shell_NotifyIconW(NIM_DELETE, &nid);
} }
diag_log("Quiting HandlingTrayIcon()");
LOG_TO_FILE(L"Exit from the %s() function", TEXT(__FUNCTION__));
} }
VOID ShowError(UINT uID) VOID ShowError(UINT uID)
@@ -498,7 +526,7 @@ INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{ {
case WM_INITDIALOG: case WM_INITDIALOG:
{ {
diag_log("Initializing 'About' dialog"); LOG_TO_FILE(L"%s(%d): Initializing the 'About' dialog", TEXT(__FUNCTION__), __LINE__);
WCHAR szAboutProgName[MAX_LOADSTRING]; WCHAR szAboutProgName[MAX_LOADSTRING];
WCHAR szAboutCopyright[MAX_LOADSTRING]; WCHAR szAboutCopyright[MAX_LOADSTRING];
@@ -516,6 +544,9 @@ INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
HWND hLink = GetDlgItem(hDlg, IDC_DONATIONLINK); HWND hLink = GetDlgItem(hDlg, IDC_DONATIONLINK);
if (hLink) DestroyWindow(hLink); if (hLink) DestroyWindow(hLink);
#endif // !NO_DONATION #endif // !NO_DONATION
LOG_TO_FILE(L"%s(%d): End of initializing the 'About' dialog", TEXT(__FUNCTION__), __LINE__);
return (INT_PTR)TRUE; return (INT_PTR)TRUE;
break; break;
} }
@@ -528,7 +559,9 @@ INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
PNMLINK pNMLink = (PNMLINK)pNMHdr; PNMLINK pNMLink = (PNMLINK)pNMHdr;
LITEM item = pNMLink->item; LITEM item = pNMLink->item;
ShellExecuteW(NULL, L"open", item.szUrl, NULL, NULL, SW_SHOW); ShellExecuteW(NULL, L"open", item.szUrl, NULL, NULL, SW_SHOW);
diag_log("Pressed donation link");
LOG_TO_FILE(L"%s(%d): Pressed the donation link! :-)", TEXT(__FUNCTION__), __LINE__);
return (INT_PTR)TRUE; return (INT_PTR)TRUE;
} }
break; break;
@@ -539,7 +572,9 @@ INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
if (IDOK == LOWORD(wParam) || IDCANCEL == LOWORD(wParam)) if (IDOK == LOWORD(wParam) || IDCANCEL == LOWORD(wParam))
{ {
EndDialog(hDlg, LOWORD(wParam)); EndDialog(hDlg, LOWORD(wParam));
diag_log("Closing 'About' dialog");
LOG_TO_FILE(L"%s(%d): Closing the 'About' dialog", TEXT(__FUNCTION__), __LINE__);
return (INT_PTR)TRUE; return (INT_PTR)TRUE;
} }
break; break;
@@ -547,11 +582,3 @@ INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
} }
return (INT_PTR)FALSE; return (INT_PTR)FALSE;
} }
std::string ConvertWideToUtf8(const std::wstring& wstr)
{
int count = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), wstr.length(), NULL, 0, NULL, NULL);
std::string str(count, 0);
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, &str[0], count, NULL, NULL);
return str;
}

View File

@@ -187,13 +187,12 @@
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="framework.h" /> <ClInclude Include="framework.h" />
<ClInclude Include="Logger.h" /> <ClInclude Include="logger.h" />
<ClInclude Include="Resource.h" /> <ClInclude Include="Resource.h" />
<ClInclude Include="targetver.h" /> <ClInclude Include="targetver.h" />
<ClInclude Include="wCenterWindow.h" /> <ClInclude Include="wCenterWindow.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Logger.cpp" />
<ClCompile Include="wCenterWindow.cpp" /> <ClCompile Include="wCenterWindow.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -27,7 +27,7 @@
<ClInclude Include="wCenterWindow.h"> <ClInclude Include="wCenterWindow.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Logger.h"> <ClInclude Include="logger.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>
@@ -35,9 +35,6 @@
<ClCompile Include="wCenterWindow.cpp"> <ClCompile Include="wCenterWindow.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Logger.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="wCenterWindow.rc"> <ResourceCompile Include="wCenterWindow.rc">