Clean up code.

This commit is contained in:
2022-02-25 16:28:14 +03:00
parent 28de2585a8
commit e15f08d521
7 changed files with 193 additions and 212 deletions

View File

@@ -1,4 +1,4 @@
// wCenterWindow, v2.3.2
// wCenterWindow
// Logger.cpp
//
#include "framework.h"
@@ -9,59 +9,53 @@
std::wofstream logfile;
extern WCHAR szTitle[];
extern LPVOID szBuffer;
namespace fs = std::filesystem;
std::wstring PrintTitle()
{
wchar_t szWinTitle[2048];
StringCchPrintf(szWinTitle, 2048, L"%s", szBuffer);
return szWinTitle;
}
std::wstring GetTimeStamp()
{
SYSTEMTIME lt;
GetLocalTime(&lt);
wchar_t ts[TS_LEN];
WCHAR ts[TS_LEN];
StringCchPrintfW(ts, TS_LEN, L"%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_t lpszPath[PATH_LEN]{};
WCHAR lpszPath[PATH_LEN] = { 0 };
DWORD dwPathLength = GetModuleFileNameW(NULL, lpszPath, PATH_LEN);
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);
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);
MessageBoxW(NULL, L"Can't get module filename! Working without logging", (LPCWSTR)szTitle, MB_OK | MB_ICONWARNING);
return;
}
fs::path log_path = lpszPath;
std::filesystem::path log_path = lpszPath;
log_path.replace_extension(L".log");
fs::path bak_path = log_path;
std::filesystem::path bak_path = log_path;
bak_path.replace_extension(L".bak");
if (fs::exists(log_path)) fs::rename(log_path, bak_path);
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())
{
diag_log(L"Start logging.");
diag_log(L"Start logging");
diag_log(L"Logfile:", log_path);
diag_log(log_path, L"successfully opened.");
diag_log(L"Log-file was successfully opened");
}
else
{
MessageBoxW(NULL, L"Can't open logfile! Working without logging.", (LPCWSTR)szTitle, MB_OK | MB_ICONWARNING);
MessageBoxW(NULL, L"Can't open logfile! Working without logging", (LPCWSTR)szTitle, MB_OK | MB_ICONWARNING);
}
return;
}
@@ -70,7 +64,7 @@ void CloseLogFile()
{
if (logfile)
{
diag_log(L"End logging.");
diag_log(L"End logging");
logfile.close();
}
}

View File

@@ -1,4 +1,4 @@
// wCenterWindow, v2.3.2
// wCenterWindow
// Logger.h
//
#pragma once
@@ -6,7 +6,6 @@
extern std::wofstream logfile;
std::wstring GetTimeStamp();
std::wstring PrintTitle();
template <typename T1>
void diag_log(T1 arg1)

View File

@@ -1,9 +1,7 @@
// header.h : include file for standard system include files,
// or project specific include files
// wCenterWindow
// framework.h
//
#pragma once
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
@@ -17,10 +15,5 @@
#include <shellapi.h>
#include <CommCtrl.h>
// C RunTime Header Files
//#include <stdlib.h>
//#include <malloc.h>
//#include <memory.h>
// Project Specific Header Files
#include "Logger.h"

View File

