Changed code formatting.

This commit is contained in:
2024-02-16 01:17:39 +03:00
parent b09c969798
commit 2b55035234
5 changed files with 759 additions and 853 deletions

View File

@@ -28,17 +28,14 @@
#include <filesystem>
#include <strsafe.h>
inline wchar_t* CLogger::GetTimeStamp()
{
inline wchar_t* CLogger::GetTimeStamp() {
GetLocalTime(&lt);
StringCchPrintfW(logTimeBuffer, _countof(logTimeBuffer), L"%d-%02d-%02d %02d:%02d:%02d.%03d | ", lt.wYear, lt.wMonth, lt.wDay, lt.wHour, lt.wMinute, lt.wSecond, lt.wMilliseconds);
return logTimeBuffer;
}
void CLogger::Out(const wchar_t* fmt, ...)
{
if (fsLogFile.is_open())
{
void CLogger::Out(const wchar_t* fmt, ...) {
if (fsLogFile.is_open()) {
va_list args;
va_start(args, fmt);
EnterCriticalSection(&cs);
@@ -49,18 +46,15 @@ void CLogger::Out(const wchar_t* fmt, ...)
}
}
void CLogger::Init()
{
void CLogger::Init() {
wchar_t szPath[MAX_PATH] = { 0 };
DWORD dwPathLength = GetModuleFileNameW(NULL, szPath, MAX_PATH);
DWORD dwError = GetLastError();
if (ERROR_INSUFFICIENT_BUFFER == dwError)
{
if (ERROR_INSUFFICIENT_BUFFER == dwError) {
MessageBoxW(NULL, L"Warning!\nPath to log file is too long! Working without logging.", szAppTitle.c_str(), MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
return;
}
if (NULL == dwPathLength)
{
if (NULL == dwPathLength) {
MessageBoxW(NULL, L"Warning!\nCan't get application's filename! Working without logging.", szAppTitle.c_str(), MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
return;
}
@@ -75,31 +69,26 @@ void CLogger::Init()
log_path = L"D:\\test.log";
#endif
fsLogFile.open(log_path, std::ios::trunc);
if (fsLogFile.is_open())
{
if (fsLogFile.is_open()) {
InitializeCriticalSection(&cs);
fsLogFile << "\xEF\xBB\xBF"; // (0xEF, 0xBB, 0xBF) - UTF-8 BOM
fsLogFile.imbue(std::locale("en-US.utf8"));
fsLogFile << GetTimeStamp() << "[ " << szAppTitleVer.c_str() << " ] Start log." << std::endl;
fsLogFile << GetTimeStamp() << "Logfile: \"" << log_path.native() << "\"" << std::endl;
}
else
{
else {
MessageBoxW(NULL, L"Warning!\nCan't create log file! Working without logging.", szAppTitle.c_str(), MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
}
}
CLogger::CLogger(const wchar_t* _appTitle)
{
CLogger::CLogger(const wchar_t* _appTitle) {
szAppTitle = _appTitle;
szAppTitleVer = _appTitle;
Init();
}
CLogger::~CLogger()
{
if (fsLogFile)
{
CLogger::~CLogger() {
if (fsLogFile) {
fsLogFile << GetTimeStamp() << "Stop log." << std::endl;
fsLogFile.close();
DeleteCriticalSection(&cs);

View File

@@ -29,8 +29,7 @@
#define MAX_LOGBUFFER_LENGTH 512
class CLogger
{
class CLogger {
public:
void Out(const wchar_t*, ...);
CLogger(const wchar_t*);

View File

@@ -33,8 +33,7 @@
picojson::value json;
struct Version
{
struct Version {
UINT Major = 0;
UINT Minor = 0;
UINT Build = 0;
@@ -46,15 +45,13 @@ void FillVersionStructure(Version& ver, const std::wstring& str);
std::vector<std::wstring> Split(const std::wstring& s, wchar_t delim);
std::wstring ConvertUtf8ToWide(const std::string& str);
UINT WINAPI Updater(void*)
{
UINT WINAPI Updater(void*) {
logger.Out(L"Entering the %s() function", TEXT(__FUNCTION__));
logger.Out(L"[UPDT] Sleeping %d seconds", T0);
Sleep(T0 * 1000); // 10 seconds
if (!GetLatestRelease(GITHUB_URI))
{
if (!GetLatestRelease(GITHUB_URI)) {
logger.Out(L"[UPDT] %s(%d): Failed getting releases!", TEXT(__FUNCTION__), __LINE__);
MessageBoxW(NULL, L"Failed getting releases!", szTitle, MB_OK | MB_ICONERROR);
@@ -67,30 +64,25 @@ UINT WINAPI Updater(void*)
picojson::object obj, obj2;
picojson::object::iterator it, it2;
if (json.is<picojson::object>())
{
if (json.is<picojson::object>()) {
logger.Out(L"[UPDT] %s(%d): Parsing JSON object", TEXT(__FUNCTION__), __LINE__);
obj = json.get<picojson::object>();
it = obj.find("message"), it2;
if (it != obj.end())
{
if (it != obj.end()) {
std::string u = (*it).second.get<std::string>();
logger.Out(L"[UPDT] %s(%d): Error! The url is %s", TEXT(__FUNCTION__), __LINE__, u);
return 102;
_endthreadex(102);
}
for (it = obj.begin(); it != obj.end(); it++)
{
for (it = obj.begin(); it != obj.end(); it++) {
if ((*it).first == "tag_name") j_tag_name = ConvertUtf8ToWide((*it).second.to_str());
if ((*it).first == "html_url") j_page_url = ConvertUtf8ToWide((*it).second.to_str());
if ((*it).first == "assets" && (*it).second.is<picojson::array>())
{
if ((*it).first == "assets" && (*it).second.is<picojson::array>()) {
picojson::array a = (*it).second.get<picojson::array>();
obj2 = a[0].get<picojson::object>();
for (it2 = obj2.begin(); it2 != obj2.end(); it2++)
{
for (it2 = obj2.begin(); it2 != obj2.end(); it2++) {
if ((*it2).first == "name") j_file_name = ConvertUtf8ToWide((*it2).second.to_str());
if ((*it2).first == "browser_download_url") j_file_url = ConvertUtf8ToWide((*it2).second.to_str());
if ((*it2).first == "size") j_file_size = static_cast<int64_t>((*it2).second.get<double>());
@@ -98,8 +90,7 @@ UINT WINAPI Updater(void*)
}
}
}
else
{
else {
logger.Out(L"[UPDT] %s(%d): Error! Cannot recognize JSON object!", TEXT(__FUNCTION__), __LINE__);
return 103;
_endthreadex(103);
@@ -119,23 +110,19 @@ UINT WINAPI Updater(void*)
FillVersionStructure(verApp, TEXT(VERSION_STR));
FillVersionStructure(verGh, gh_version);
if ((verGh.Major > verApp.Major) || (verGh.Minor > verApp.Minor) || (verGh.Build > verApp.Build) || (verGh.Patch > verApp.Patch))
{
if ((verGh.Major > verApp.Major) || (verGh.Minor > verApp.Minor) || (verGh.Build > verApp.Build) || (verGh.Patch > verApp.Patch)) {
logger.Out(L"[UPDT] %s(%d): An update is available!", TEXT(__FUNCTION__), __LINE__);
if (IDYES == MessageBoxW(NULL, L"An update is available!\nDo you want to open the download page?", szTitle, MB_YESNO | MB_ICONINFORMATION))
{
if (IDYES == MessageBoxW(NULL, L"An update is available!\nDo you want to open the download page?", szTitle, MB_YESNO | MB_ICONINFORMATION)) {
logger.Out(L"[UPDT] %s(%d): Opening download page by default browser", TEXT(__FUNCTION__), __LINE__);
ShellExecuteW(NULL, L"open", j_page_url.c_str(), NULL, NULL, SW_SHOW);
}
else
{
else {
logger.Out(L"[UPDT] %s(%d): The user refused the update", TEXT(__FUNCTION__), __LINE__);
}
}
else
{
else {
logger.Out(L"[UPDT] %s(%d): No updates is available", TEXT(__FUNCTION__), __LINE__);
}
@@ -145,8 +132,7 @@ UINT WINAPI Updater(void*)
_endthreadex(0);
}
bool GetLatestRelease(const std::wstring& urn)
{
bool GetLatestRelease(const std::wstring& urn) {
std::wstring user_agent = L"User-Agent: ";
user_agent.append(szTitle);
const std::wstring url = GITHUB_URL;
@@ -156,30 +142,25 @@ bool GetLatestRelease(const std::wstring& urn)
logger.Out(L"[UPDT] %s(%d): %s", TEXT(__FUNCTION__), __LINE__, user_agent.c_str());
HINTERNET hInternet = InternetOpenW(user_agent.c_str(), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hInternet != NULL)
{
if (hInternet != NULL) {
HINTERNET hConnect = InternetConnectW(hInternet, url.c_str(), INTERNET_DEFAULT_HTTPS_PORT, L"", L"", INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0);
if (hConnect != NULL)
{
if (hConnect != NULL) {
HINTERNET hRequest = HttpOpenRequestW(hConnect, L"GET", urn.c_str(), NULL, NULL, NULL, INTERNET_FLAG_SECURE | INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_UI, 0);
if (hRequest != NULL)
{
if (hRequest != NULL) {
BOOL isSend = HttpSendRequestW(hRequest, NULL, 0, 0, 0);
if (isSend)
{
if (isSend) {
char szData[1024]{ 0 };
DWORD dwBytesRead = 0;
std::string buffer;
do
{
do {
InternetReadFile(hRequest, szData, sizeof(szData), &dwBytesRead);
buffer.append(szData, dwBytesRead);
} while (dwBytesRead != 0);
}
while (dwBytesRead != 0);
picojson::parse(json, buffer);
std::string jerr = picojson::get_last_error();
if (!jerr.empty())
{
if (!jerr.empty()) {
logger.Out(L"[UPDT] %s(%d): Error while parsing JSON object: %s", TEXT(__FUNCTION__), __LINE__, ConvertUtf8ToWide(jerr));
MessageBoxW(NULL, L"Error while parsing JSON object!", szTitle, MB_OK | MB_ICONERROR);
@@ -187,31 +168,27 @@ bool GetLatestRelease(const std::wstring& urn)
}
}
else
{
else {
err = GetLastError();
logger.Out(L"[UPDT] %s(%d): HttpSendRequestW() error: %d", TEXT(__FUNCTION__), __LINE__, err);
ret = false;
}
}
else
{
else {
err = GetLastError();
logger.Out(L"[UPDT] %s(%d): HttpOpenRequestW() error: %d", TEXT(__FUNCTION__), __LINE__, err);
ret = false;
}
InternetCloseHandle(hRequest);
}
else
{
else {
err = GetLastError();
logger.Out(L"[UPDT] %s(%d): InternetConnectW() error: %d", TEXT(__FUNCTION__), __LINE__, err);
ret = false;
}
InternetCloseHandle(hConnect);
}
else
{
else {
err = GetLastError();
logger.Out(L"[UPDT] %s(%d): InternetOpenW() error: %d", TEXT(__FUNCTION__), __LINE__, err);
ret = false;
@@ -220,8 +197,7 @@ bool GetLatestRelease(const std::wstring& urn)
return ret;
}
std::vector<std::wstring> Split(const std::wstring& s, wchar_t delim)
{
std::vector<std::wstring> Split(const std::wstring& s, wchar_t delim) {
std::vector<std::wstring> result;
std::wstringstream ss(s);
std::wstring item;
@@ -229,8 +205,7 @@ std::vector<std::wstring> Split(const std::wstring& s, wchar_t delim)
return result;
}
void FillVersionStructure(Version& ver, const std::wstring& str)
{
void FillVersionStructure(Version& ver, const std::wstring& str) {
std::vector<std::wstring> v;
v = Split(str, '.');
if (v.size() < 4) v.push_back(L"0");
@@ -240,8 +215,7 @@ void FillVersionStructure(Version& ver, const std::wstring& str)
ver.Patch = std::stoul(v[3]);
}
std::wstring ConvertUtf8ToWide(const std::string& str)
{
std::wstring ConvertUtf8ToWide(const std::string& str) {
int count = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), NULL, 0);
std::wstring wstr(count, 0);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), &wstr[0], count);

View File

@@ -69,8 +69,7 @@ LRESULT CALLBACK MouseHookProc(int, WPARAM, LPARAM);
INT_PTR CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
static VOID MoveWindowToMonitorCenter(HWND hwnd, BOOL bWorkArea, BOOL bResize)
{
static VOID MoveWindowToMonitorCenter(HWND hwnd, BOOL bWorkArea, BOOL bResize) {
logger.Out(L"Entering the %s() function", TEXT(__FUNCTION__));
RECT fgwrc = { 0 };
@@ -84,15 +83,13 @@ static VOID MoveWindowToMonitorCenter(HWND hwnd, BOOL bWorkArea, BOOL bResize)
mi.cbSize = sizeof(MONITORINFO);
GetMonitorInfoW(MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST), &mi);
RECT area = { 0 };
if (bWorkArea)
{
if (bWorkArea) {
area.bottom = mi.rcWork.bottom;
area.left = mi.rcWork.left;
area.right = mi.rcWork.right;
area.top = mi.rcWork.top;
}
else
{
else {
area.bottom = mi.rcMonitor.bottom;
area.left = mi.rcMonitor.left;
area.right = mi.rcMonitor.right;
@@ -103,13 +100,11 @@ static VOID MoveWindowToMonitorCenter(HWND hwnd, BOOL bWorkArea, BOOL bResize)
LONG ah = area.bottom - area.top;
if ((nWidth > aw) && bResize) nWidth = aw;
if ((nHeight > ah) && bResize) nHeight = ah;
if (area.left < 0)
{
if (area.left < 0) {
aw = -aw;
area.left = 0;
}
if (area.top < 0)
{
if (area.top < 0) {
ah = -ah;
area.top = 0;
}
@@ -125,15 +120,13 @@ static VOID MoveWindowToMonitorCenter(HWND hwnd, BOOL bWorkArea, BOOL bResize)
logger.Out(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) {
WCHAR szClass[MAX_LOADSTRING]{ 0 }; // Window's class
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, _countof(szTitle));
LoadStringW(hInstance, IDS_CLASSNAME, szClass, _countof(szClass));
if (FindWindowW(szClass, NULL))
{
if (FindWindowW(szClass, NULL)) {
ShowError(IDS_RUNNING, szTitle);
return -10;
}
@@ -145,10 +138,8 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
logger.Out(L"Arguments count: %d", nArgs - 1);
if (nArgs > 1)
{
for (int i = 1; i < nArgs; i++)
{
if (nArgs > 1) {
for (int i = 1; i < nArgs; i++) {
logger.Out(L"Argument %d: %s", i, szArglist[i]);
if (0 == lstrcmpiW(szArglist[i], L"/hide")) fShowIcon = FALSE;
if (0 == lstrcmpiW(szArglist[i], L"/noupdate")) fCheckUpdates = FALSE;
@@ -168,23 +159,20 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
wcex.hCursor = LoadCursorW(nullptr, IDC_ARROW);
wcex.lpszClassName = szClass;
wcex.hIconSm = hIconSmall;
if (!RegisterClassExW(&wcex))
{
if (!RegisterClassExW(&wcex)) {
ShowError(IDS_ERR_CLASS, szTitle);
return -9;
}
HWND hMainWnd = CreateWindowExW(0, szClass, szTitle, 0, 0, 0, 0, 0, NULL, NULL, hInstance, NULL);
if (!hMainWnd)
{
if (!hMainWnd) {
ShowError(IDS_ERR_WND, szTitle);
return -8;
}
#ifndef _DEBUG
HHOOK hMouseHook = SetWindowsHookExW(WH_MOUSE_LL, MouseHookProc, GetModuleHandleW(NULL), NULL);
if (!hMouseHook)
{
if (!hMouseHook) {
logger.Out(L"%s(%d): Mouse hook creation failed!", TEXT(__FUNCTION__), __LINE__);
ShowError(IDS_ERR_HOOK, szTitle);
@@ -194,8 +182,7 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
#endif // !_DEBUG
HHOOK hKbdHook = SetWindowsHookExW(WH_KEYBOARD_LL, KeyboardHookProc, GetModuleHandleW(NULL), NULL);
if (!hKbdHook)
{
if (!hKbdHook) {
logger.Out(L"%s(%d): Keyboard hook creation failed!", TEXT(__FUNCTION__), __LINE__);
ShowError(IDS_ERR_HOOK, szTitle);
@@ -204,8 +191,7 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
logger.Out(L"%s(%d): The keyboard hook was successfully installed", TEXT(__FUNCTION__), __LINE__);
HMENU hMenu = LoadMenuW(hInstance, MAKEINTRESOURCE(IDR_MENU));
if (!hMenu)
{
if (!hMenu) {
logger.Out(L"%s(%d): Loading context menu failed!", TEXT(__FUNCTION__), __LINE__);
ShowError(IDS_ERR_MENU, szTitle);
return -5;
@@ -213,8 +199,7 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
logger.Out(L"%s(%d): Context menu successfully loaded", TEXT(__FUNCTION__), __LINE__);
hPopup = GetSubMenu(hMenu, 0);
if (!hPopup)
{
if (!hPopup) {
logger.Out(L"%s(%d): Creating popup menu failed!", TEXT(__FUNCTION__), __LINE__);
ShowError(IDS_ERR_POPUP, szTitle);
return -4;
@@ -231,15 +216,13 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
HANDLE hHeap = GetProcessHeap();
szWinTitleBuffer = HeapAlloc(hHeap, HEAP_ZERO_MEMORY, MAX_WINTITLE_BUFFER_LENGTH);
if (nullptr == szWinTitleBuffer)
{
if (nullptr == szWinTitleBuffer) {
ShowError(IDS_ERR_HEAP, szTitle);
return -3;
}
szWinClassBuffer = HeapAlloc(hHeap, HEAP_ZERO_MEMORY, MAX_WINTITLE_BUFFER_LENGTH);
if (nullptr == szWinClassBuffer)
{
if (nullptr == szWinClassBuffer) {
ShowError(IDS_ERR_HEAP, szTitle);
return -2;
}
@@ -247,15 +230,12 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
MSG msg;
BOOL bRet;
while ((bRet = GetMessageW(&msg, NULL, 0, 0)) != 0)
{
if (bRet == -1)
{
while ((bRet = GetMessageW(&msg, NULL, 0, 0)) != 0) {
if (bRet == -1) {
ShowError(IDS_ERR_MAIN, szTitle);
return -1;
}
else
{
else {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
@@ -278,10 +258,8 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
return static_cast<int>(msg.wParam);
}
LRESULT CALLBACK WndProc(HWND hMainWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
LRESULT CALLBACK WndProc(HWND hMainWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_CREATE:
{
logger.Out(L"%s(%d): Recived WM_CREATE message", TEXT(__FUNCTION__), __LINE__);
@@ -296,14 +274,12 @@ LRESULT CALLBACK WndProc(HWND hMainWnd, UINT message, WPARAM wParam, LPARAM lPar
StringCchCopyW(nid.szTip, _countof(nid.szTip), szTitle);
uMsgRestore = RegisterWindowMessageW(L"TaskbarCreated");
if (!uMsgRestore)
{
if (!uMsgRestore) {
logger.Out(L"%s(%d): Registering 'TaskbarCreated' message failed!", TEXT(__FUNCTION__), __LINE__);
}
logger.Out(L"%s(%d): The 'TaskbarCreated' message successfully registered", TEXT(__FUNCTION__), __LINE__);
if (fCheckUpdates)
{
if (fCheckUpdates) {
if (!SetTimer(hMainWnd, IDT_TIMER, (T1 * 1000 - T0 * 1000), NULL)) // 50 seconds
{
logger.Out(L"%s(%d): Creating timer failed!", TEXT(__FUNCTION__), __LINE__);
@@ -317,18 +293,15 @@ LRESULT CALLBACK WndProc(HWND hMainWnd, UINT message, WPARAM wParam, LPARAM lPar
case WM_TIMER:
{
if (fCheckUpdates)
{
if (fCheckUpdates) {
logger.Out(L"%s(%d): Checking for updates is enabled", TEXT(__FUNCTION__), __LINE__);
HANDLE hUpdater = reinterpret_cast<HANDLE>(_beginthreadex(NULL, 0, &Updater, NULL, 0, NULL));
if (NULL == hUpdater)
{
if (NULL == hUpdater) {
DWORD dwLastError = GetLastError();
logger.Out(L"%s(%d): Creating Updater thread failed! Error: %d", TEXT(__FUNCTION__), __LINE__, dwLastError);
}
else
{
else {
if (!SetTimer(hMainWnd, IDT_TIMER, (T2 * 1000), NULL)) // 1 day
{
logger.Out(L"%s(%d): Creating timer failed!", TEXT(__FUNCTION__), __LINE__);
@@ -339,17 +312,16 @@ LRESULT CALLBACK WndProc(HWND hMainWnd, UINT message, WPARAM wParam, LPARAM lPar
CloseHandle(hUpdater);
}
}
else
{
else {
logger.Out(L"%s(%d): Checking for updates is disabled", TEXT(__FUNCTION__), __LINE__);
}
break;
}
case WM_WCW: // Popup menu handler
{
if (WM_CONTEXTMENU == LOWORD(lParam))
// Popup menu handler
case WM_WCW:
{
if (WM_CONTEXTMENU == LOWORD(lParam)) {
logger.Out(L"%s(%d): Recived WM_CONTEXTMENU message", TEXT(__FUNCTION__), __LINE__);
POINT pt{ GET_X_LPARAM(wParam), GET_Y_LPARAM(wParam) };
@@ -361,9 +333,9 @@ LRESULT CALLBACK WndProc(HWND hMainWnd, UINT message, WPARAM wParam, LPARAM lPar
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
switch (wmId) {
case ID_POPUPMENU_ICON:
{
logger.Out(L"%s(%d): Pressed the 'Hide icon' menuitem", TEXT(__FUNCTION__), __LINE__);
@@ -384,8 +356,7 @@ LRESULT CALLBACK WndProc(HWND hMainWnd, UINT message, WPARAM wParam, LPARAM lPar
}
case ID_POPUPMENU_HELP:
{
if (!bKPressed)
{
if (!bKPressed) {
logger.Out(L"%s(%d): Pressed the 'Help' menuitem", TEXT(__FUNCTION__), __LINE__);
bKPressed = TRUE;
@@ -398,8 +369,7 @@ LRESULT CALLBACK WndProc(HWND hMainWnd, UINT message, WPARAM wParam, LPARAM lPar
}
case ID_POPUPMENU_ABOUT:
{
if (!bKPressed)
{
if (!bKPressed) {
logger.Out(L"%s(%d): Pressed the 'About' menuitem", TEXT(__FUNCTION__), __LINE__);
bKPressed = TRUE;
@@ -438,8 +408,7 @@ LRESULT CALLBACK WndProc(HWND hMainWnd, UINT message, WPARAM wParam, LPARAM lPar
default:
{
if (message == uMsgRestore)
{
if (message == uMsgRestore) {
Shell_NotifyIconW(NIM_DELETE, &nid);
Shell_NotifyIconW(NIM_ADD, &nid);
break;
@@ -450,11 +419,9 @@ LRESULT CALLBACK WndProc(HWND hMainWnd, UINT message, WPARAM wParam, LPARAM lPar
return 0;
}
LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (WM_MBUTTONUP == wParam) bMPressed = FALSE;
if (WM_MBUTTONDOWN == wParam && bLCTRL && bLWIN && !bMPressed)
{
if (WM_MBUTTONDOWN == wParam && bLCTRL && bLWIN && !bMPressed) {
logger.Out(L"%s(%d): Pressed LCTRL + LWIN + MMB", TEXT(__FUNCTION__), __LINE__);
bMPressed = TRUE;
@@ -465,23 +432,21 @@ LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM 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) {
LPKBDLLHOOKSTRUCT pkhs = { 0 };
pkhs = reinterpret_cast<LPKBDLLHOOKSTRUCT>(lParam);
if (WM_KEYUP == wParam)
{
if (WM_KEYUP == wParam) {
if (VK_LCONTROL == pkhs->vkCode) bLCTRL = FALSE;
if (VK_LWIN == pkhs->vkCode) bLWIN = FALSE;
bKPressed = FALSE;
}
if (WM_KEYDOWN == wParam)
{
if (WM_KEYDOWN == wParam) {
if (VK_LCONTROL == pkhs->vkCode) bLCTRL = TRUE;
if (VK_LWIN == pkhs->vkCode) bLWIN = TRUE;
if (KEY_I == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed) // 'I' key
// 'I' key
if (KEY_I == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed)
{
logger.Out(L"%s(%d): Pressed LCTRL + LWIN + I", TEXT(__FUNCTION__), __LINE__);
@@ -491,7 +456,8 @@ LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
return TRUE;
}
if (KEY_C == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed && !bKEYV) // 'C' key
// 'C' key
if (KEY_C == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed && !bKEYV)
{
logger.Out(L"%s(%d): Pressed LCTRL + LWIN + C", TEXT(__FUNCTION__), __LINE__);
@@ -502,14 +468,14 @@ LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
return TRUE;
}
if (KEY_V == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed && !bKEYV) // 'V' key
// 'V' key
if (KEY_V == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed && !bKEYV)
{
logger.Out(L"%s(%d): Pressed LCTRL + LWIN + V", TEXT(__FUNCTION__), __LINE__);
bKPressed = TRUE; bKEYV = TRUE;
hFgWnd = GetForegroundWindow();
if (IsWindowApprooved(hFgWnd))
{
if (IsWindowApprooved(hFgWnd)) {
logger.Out(L"%s(%d): Opening the 'Manual editing' dialog", TEXT(__FUNCTION__), __LINE__);
DialogBoxParamW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(IDD_MANUAL_EDITING), hFgWnd, static_cast<DLGPROC>(DlgProc), 0L);
@@ -523,12 +489,10 @@ LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
INT_PTR CALLBACK DlgProc(HWND hDlg, UINT dlgmsg, WPARAM wParam, LPARAM lParam)
{
INT_PTR CALLBACK DlgProc(HWND hDlg, UINT dlgmsg, WPARAM wParam, LPARAM lParam) {
RECT rcFW = { 0 };
int x, y, w, h;
switch (dlgmsg)
{
switch (dlgmsg) {
case WM_INITDIALOG:
{
logger.Out(L"%s(%d): Initializing the 'Manual editing' dialog", TEXT(__FUNCTION__), __LINE__);
@@ -552,8 +516,7 @@ INT_PTR CALLBACK DlgProc(HWND hDlg, UINT dlgmsg, WPARAM wParam, LPARAM lParam)
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
switch (LOWORD(wParam)) {
case IDC_BUTTON_SET:
{
logger.Out(L"%s(%d): Pressed the 'Set' button", TEXT(__FUNCTION__), __LINE__);
@@ -596,47 +559,38 @@ INT_PTR CALLBACK DlgProc(HWND hDlg, UINT dlgmsg, WPARAM wParam, LPARAM lParam)
return static_cast<INT_PTR>(FALSE);
}
BOOL IsWindowApprooved(HWND hFW)
{
BOOL IsWindowApprooved(HWND hFW) {
logger.Out(L"Entering the %s() function", TEXT(__FUNCTION__));
BOOL bApprooved = FALSE;
if (hFW)
{
if (GetWindowTextW(hFW, reinterpret_cast<LPWSTR>(szWinTitleBuffer), MAX_WINTITLE_BUFFER_LENGTH))
{
if (hFW) {
if (GetWindowTextW(hFW, reinterpret_cast<LPWSTR>(szWinTitleBuffer), MAX_WINTITLE_BUFFER_LENGTH)) {
logger.Out(L"%s(%d): Window handle: 0x%p. Title: '%s'", TEXT(__FUNCTION__), __LINE__, hFW, reinterpret_cast<LPWSTR>(szWinTitleBuffer));
}
if (IsIconic(hFW))
{
if (IsIconic(hFW)) {
logger.Out(L"%s(%d): The window is iconified", TEXT(__FUNCTION__), __LINE__);
}
if (IsZoomed(hFW))
{
if (IsZoomed(hFW)) {
logger.Out(L"%s(%d): The window is maximized", TEXT(__FUNCTION__), __LINE__);
}
LONG_PTR wlp = GetWindowLongPtrW(hFW, GWL_STYLE);
if (wlp & WS_CAPTION)
{
if (!IsIconic(hFW) && !IsZoomed(hFW))
{
if (wlp & WS_CAPTION) {
if (!IsIconic(hFW) && !IsZoomed(hFW)) {
logger.Out(L"%s(%d): The window is approved!", TEXT(__FUNCTION__), __LINE__);
bApprooved = TRUE;
}
else ShowError(IDS_ERR_MAXMIN, szTitle);
}
else
{
else {
logger.Out(L"%s(%d): The window has no caption!", TEXT(__FUNCTION__), __LINE__);
}
}
if (!bApprooved)
{
if (!bApprooved) {
logger.Out(L"%s(%d): The window is not approved!", TEXT(__FUNCTION__), __LINE__);
}
@@ -645,20 +599,17 @@ BOOL IsWindowApprooved(HWND hFW)
return bApprooved;
}
VOID HandlingTrayIcon()
{
VOID HandlingTrayIcon() {
logger.Out(L"Entering the %s() function, fShowIcon = %s", TEXT(__FUNCTION__), fShowIcon ? L"True" : L"False");
if (fShowIcon)
{
if (fShowIcon) {
BOOL bResult1 = Shell_NotifyIconW(NIM_ADD, &nid);
logger.Out(L"%s(%d): Shell_NotifyIconW(NIM_ADD) returned %s", TEXT(__FUNCTION__), __LINE__, bResult1 ? L"True" : L"False");
BOOL bResult2 = Shell_NotifyIconW(NIM_SETVERSION, &nid);
logger.Out(L"%s(%d): Shell_NotifyIconW(NIM_SETVERSION) returned %s", TEXT(__FUNCTION__), __LINE__, bResult2 ? L"True" : L"False");
if (!bResult1 || !bResult2)
{
if (!bResult1 || !bResult2) {
logger.Out(L"%s(%d): Error creating trayicon!", TEXT(__FUNCTION__), __LINE__);
ShowError(IDS_ERR_ICON, szTitle);
@@ -666,23 +617,20 @@ VOID HandlingTrayIcon()
fShowIcon = FALSE;
}
}
else
{
else {
Shell_NotifyIconW(NIM_DELETE, &nid);
}
logger.Out(L"Exit from the %s() function", TEXT(__FUNCTION__));
}
VOID ShowError(UINT uID, LPCWSTR szAppTitle)
{
VOID ShowError(UINT uID, LPCWSTR szAppTitle) {
WCHAR szErrorText[MAX_LOADSTRING]; // Error's text
LoadStringW(GetModuleHandleW(NULL), uID, szErrorText, _countof(szErrorText));
MessageBoxW(hFgWnd, szErrorText, szAppTitle, MB_OK | MB_ICONERROR | MB_TOPMOST);
}
VOID ShowPopupMenu(HWND hMainWnd, POINT pt)
{
VOID ShowPopupMenu(HWND hMainWnd, POINT pt) {
SetForegroundWindow(hMainWnd);
UINT uFlags = TPM_RIGHTBUTTON;
GetSystemMetrics(SM_MENUDROPALIGNMENT) != 0 ? uFlags |= TPM_RIGHTALIGN : uFlags |= TPM_LEFTALIGN;
@@ -690,15 +638,13 @@ VOID ShowPopupMenu(HWND hMainWnd, POINT pt)
PostMessageW(hMainWnd, WM_NULL, 0, 0);
}
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 = { 0 };
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_LINK_CLASS;
InitCommonControlsEx(&icex);
switch (message)
{
switch (message) {
case WM_INITDIALOG:
{
logger.Out(L"%s(%d): Initializing the 'About' dialog", TEXT(__FUNCTION__), __LINE__);
@@ -731,8 +677,7 @@ INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
case WM_NOTIFY:
{
LPNMHDR pNMHdr = (LPNMHDR)lParam;
if ((NM_CLICK == pNMHdr->code || NM_RETURN == pNMHdr->code) && IDC_DONATIONLINK == pNMHdr->idFrom)
{
if ((NM_CLICK == pNMHdr->code || NM_RETURN == pNMHdr->code) && IDC_DONATIONLINK == pNMHdr->idFrom) {
PNMLINK pNMLink = (PNMLINK)pNMHdr;
LITEM item = pNMLink->item;
ShellExecuteW(NULL, L"open", item.szUrl, NULL, NULL, SW_SHOW);
@@ -746,8 +691,7 @@ INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
case WM_COMMAND:
{
if (IDOK == LOWORD(wParam) || IDCANCEL == LOWORD(wParam))
{
if (IDOK == LOWORD(wParam) || IDCANCEL == LOWORD(wParam)) {
EndDialog(hDlg, LOWORD(wParam));
logger.Out(L"%s(%d): Closing the 'About' dialog", TEXT(__FUNCTION__), __LINE__);