@@ -1,8 +1,8 @@
// wCenterWindow, v2.3.2
// wCenterWindow
//
// TODO: More verbose logs - partially done.
// TODO: More verbose error messages
// TODO: Window's title in logs (Impossible?)
// TODO: Window's title in logs
#include "framework.h"
#include "wCenterWindow.h"
@@ -55,7 +55,7 @@ INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
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 = { 0 };
GetWindowRect(hwnd, &fgwrc);
@@ -221,10 +221,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
bWorkArea ? mii.fState = MFS_CHECKED : mii.fState = MFS_UNCHECKED;
SetMenuItemInfoW(hPopup, ID_POPUPMENU_AREA, FALSE, &mii);
//nid.cbSize = sizeof(NOTIFYICONDATAW);
nid.cbSize = sizeof(nid);
nid.cbSize = sizeof(NOTIFYICONDATAW);
nid.hWnd = hWnd;
//nid.uVersion = NOTIFYICON_VERSION_4;
nid.uVersion = NOTIFYICON_VERSION;
nid.uCallbackMessage = WM_WCW;
nid.hIcon = hIcon;
@@ -233,8 +231,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
nid.dwInfoFlags = NIIF_NONE;
nid.dwState = NIS_HIDDEN;
nid.dwStateMask = NIS_HIDDEN;
//StringCchCopyW(nid.szTip, _countof(nid.szTip), szTitle);
StringCchCopyW(nid.szTip, ARRAYSIZE(nid.szTip), szTitle);
StringCchCopyW(nid.szTip, _countof(nid.szTip), szTitle);
hMouseHook = SetWindowsHookExW(WH_MOUSE_LL, MouseHookProc, hInst, NULL);
if (!hMouseHook)
@@ -255,40 +252,40 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
diag_log(L"Keyboard hook was successfully set");
LoadStringW(hInst, IDS_ABOUT, szAbout, _countof(szAbout));
}
break;
}
case WM_WCW:
{
if ((lParam == WM_RBUTTONDOWN || lParam == WM_LBUTTONDOWN) && wParam == IDI_TRAYICON)
if (IDI_TRAYICON == wParam && (WM_RBUTTONDOWN == lParam || WM_LBUTTONDOWN == lParam))
{
SetForegroundWindow(hWnd);
POINT pt;
GetCursorPos(&pt);
int idMenu = TrackPopupMenu(hPopup, TPM_RETURNCMD, pt.x, pt.y, 0, hWnd, NULL);
if (idMenu == ID_POPUPMENU_ICON)
if (ID_POPUPMENU_ICON == idMenu)
{
bShowIcon = FALSE;
HandlingTrayIcon();
}
if (idMenu == ID_POPUPMENU_AREA)
if (ID_POPUPMENU_AREA == idMenu)
{
bWorkArea = !bWorkArea;
bWorkArea ? mii.fState = MFS_CHECKED : mii.fState = MFS_UNCHECKED;
SetMenuItemInfoW(hPopup, ID_POPUPMENU_AREA, FALSE, &mii);
diag_log(L"Changed 'Use workarea' option to", bWorkArea);
}
if (idMenu == ID_POPUPMENU_ABOUT && !bKPressed)
if (ID_POPUPMENU_ABOUT == idMenu && !bKPressed)
{
bKPressed = TRUE;
diag_log(L"Opening 'About' dialog");
DialogBoxW(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, (DLGPROC)About);
bKPressed = FALSE;
}
if (idMenu == ID_POPUPMENU_EXIT) SendMessageW(hWnd, WM_CLOSE, NULL, NULL);
}
if (ID_POPUPMENU_EXIT == idMenu) SendMessageW(hWnd, WM_CLOSE, NULL, NULL);
}
break;
}
case WM_QUERYENDSESSION:
{
@@ -301,8 +298,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
case WM_DESTROY:
{
PostQuitMessage(0);
}
break;
}
default:
return DefWindowProcW(hWnd, message, wParam, lParam);
@@ -312,8 +309,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (wParam == WM_MBUTTONUP) bMPressed = FALSE;
if (wParam == WM_MBUTTONDOWN && bLCTRL && bLWIN && !bMPressed)
if (WM_MBUTTONUP == wParam) bMPressed = FALSE;
if (WM_MBUTTONDOWN == wParam && bLCTRL && bLWIN && !bMPressed)
{
diag_log(L"Pressed LCTRL + LWIN + MMB");
bMPressed = TRUE;
@@ -329,19 +326,19 @@ LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
pkhs = (KBDLLHOOKSTRUCT*)lParam;
if (wParam == WM_KEYUP)
if (WM_KEYUP == wParam)
{
if (pkhs->vkCode == VK_LCONTROL) bLCTRL = FALSE;
if (pkhs->vkCode == VK_LWIN) bLWIN = FALSE;
if (VK_LCONTROL == pkhs->vkCode) bLCTRL = FALSE;
if (VK_LWIN == pkhs->vkCode) bLWIN = FALSE;
bKPressed = FALSE;
}
if (wParam == WM_KEYDOWN)
if (WM_KEYDOWN == wParam)
{
if (pkhs->vkCode == VK_LCONTROL) bLCTRL = TRUE;
if (pkhs->vkCode == VK_LWIN) bLWIN = TRUE;
if (VK_LCONTROL == pkhs->vkCode) bLCTRL = TRUE;
if (VK_LWIN == pkhs->vkCode) bLWIN = TRUE;
if (bLCTRL && bLWIN && pkhs->vkCode == KEY_I && !bKPressed) // 'I' key
if (KEY_I == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed) // 'I' key
{
diag_log(L"Pressed LCTRL + LWIN + I");
bKPressed = TRUE;
@@ -350,7 +347,7 @@ LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
return TRUE;
}
if (bLCTRL && bLWIN && pkhs->vkCode == KEY_C && !bKPressed && !bKEYV) // 'C' key
if (KEY_C == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed && !bKEYV) // 'C' key
{
diag_log(L"Pressed LCTRL + LWIN + C");
bKPressed = TRUE;
@@ -361,7 +358,7 @@ LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
return TRUE;
}
if (bLCTRL && bLWIN && pkhs->vkCode == KEY_V && !bKPressed && !bKEYV) // 'V' key
if (KEY_V == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed && !bKEYV) // 'V' key
{
diag_log(L"Pressed LCTRL + LWIN + V");
bKPressed = TRUE; bKEYV = TRUE;
@@ -405,6 +402,7 @@ BOOL CALLBACK DlgProc(HWND hDlg, UINT dlgmsg, WPARAM wParam, LPARAM lParam)
UpdateWindow(hDlg);
break;
}
case WM_COMMAND:
switch (LOWORD(wParam))
{
@@ -484,7 +482,7 @@ VOID ShowError(UINT uID)
{
WCHAR szErrorText[MAX_LOADSTRING]; // Error's text
LoadStringW(hInst, uID, szErrorText, _countof(szErrorText));
MessageBoxW(hWnd, szErrorText, szTitle, MB_OK | MB_ICONERROR | MB_TOPMOST);
MessageBoxW(NULL, szErrorText, szTitle, MB_OK | MB_ICONERROR | MB_TOPMOST);
}
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
@@ -506,11 +504,7 @@ INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
case WM_NOTIFY:
{
LPNMHDR pNMHdr = (LPNMHDR)lParam;
switch (pNMHdr->code)
{
case NM_CLICK:
case NM_RETURN:
if (pNMHdr->idFrom == IDC_DONATIONLINK)
if ((NM_CLICK == pNMHdr->code || NM_RETURN == pNMHdr->code) && IDC_DONATIONLINK == pNMHdr->idFrom)
{
PNMLINK pNMLink = (PNMLINK)pNMHdr;
LITEM item = pNMLink->item;
@@ -520,15 +514,15 @@ INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
}
break;
}
break;
}
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
if (IDOK == LOWORD(wParam) || IDCANCEL == LOWORD(wParam))
{
EndDialog(hDlg, LOWORD(wParam));
diag_log(L"Closing 'About' dialog");
return (INT_PTR)TRUE;
}
break;
}
}

View File

@@ -1,3 +1,4 @@
// wCenterWindow
// wCenterWindow.h
#pragma once
#include "resource.h"

View File

@@ -32,7 +32,7 @@
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
<!--ID Windows 8.1 -->
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
<!--ID Windows 10 -->
<!--ID Windows 10/11 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
</application>
</compatibility>

Binary file not shown.