diff --git a/wCenterWindow/CLogger.cpp b/wCenterWindow/CLogger.cpp index fe79fa3..9545f66 100644 --- a/wCenterWindow/CLogger.cpp +++ b/wCenterWindow/CLogger.cpp @@ -28,80 +28,69 @@ #include #include -inline wchar_t* CLogger::GetTimeStamp() -{ - GetLocalTime(<); - 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; +inline wchar_t* CLogger::GetTimeStamp() { + GetLocalTime(<); + 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()) - { - va_list args; - va_start(args, fmt); - EnterCriticalSection(&cs); - StringCchVPrintfW(logBuffer, _countof(logBuffer), fmt, args); - va_end(args); - fsLogFile << GetTimeStamp() << logBuffer << std::endl; - LeaveCriticalSection(&cs); - } +void CLogger::Out(const wchar_t* fmt, ...) { + if (fsLogFile.is_open()) { + va_list args; + va_start(args, fmt); + EnterCriticalSection(&cs); + StringCchVPrintfW(logBuffer, _countof(logBuffer), fmt, args); + va_end(args); + fsLogFile << GetTimeStamp() << logBuffer << std::endl; + LeaveCriticalSection(&cs); + } } -void CLogger::Init() -{ - wchar_t szPath[MAX_PATH] = { 0 }; - DWORD dwPathLength = GetModuleFileNameW(NULL, szPath, MAX_PATH); - DWORD dwError = GetLastError(); - 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) - { - MessageBoxW(NULL, L"Warning!\nCan't get application's filename! Working without logging.", szAppTitle.c_str(), MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL); - return; - } +void CLogger::Init() { + wchar_t szPath[MAX_PATH] = { 0 }; + DWORD dwPathLength = GetModuleFileNameW(NULL, szPath, MAX_PATH); + DWORD dwError = GetLastError(); + 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) { + MessageBoxW(NULL, L"Warning!\nCan't get application's filename! Working without logging.", szAppTitle.c_str(), MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL); + return; + } - std::filesystem::path log_path = szPath; - log_path.replace_extension(L".log"); - std::filesystem::path bak_path = log_path; - bak_path.replace_extension(L".bak"); + std::filesystem::path log_path = szPath; + 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); + if (std::filesystem::exists(log_path)) std::filesystem::rename(log_path, bak_path); #ifdef _DEBUG - log_path = L"D:\\test.log"; + log_path = L"D:\\test.log"; #endif - fsLogFile.open(log_path, std::ios::trunc); - 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 - { - MessageBoxW(NULL, L"Warning!\nCan't create log file! Working without logging.", szAppTitle.c_str(), MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL); - } + fsLogFile.open(log_path, std::ios::trunc); + 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 { + 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) -{ - szAppTitle = _appTitle; - szAppTitleVer = _appTitle; - Init(); +CLogger::CLogger(const wchar_t* _appTitle) { + szAppTitle = _appTitle; + szAppTitleVer = _appTitle; + Init(); } -CLogger::~CLogger() -{ - if (fsLogFile) - { - fsLogFile << GetTimeStamp() << "Stop log." << std::endl; - fsLogFile.close(); - DeleteCriticalSection(&cs); - } +CLogger::~CLogger() { + if (fsLogFile) { + fsLogFile << GetTimeStamp() << "Stop log." << std::endl; + fsLogFile.close(); + DeleteCriticalSection(&cs); + } } diff --git a/wCenterWindow/CLogger.h b/wCenterWindow/CLogger.h index 1e66c9f..cf953e3 100644 --- a/wCenterWindow/CLogger.h +++ b/wCenterWindow/CLogger.h @@ -29,24 +29,23 @@ #define MAX_LOGBUFFER_LENGTH 512 -class CLogger -{ +class CLogger { public: - void Out(const wchar_t*, ...); - CLogger(const wchar_t*); - ~CLogger(); + void Out(const wchar_t*, ...); + CLogger(const wchar_t*); + ~CLogger(); private: - SYSTEMTIME lt; - CRITICAL_SECTION cs; - wchar_t logTimeBuffer[28]{ 0 }; - wchar_t logBuffer[MAX_LOGBUFFER_LENGTH]{ 0 }; - std::wofstream fsLogFile; - std::wstring szAppTitle{ 0 }; - std::wstring szAppVersion{ 0 }; - std::wstring szAppPlatform{ 0 }; - std::wstring szAppTitleVer{ 0 }; + SYSTEMTIME lt; + CRITICAL_SECTION cs; + wchar_t logTimeBuffer[28]{ 0 }; + wchar_t logBuffer[MAX_LOGBUFFER_LENGTH]{ 0 }; + std::wofstream fsLogFile; + std::wstring szAppTitle{ 0 }; + std::wstring szAppVersion{ 0 }; + std::wstring szAppPlatform{ 0 }; + std::wstring szAppTitleVer{ 0 }; - inline wchar_t* GetTimeStamp(); - void Init(); + inline wchar_t* GetTimeStamp(); + void Init(); }; diff --git a/wCenterWindow/framework.h b/wCenterWindow/framework.h index 2a3f1cc..ae5d20b 100644 --- a/wCenterWindow/framework.h +++ b/wCenterWindow/framework.h @@ -25,4 +25,4 @@ #pragma once #include "targetver.h" -#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers +#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers diff --git a/wCenterWindow/updater.cpp b/wCenterWindow/updater.cpp index ce87212..4734a36 100644 --- a/wCenterWindow/updater.cpp +++ b/wCenterWindow/updater.cpp @@ -33,12 +33,11 @@ picojson::value json; -struct Version -{ - UINT Major = 0; - UINT Minor = 0; - UINT Build = 0; - UINT Patch = 0; +struct Version { + UINT Major = 0; + UINT Minor = 0; + UINT Build = 0; + UINT Patch = 0; } verApp, verGh; bool GetLatestRelease(const std::wstring& urn); @@ -46,204 +45,179 @@ void FillVersionStructure(Version& ver, const std::wstring& str); std::vector Split(const std::wstring& s, wchar_t delim); std::wstring ConvertUtf8ToWide(const std::string& str); -UINT WINAPI Updater(void*) -{ - logger.Out(L"Entering the %s() function", TEXT(__FUNCTION__)); - logger.Out(L"[UPDT] Sleeping %d seconds", T0); +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 + Sleep(T0 * 1000); // 10 seconds - if (!GetLatestRelease(GITHUB_URI)) - { - logger.Out(L"[UPDT] %s(%d): Failed getting releases!", TEXT(__FUNCTION__), __LINE__); + 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); - return 101; - _endthreadex(101); - } + MessageBoxW(NULL, L"Failed getting releases!", szTitle, MB_OK | MB_ICONERROR); + return 101; + _endthreadex(101); + } - std::wstring j_tag_name, j_file_name, j_file_ext, j_file_url, j_page_url; - int64_t j_file_size = 0; - picojson::object obj, obj2; - picojson::object::iterator it, it2; + std::wstring j_tag_name, j_file_name, j_file_ext, j_file_url, j_page_url; + int64_t j_file_size = 0; + picojson::object obj, obj2; + picojson::object::iterator it, it2; - if (json.is()) - { - logger.Out(L"[UPDT] %s(%d): Parsing JSON object", TEXT(__FUNCTION__), __LINE__); + if (json.is()) { + logger.Out(L"[UPDT] %s(%d): Parsing JSON object", TEXT(__FUNCTION__), __LINE__); - obj = json.get(); - it = obj.find("message"), it2; - if (it != obj.end()) - { - std::string u = (*it).second.get(); - logger.Out(L"[UPDT] %s(%d): Error! The url is %s", TEXT(__FUNCTION__), __LINE__, u); - return 102; - _endthreadex(102); - } + obj = json.get(); + it = obj.find("message"), it2; + if (it != obj.end()) { + std::string u = (*it).second.get(); + 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++) - { - 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 a = (*it).second.get(); - obj2 = a[0].get(); - 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((*it2).second.get()); - } - } - } - } - else - { - logger.Out(L"[UPDT] %s(%d): Error! Cannot recognize JSON object!", TEXT(__FUNCTION__), __LINE__); - return 103; - _endthreadex(103); - } + 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 a = (*it).second.get(); + obj2 = a[0].get(); + 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((*it2).second.get()); + } + } + } + } + else { + logger.Out(L"[UPDT] %s(%d): Error! Cannot recognize JSON object!", TEXT(__FUNCTION__), __LINE__); + return 103; + _endthreadex(103); + } - size_t pos = 0; - while (std::iswdigit(j_tag_name.at(pos)) == 0) pos++; - std::wstring gh_version = j_tag_name.substr(pos); + size_t pos = 0; + while (std::iswdigit(j_tag_name.at(pos)) == 0) pos++; + std::wstring gh_version = j_tag_name.substr(pos); - logger.Out(L"[UPDT] %s(%d): AppVersion : %s", TEXT(__FUNCTION__), __LINE__, TEXT(VERSION_STR)); - logger.Out(L"[UPDT] %s(%d): GitVersion : %s", TEXT(__FUNCTION__), __LINE__, gh_version.c_str()); - //logger.Out(L"[UPDT] %s(%d): FileName : %s", TEXT(__FUNCTION__), __LINE__, j_file_name.c_str()); - //logger.Out(L"[UPDT] %s(%d): FileSize : %d", TEXT(__FUNCTION__), __LINE__, j_file_size); - //logger.Out(L"[UPDT] %s(%d): File Url : %s", TEXT(__FUNCTION__), __LINE__, j_file_url.c_str()); - //logger.Out(L"[UPDT] %s(%d): Page Url : %s", TEXT(__FUNCTION__), __LINE__, j_page_url.c_str()); + logger.Out(L"[UPDT] %s(%d): AppVersion : %s", TEXT(__FUNCTION__), __LINE__, TEXT(VERSION_STR)); + logger.Out(L"[UPDT] %s(%d): GitVersion : %s", TEXT(__FUNCTION__), __LINE__, gh_version.c_str()); + //logger.Out(L"[UPDT] %s(%d): FileName : %s", TEXT(__FUNCTION__), __LINE__, j_file_name.c_str()); + //logger.Out(L"[UPDT] %s(%d): FileSize : %d", TEXT(__FUNCTION__), __LINE__, j_file_size); + //logger.Out(L"[UPDT] %s(%d): File Url : %s", TEXT(__FUNCTION__), __LINE__, j_file_url.c_str()); + //logger.Out(L"[UPDT] %s(%d): Page Url : %s", TEXT(__FUNCTION__), __LINE__, j_page_url.c_str()); - FillVersionStructure(verApp, TEXT(VERSION_STR)); - FillVersionStructure(verGh, gh_version); + 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__); + 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)) - { - 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 - { - logger.Out(L"[UPDT] %s(%d): The user refused the update", TEXT(__FUNCTION__), __LINE__); - } - } - else - { - logger.Out(L"[UPDT] %s(%d): No updates 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)) { + 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 { + logger.Out(L"[UPDT] %s(%d): The user refused the update", TEXT(__FUNCTION__), __LINE__); + } + } + else { + logger.Out(L"[UPDT] %s(%d): No updates is available", TEXT(__FUNCTION__), __LINE__); + } - logger.Out(L"[UPDT] Exit from the %s() function", TEXT(__FUNCTION__)); + logger.Out(L"[UPDT] Exit from the %s() function", TEXT(__FUNCTION__)); - return 0; - _endthreadex(0); + return 0; + _endthreadex(0); } -bool GetLatestRelease(const std::wstring& urn) -{ - std::wstring user_agent = L"User-Agent: "; - user_agent.append(szTitle); - const std::wstring url = GITHUB_URL; - bool ret = true; - DWORD err = 0; +bool GetLatestRelease(const std::wstring& urn) { + std::wstring user_agent = L"User-Agent: "; + user_agent.append(szTitle); + const std::wstring url = GITHUB_URL; + bool ret = true; + DWORD err = 0; - logger.Out(L"[UPDT] %s(%d): %s", TEXT(__FUNCTION__), __LINE__, user_agent.c_str()); + 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) - { - HINTERNET hConnect = InternetConnectW(hInternet, url.c_str(), INTERNET_DEFAULT_HTTPS_PORT, L"", L"", INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0); - 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) - { - BOOL isSend = HttpSendRequestW(hRequest, NULL, 0, 0, 0); - if (isSend) - { - char szData[1024]{ 0 }; - DWORD dwBytesRead = 0; - std::string buffer; - do - { - InternetReadFile(hRequest, szData, sizeof(szData), &dwBytesRead); - buffer.append(szData, dwBytesRead); - } while (dwBytesRead != 0); + HINTERNET hInternet = InternetOpenW(user_agent.c_str(), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); + 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) { + 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) { + BOOL isSend = HttpSendRequestW(hRequest, NULL, 0, 0, 0); + if (isSend) { + char szData[1024]{ 0 }; + DWORD dwBytesRead = 0; + std::string buffer; + do { + InternetReadFile(hRequest, szData, sizeof(szData), &dwBytesRead); + buffer.append(szData, dwBytesRead); + } + while (dwBytesRead != 0); - picojson::parse(json, buffer); - std::string jerr = picojson::get_last_error(); - if (!jerr.empty()) - { - logger.Out(L"[UPDT] %s(%d): Error while parsing JSON object: %s", TEXT(__FUNCTION__), __LINE__, ConvertUtf8ToWide(jerr)); + picojson::parse(json, buffer); + std::string jerr = picojson::get_last_error(); + 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); - ret = false; - } + MessageBoxW(NULL, L"Error while parsing JSON object!", szTitle, MB_OK | MB_ICONERROR); + ret = false; + } - } - else - { - err = GetLastError(); - logger.Out(L"[UPDT] %s(%d): HttpSendRequestW() error: %d", TEXT(__FUNCTION__), __LINE__, err); - ret = false; - } - } - else - { - err = GetLastError(); - logger.Out(L"[UPDT] %s(%d): HttpOpenRequestW() error: %d", TEXT(__FUNCTION__), __LINE__, err); - ret = false; - } - InternetCloseHandle(hRequest); - } - else - { - err = GetLastError(); - logger.Out(L"[UPDT] %s(%d): InternetConnectW() error: %d", TEXT(__FUNCTION__), __LINE__, err); - ret = false; - } - InternetCloseHandle(hConnect); - } - else - { - err = GetLastError(); - logger.Out(L"[UPDT] %s(%d): InternetOpenW() error: %d", TEXT(__FUNCTION__), __LINE__, err); - ret = false; - } - InternetCloseHandle(hInternet); - return ret; + } + else { + err = GetLastError(); + logger.Out(L"[UPDT] %s(%d): HttpSendRequestW() error: %d", TEXT(__FUNCTION__), __LINE__, err); + ret = false; + } + } + else { + err = GetLastError(); + logger.Out(L"[UPDT] %s(%d): HttpOpenRequestW() error: %d", TEXT(__FUNCTION__), __LINE__, err); + ret = false; + } + InternetCloseHandle(hRequest); + } + else { + err = GetLastError(); + logger.Out(L"[UPDT] %s(%d): InternetConnectW() error: %d", TEXT(__FUNCTION__), __LINE__, err); + ret = false; + } + InternetCloseHandle(hConnect); + } + else { + err = GetLastError(); + logger.Out(L"[UPDT] %s(%d): InternetOpenW() error: %d", TEXT(__FUNCTION__), __LINE__, err); + ret = false; + } + InternetCloseHandle(hInternet); + return ret; } -std::vector Split(const std::wstring& s, wchar_t delim) -{ - std::vector result; - std::wstringstream ss(s); - std::wstring item; - while (getline(ss, item, delim)) result.push_back(item); - return result; +std::vector Split(const std::wstring& s, wchar_t delim) { + std::vector result; + std::wstringstream ss(s); + std::wstring item; + while (getline(ss, item, delim)) result.push_back(item); + return result; } -void FillVersionStructure(Version& ver, const std::wstring& str) -{ - std::vector v; - v = Split(str, '.'); - if (v.size() < 4) v.push_back(L"0"); - ver.Major = std::stoul(v[0]); - ver.Minor = std::stoul(v[1]); - ver.Build = std::stoul(v[2]); - ver.Patch = std::stoul(v[3]); +void FillVersionStructure(Version& ver, const std::wstring& str) { + std::vector v; + v = Split(str, '.'); + if (v.size() < 4) v.push_back(L"0"); + ver.Major = std::stoul(v[0]); + ver.Minor = std::stoul(v[1]); + ver.Build = std::stoul(v[2]); + ver.Patch = std::stoul(v[3]); } -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); - return wstr; +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); + return wstr; } diff --git a/wCenterWindow/wCenterWindow.cpp b/wCenterWindow/wCenterWindow.cpp index 38914e6..3ec3e57 100644 --- a/wCenterWindow/wCenterWindow.cpp +++ b/wCenterWindow/wCenterWindow.cpp @@ -43,719 +43,663 @@ #define WM_WCW (WM_APP + 0x0F00) // Global variables: -WCHAR szTitle[MAX_LOADSTRING]{ 0 }; // wCenterWindow's title -HICON hIconSmall = NULL, hIconLarge = NULL; -HMENU hPopup = NULL; -HWND hFgWnd = NULL; -BOOL bKPressed = FALSE, bMPressed = FALSE, fShowIcon = TRUE, fCheckUpdates = TRUE, bWorkArea = TRUE; -BOOL bLCTRL = FALSE, bLWIN = FALSE, bKEYV = FALSE; -UINT uMsgRestore = 0; -CLogger logger(TEXT(PRODUCT_NAME_FULL)); +WCHAR szTitle[MAX_LOADSTRING]{ 0 }; // wCenterWindow's title +HICON hIconSmall = NULL, hIconLarge = NULL; +HMENU hPopup = NULL; +HWND hFgWnd = NULL; +BOOL bKPressed = FALSE, bMPressed = FALSE, fShowIcon = TRUE, fCheckUpdates = TRUE, bWorkArea = TRUE; +BOOL bLCTRL = FALSE, bLWIN = FALSE, bKEYV = FALSE; +UINT uMsgRestore = 0; +CLogger logger(TEXT(PRODUCT_NAME_FULL)); -NOTIFYICONDATAW nid = { 0 }; -MENUITEMINFOW mii = { 0 }; +NOTIFYICONDATAW nid = { 0 }; +MENUITEMINFOW mii = { 0 }; -LPVOID szWinTitleBuffer = nullptr; -LPVOID szWinClassBuffer = nullptr; +LPVOID szWinTitleBuffer = nullptr; +LPVOID szWinClassBuffer = nullptr; // Forward declarations of functions included in this code module: -VOID HandlingTrayIcon(); -VOID ShowError(UINT, LPCWSTR); -VOID ShowPopupMenu(HWND, POINT); -BOOL IsWindowApprooved(HWND); -LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); -LRESULT CALLBACK KeyboardHookProc(int, WPARAM, LPARAM); -LRESULT CALLBACK MouseHookProc(int, WPARAM, LPARAM); -INT_PTR CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM); -INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); +VOID HandlingTrayIcon(); +VOID ShowError(UINT, LPCWSTR); +VOID ShowPopupMenu(HWND, POINT); +BOOL IsWindowApprooved(HWND); +LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); +LRESULT CALLBACK KeyboardHookProc(int, WPARAM, LPARAM); +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) -{ - logger.Out(L"Entering the %s() function", TEXT(__FUNCTION__)); +static VOID MoveWindowToMonitorCenter(HWND hwnd, BOOL bWorkArea, BOOL bResize) { + logger.Out(L"Entering the %s() function", TEXT(__FUNCTION__)); - RECT fgwrc = { 0 }; - GetWindowRect(hwnd, &fgwrc); - LONG nWidth = fgwrc.right - fgwrc.left; - LONG nHeight = fgwrc.bottom - fgwrc.top; + RECT fgwrc = { 0 }; + GetWindowRect(hwnd, &fgwrc); + LONG nWidth = fgwrc.right - fgwrc.left; + LONG nHeight = fgwrc.bottom - fgwrc.top; - logger.Out(L"%s(%d): Moving the window from %d, %d", TEXT(__FUNCTION__), __LINE__, fgwrc.left, fgwrc.top); + logger.Out(L"%s(%d): Moving the window from %d, %d", TEXT(__FUNCTION__), __LINE__, fgwrc.left, fgwrc.top); - MONITORINFO mi = { 0 }; - mi.cbSize = sizeof(MONITORINFO); - GetMonitorInfoW(MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST), &mi); - RECT area = { 0 }; - if (bWorkArea) - { - area.bottom = mi.rcWork.bottom; - area.left = mi.rcWork.left; - area.right = mi.rcWork.right; - area.top = mi.rcWork.top; - } - else - { - area.bottom = mi.rcMonitor.bottom; - area.left = mi.rcMonitor.left; - area.right = mi.rcMonitor.right; - area.top = mi.rcMonitor.top; - } + MONITORINFO mi = { 0 }; + mi.cbSize = sizeof(MONITORINFO); + GetMonitorInfoW(MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST), &mi); + RECT area = { 0 }; + if (bWorkArea) { + area.bottom = mi.rcWork.bottom; + area.left = mi.rcWork.left; + area.right = mi.rcWork.right; + area.top = mi.rcWork.top; + } + else { + area.bottom = mi.rcMonitor.bottom; + area.left = mi.rcMonitor.left; + area.right = mi.rcMonitor.right; + area.top = mi.rcMonitor.top; + } - LONG aw = area.right - area.left; - LONG ah = area.bottom - area.top; - if ((nWidth > aw) && bResize) nWidth = aw; - if ((nHeight > ah) && bResize) nHeight = ah; - if (area.left < 0) - { - aw = -aw; - area.left = 0; - } - if (area.top < 0) - { - ah = -ah; - area.top = 0; - } - int x = area.left + (aw - nWidth) / 2; - int y = area.top + (ah - nHeight) / 2; + LONG aw = area.right - area.left; + LONG ah = area.bottom - area.top; + if ((nWidth > aw) && bResize) nWidth = aw; + if ((nHeight > ah) && bResize) nHeight = ah; + if (area.left < 0) { + aw = -aw; + area.left = 0; + } + if (area.top < 0) { + ah = -ah; + area.top = 0; + } + int x = area.left + (aw - nWidth) / 2; + int y = area.top + (ah - nHeight) / 2; - logger.Out(L"%s(%d): Moving the window to %d, %d", TEXT(__FUNCTION__), __LINE__, x, y); + logger.Out(L"%s(%d): Moving the window to %d, %d", TEXT(__FUNCTION__), __LINE__, x, y); - SendMessageW(hwnd, WM_ENTERSIZEMOVE, NULL, NULL); - MoveWindow(hwnd, x, y, nWidth, nHeight, TRUE); - SendMessageW(hwnd, WM_EXITSIZEMOVE, NULL, NULL); + SendMessageW(hwnd, WM_ENTERSIZEMOVE, NULL, NULL); + MoveWindow(hwnd, x, y, nWidth, nHeight, TRUE); + SendMessageW(hwnd, WM_EXITSIZEMOVE, NULL, NULL); - logger.Out(L"Exit from the %s() function", TEXT(__FUNCTION__)); + logger.Out(L"Exit from the %s() function", TEXT(__FUNCTION__)); } -int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) -{ - WCHAR szClass[MAX_LOADSTRING]{ 0 }; // Window's class +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)); + LoadStringW(hInstance, IDS_APP_TITLE, szTitle, _countof(szTitle)); + LoadStringW(hInstance, IDS_CLASSNAME, szClass, _countof(szClass)); - if (FindWindowW(szClass, NULL)) - { - ShowError(IDS_RUNNING, szTitle); - return -10; - } + if (FindWindowW(szClass, NULL)) { + ShowError(IDS_RUNNING, szTitle); + return -10; + } - logger.Out(L"Entering the %s() function", TEXT(__FUNCTION__)); + logger.Out(L"Entering the %s() function", TEXT(__FUNCTION__)); - int nArgs = 0; - LPWSTR* szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs); + int nArgs = 0; + LPWSTR* szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs); - logger.Out(L"Arguments count: %d", nArgs - 1); + logger.Out(L"Arguments count: %d", nArgs - 1); - 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; - } + 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; + } - } - LocalFree(szArglist); + } + LocalFree(szArglist); - LoadIconMetric(hInstance, MAKEINTRESOURCEW(IDI_TRAYICON), LIM_LARGE, &hIconLarge); - LoadIconMetric(hInstance, MAKEINTRESOURCEW(IDI_TRAYICON), LIM_SMALL, &hIconSmall); + LoadIconMetric(hInstance, MAKEINTRESOURCEW(IDI_TRAYICON), LIM_LARGE, &hIconLarge); + LoadIconMetric(hInstance, MAKEINTRESOURCEW(IDI_TRAYICON), LIM_SMALL, &hIconSmall); - WNDCLASSEXW wcex = { 0 }; - wcex.cbSize = sizeof(WNDCLASSEXW); - wcex.lpfnWndProc = WndProc; - wcex.hInstance = hInstance; - wcex.hIcon = hIconLarge; - wcex.hCursor = LoadCursorW(nullptr, IDC_ARROW); - wcex.lpszClassName = szClass; - wcex.hIconSm = hIconSmall; - if (!RegisterClassExW(&wcex)) - { - ShowError(IDS_ERR_CLASS, szTitle); - return -9; - } + WNDCLASSEXW wcex = { 0 }; + wcex.cbSize = sizeof(WNDCLASSEXW); + wcex.lpfnWndProc = WndProc; + wcex.hInstance = hInstance; + wcex.hIcon = hIconLarge; + wcex.hCursor = LoadCursorW(nullptr, IDC_ARROW); + wcex.lpszClassName = szClass; + wcex.hIconSm = hIconSmall; + 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) - { - ShowError(IDS_ERR_WND, szTitle); - return -8; - } + HWND hMainWnd = CreateWindowExW(0, szClass, szTitle, 0, 0, 0, 0, 0, NULL, NULL, hInstance, NULL); + if (!hMainWnd) { + ShowError(IDS_ERR_WND, szTitle); + return -8; + } #ifndef _DEBUG - HHOOK hMouseHook = SetWindowsHookExW(WH_MOUSE_LL, MouseHookProc, GetModuleHandleW(NULL), NULL); - if (!hMouseHook) - { - logger.Out(L"%s(%d): Mouse hook creation failed!", TEXT(__FUNCTION__), __LINE__); + HHOOK hMouseHook = SetWindowsHookExW(WH_MOUSE_LL, MouseHookProc, GetModuleHandleW(NULL), NULL); + if (!hMouseHook) { + logger.Out(L"%s(%d): Mouse hook creation failed!", TEXT(__FUNCTION__), __LINE__); - ShowError(IDS_ERR_HOOK, szTitle); - return -7; - } - logger.Out(L"%s(%d): The mouse hook was successfully installed", TEXT(__FUNCTION__), __LINE__); + ShowError(IDS_ERR_HOOK, szTitle); + return -7; + } + logger.Out(L"%s(%d): The mouse hook was successfully installed", TEXT(__FUNCTION__), __LINE__); #endif // !_DEBUG - HHOOK hKbdHook = SetWindowsHookExW(WH_KEYBOARD_LL, KeyboardHookProc, GetModuleHandleW(NULL), NULL); - if (!hKbdHook) - { - logger.Out(L"%s(%d): Keyboard hook creation failed!", TEXT(__FUNCTION__), __LINE__); + HHOOK hKbdHook = SetWindowsHookExW(WH_KEYBOARD_LL, KeyboardHookProc, GetModuleHandleW(NULL), NULL); + if (!hKbdHook) { + logger.Out(L"%s(%d): Keyboard hook creation failed!", TEXT(__FUNCTION__), __LINE__); - ShowError(IDS_ERR_HOOK, szTitle); - return -6; - } - logger.Out(L"%s(%d): The keyboard hook was successfully installed", TEXT(__FUNCTION__), __LINE__); + ShowError(IDS_ERR_HOOK, szTitle); + return -6; + } + logger.Out(L"%s(%d): The keyboard hook was successfully installed", TEXT(__FUNCTION__), __LINE__); - HMENU hMenu = LoadMenuW(hInstance, MAKEINTRESOURCE(IDR_MENU)); - if (!hMenu) - { - logger.Out(L"%s(%d): Loading context menu failed!", TEXT(__FUNCTION__), __LINE__); - ShowError(IDS_ERR_MENU, szTitle); - return -5; - } - logger.Out(L"%s(%d): Context menu successfully loaded", TEXT(__FUNCTION__), __LINE__); + HMENU hMenu = LoadMenuW(hInstance, MAKEINTRESOURCE(IDR_MENU)); + if (!hMenu) { + logger.Out(L"%s(%d): Loading context menu failed!", TEXT(__FUNCTION__), __LINE__); + ShowError(IDS_ERR_MENU, szTitle); + return -5; + } + logger.Out(L"%s(%d): Context menu successfully loaded", TEXT(__FUNCTION__), __LINE__); - hPopup = GetSubMenu(hMenu, 0); - if (!hPopup) - { - logger.Out(L"%s(%d): Creating popup menu failed!", TEXT(__FUNCTION__), __LINE__); - ShowError(IDS_ERR_POPUP, szTitle); - return -4; - } - logger.Out(L"%s(%d): Popup menu successfully created", TEXT(__FUNCTION__), __LINE__); + hPopup = GetSubMenu(hMenu, 0); + if (!hPopup) { + logger.Out(L"%s(%d): Creating popup menu failed!", TEXT(__FUNCTION__), __LINE__); + ShowError(IDS_ERR_POPUP, szTitle); + return -4; + } + logger.Out(L"%s(%d): Popup menu successfully created", TEXT(__FUNCTION__), __LINE__); - mii.cbSize = sizeof(MENUITEMINFOW); - mii.fMask = MIIM_STATE; - bWorkArea ? mii.fState = MFS_CHECKED : mii.fState = MFS_UNCHECKED; - SetMenuItemInfoW(hPopup, ID_POPUPMENU_AREA, FALSE, &mii); + mii.cbSize = sizeof(MENUITEMINFOW); + mii.fMask = MIIM_STATE; + bWorkArea ? mii.fState = MFS_CHECKED : mii.fState = MFS_UNCHECKED; + SetMenuItemInfoW(hPopup, ID_POPUPMENU_AREA, FALSE, &mii); - HandlingTrayIcon(); + HandlingTrayIcon(); - HANDLE hHeap = GetProcessHeap(); + HANDLE hHeap = GetProcessHeap(); - szWinTitleBuffer = HeapAlloc(hHeap, HEAP_ZERO_MEMORY, MAX_WINTITLE_BUFFER_LENGTH); - if (nullptr == szWinTitleBuffer) - { - ShowError(IDS_ERR_HEAP, szTitle); - return -3; - } + szWinTitleBuffer = HeapAlloc(hHeap, HEAP_ZERO_MEMORY, MAX_WINTITLE_BUFFER_LENGTH); + if (nullptr == szWinTitleBuffer) { + ShowError(IDS_ERR_HEAP, szTitle); + return -3; + } - szWinClassBuffer = HeapAlloc(hHeap, HEAP_ZERO_MEMORY, MAX_WINTITLE_BUFFER_LENGTH); - if (nullptr == szWinClassBuffer) - { - ShowError(IDS_ERR_HEAP, szTitle); - return -2; - } + szWinClassBuffer = HeapAlloc(hHeap, HEAP_ZERO_MEMORY, MAX_WINTITLE_BUFFER_LENGTH); + if (nullptr == szWinClassBuffer) { + ShowError(IDS_ERR_HEAP, szTitle); + return -2; + } - MSG msg; - BOOL bRet; + MSG msg; + BOOL bRet; - while ((bRet = GetMessageW(&msg, NULL, 0, 0)) != 0) - { - if (bRet == -1) - { - ShowError(IDS_ERR_MAIN, szTitle); - return -1; - } - else - { - TranslateMessage(&msg); - DispatchMessageW(&msg); - } - } + while ((bRet = GetMessageW(&msg, NULL, 0, 0)) != 0) { + if (bRet == -1) { + ShowError(IDS_ERR_MAIN, szTitle); + return -1; + } + else { + TranslateMessage(&msg); + DispatchMessageW(&msg); + } + } #ifndef _DEBUG - if (hMouseHook) UnhookWindowsHookEx(hMouseHook); + if (hMouseHook) UnhookWindowsHookEx(hMouseHook); #endif // !_DEBUG - if (hKbdHook) UnhookWindowsHookEx(hKbdHook); - if (hMenu) DestroyMenu(hMenu); - Shell_NotifyIconW(NIM_DELETE, &nid); - KillTimer(hMainWnd, IDT_TIMER); - DestroyIcon(hIconSmall); - DestroyIcon(hIconLarge); - HeapFree(hHeap, NULL, szWinClassBuffer); - HeapFree(hHeap, NULL, szWinTitleBuffer); + if (hKbdHook) UnhookWindowsHookEx(hKbdHook); + if (hMenu) DestroyMenu(hMenu); + Shell_NotifyIconW(NIM_DELETE, &nid); + KillTimer(hMainWnd, IDT_TIMER); + DestroyIcon(hIconSmall); + DestroyIcon(hIconLarge); + HeapFree(hHeap, NULL, szWinClassBuffer); + HeapFree(hHeap, NULL, szWinTitleBuffer); - logger.Out(L"Exit from the %s() function, msg.wParam = 0x%0*tX", TEXT(__FUNCTION__), (sizeof(int) * 2), static_cast(msg.wParam)); + logger.Out(L"Exit from the %s() function, msg.wParam = 0x%0*tX", TEXT(__FUNCTION__), (sizeof(int) * 2), static_cast(msg.wParam)); - return static_cast(msg.wParam); + return static_cast(msg.wParam); } -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__); +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__); - nid.cbSize = sizeof(NOTIFYICONDATAW); - nid.hWnd = hMainWnd; - nid.uVersion = NOTIFYICON_VERSION_4; - nid.uCallbackMessage = WM_WCW; - nid.hIcon = hIconSmall; - nid.uID = IDI_TRAYICON; - nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_SHOWTIP; - StringCchCopyW(nid.szTip, _countof(nid.szTip), szTitle); + nid.cbSize = sizeof(NOTIFYICONDATAW); + nid.hWnd = hMainWnd; + nid.uVersion = NOTIFYICON_VERSION_4; + nid.uCallbackMessage = WM_WCW; + nid.hIcon = hIconSmall; + nid.uID = IDI_TRAYICON; + nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_SHOWTIP; + StringCchCopyW(nid.szTip, _countof(nid.szTip), szTitle); - uMsgRestore = RegisterWindowMessageW(L"TaskbarCreated"); - 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__); + uMsgRestore = RegisterWindowMessageW(L"TaskbarCreated"); + 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 (!SetTimer(hMainWnd, IDT_TIMER, (T1 * 1000 - T0 * 1000), NULL)) // 50 seconds - { - logger.Out(L"%s(%d): Creating timer failed!", TEXT(__FUNCTION__), __LINE__); - ShowError(IDS_ERR_TIMER, szTitle); - fCheckUpdates = FALSE; - } - logger.Out(L"%s(%d): Timer successfully created (%d sec)", TEXT(__FUNCTION__), __LINE__, (T1 - T0)); - } - break; - } + 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__); + ShowError(IDS_ERR_TIMER, szTitle); + fCheckUpdates = FALSE; + } + logger.Out(L"%s(%d): Timer successfully created (%d sec)", TEXT(__FUNCTION__), __LINE__, (T1 - T0)); + } + break; + } - case WM_TIMER: - { - if (fCheckUpdates) - { - logger.Out(L"%s(%d): Checking for updates is enabled", TEXT(__FUNCTION__), __LINE__); + case WM_TIMER: + { + if (fCheckUpdates) { + logger.Out(L"%s(%d): Checking for updates is enabled", TEXT(__FUNCTION__), __LINE__); - HANDLE hUpdater = reinterpret_cast(_beginthreadex(NULL, 0, &Updater, NULL, 0, NULL)); - if (NULL == hUpdater) - { - DWORD dwLastError = GetLastError(); - logger.Out(L"%s(%d): Creating Updater thread failed! Error: %d", TEXT(__FUNCTION__), __LINE__, dwLastError); - } - else - { - if (!SetTimer(hMainWnd, IDT_TIMER, (T2 * 1000), NULL)) // 1 day - { - logger.Out(L"%s(%d): Creating timer failed!", TEXT(__FUNCTION__), __LINE__); - ShowError(IDS_ERR_TIMER, szTitle); - fCheckUpdates = FALSE; - } - logger.Out(L"%s(%d): Timer successfully created (%d sec)", TEXT(__FUNCTION__), __LINE__, T2); - CloseHandle(hUpdater); - } - } - else - { - logger.Out(L"%s(%d): Checking for updates is disabled", TEXT(__FUNCTION__), __LINE__); - } - break; - } + HANDLE hUpdater = reinterpret_cast(_beginthreadex(NULL, 0, &Updater, NULL, 0, NULL)); + if (NULL == hUpdater) { + DWORD dwLastError = GetLastError(); + logger.Out(L"%s(%d): Creating Updater thread failed! Error: %d", TEXT(__FUNCTION__), __LINE__, dwLastError); + } + else { + if (!SetTimer(hMainWnd, IDT_TIMER, (T2 * 1000), NULL)) // 1 day + { + logger.Out(L"%s(%d): Creating timer failed!", TEXT(__FUNCTION__), __LINE__); + ShowError(IDS_ERR_TIMER, szTitle); + fCheckUpdates = FALSE; + } + logger.Out(L"%s(%d): Timer successfully created (%d sec)", TEXT(__FUNCTION__), __LINE__, T2); + CloseHandle(hUpdater); + } + } + 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)) - { - logger.Out(L"%s(%d): Recived WM_CONTEXTMENU message", TEXT(__FUNCTION__), __LINE__); + // 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) }; - ShowPopupMenu(hMainWnd, pt); - } - break; - } + POINT pt{ GET_X_LPARAM(wParam), GET_Y_LPARAM(wParam) }; + ShowPopupMenu(hMainWnd, pt); + } + break; + } - case WM_COMMAND: - { - int wmId = LOWORD(wParam); - // Parse the menu selections: - switch (wmId) - { - case ID_POPUPMENU_ICON: - { - logger.Out(L"%s(%d): Pressed the 'Hide icon' menuitem", TEXT(__FUNCTION__), __LINE__); + case WM_COMMAND: + { + int wmId = LOWORD(wParam); - fShowIcon = FALSE; - HandlingTrayIcon(); - break; - } - case ID_POPUPMENU_AREA: - { - logger.Out(L"%s(%d): Pressed the 'Use workarea' menuitem", TEXT(__FUNCTION__), __LINE__); + // Parse the menu selections: + switch (wmId) { + case ID_POPUPMENU_ICON: + { + logger.Out(L"%s(%d): Pressed the 'Hide icon' menuitem", TEXT(__FUNCTION__), __LINE__); - bWorkArea = !bWorkArea; - bWorkArea ? mii.fState = MFS_CHECKED : mii.fState = MFS_UNCHECKED; - SetMenuItemInfoW(hPopup, ID_POPUPMENU_AREA, FALSE, &mii); - logger.Out(L"%s(%d): Changed 'Use workarea' option to %s", TEXT(__FUNCTION__), __LINE__, bWorkArea ? L"True" : L"False"); - break; - } - case ID_POPUPMENU_HELP: - { - if (!bKPressed) - { - logger.Out(L"%s(%d): Pressed the 'Help' menuitem", TEXT(__FUNCTION__), __LINE__); + fShowIcon = FALSE; + HandlingTrayIcon(); + break; + } + case ID_POPUPMENU_AREA: + { + logger.Out(L"%s(%d): Pressed the 'Use workarea' menuitem", TEXT(__FUNCTION__), __LINE__); - bKPressed = TRUE; - WCHAR szHelp[MAX_LOADSTRING * 15]; - LoadStringW(GetModuleHandleW(NULL), IDS_HELP, szHelp, _countof(szHelp)); - MessageBoxW(hMainWnd, szHelp, szTitle, MB_OK | MB_ICONINFORMATION); - bKPressed = FALSE; - } - break; - } - case ID_POPUPMENU_ABOUT: - { - if (!bKPressed) - { - logger.Out(L"%s(%d): Pressed the 'About' menuitem", TEXT(__FUNCTION__), __LINE__); + bWorkArea = !bWorkArea; + bWorkArea ? mii.fState = MFS_CHECKED : mii.fState = MFS_UNCHECKED; + SetMenuItemInfoW(hPopup, ID_POPUPMENU_AREA, FALSE, &mii); + logger.Out(L"%s(%d): Changed 'Use workarea' option to %s", TEXT(__FUNCTION__), __LINE__, bWorkArea ? L"True" : L"False"); + break; + } + case ID_POPUPMENU_HELP: + { + if (!bKPressed) { + logger.Out(L"%s(%d): Pressed the 'Help' menuitem", TEXT(__FUNCTION__), __LINE__); - bKPressed = TRUE; - DialogBoxParamW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(IDD_ABOUTBOX), hMainWnd, static_cast(About), 0L); - bKPressed = FALSE; - } - break; - } - case ID_POPUPMENU_EXIT: - { - logger.Out(L"%s(%d): Pressed the 'Exit' menuitem", TEXT(__FUNCTION__), __LINE__); + bKPressed = TRUE; + WCHAR szHelp[MAX_LOADSTRING * 15]; + LoadStringW(GetModuleHandleW(NULL), IDS_HELP, szHelp, _countof(szHelp)); + MessageBoxW(hMainWnd, szHelp, szTitle, MB_OK | MB_ICONINFORMATION); + bKPressed = FALSE; + } + break; + } + case ID_POPUPMENU_ABOUT: + { + if (!bKPressed) { + logger.Out(L"%s(%d): Pressed the 'About' menuitem", TEXT(__FUNCTION__), __LINE__); - DestroyWindow(hMainWnd); - break; - } - } - break; - } + bKPressed = TRUE; + DialogBoxParamW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(IDD_ABOUTBOX), hMainWnd, static_cast(About), 0L); + bKPressed = FALSE; + } + break; + } + case ID_POPUPMENU_EXIT: + { + logger.Out(L"%s(%d): Pressed the 'Exit' menuitem", TEXT(__FUNCTION__), __LINE__); - case WM_QUERYENDSESSION: - { - logger.Out(L"%s(%d): Recieved the WM_QUERYENDSESSION message, lParam = 0x%p", TEXT(__FUNCTION__), __LINE__, lParam); + DestroyWindow(hMainWnd); + break; + } + } + break; + } - PostQuitMessage(0); - return TRUE; - break; - } + case WM_QUERYENDSESSION: + { + logger.Out(L"%s(%d): Recieved the WM_QUERYENDSESSION message, lParam = 0x%p", TEXT(__FUNCTION__), __LINE__, lParam); - case WM_DESTROY: - { - logger.Out(L"%s(%d): Recieved the WM_DESTROY message", TEXT(__FUNCTION__), __LINE__); + PostQuitMessage(0); + return TRUE; + break; + } - PostQuitMessage(0); - break; - } + case WM_DESTROY: + { + logger.Out(L"%s(%d): Recieved the WM_DESTROY message", TEXT(__FUNCTION__), __LINE__); - default: - { - if (message == uMsgRestore) - { - Shell_NotifyIconW(NIM_DELETE, &nid); - Shell_NotifyIconW(NIM_ADD, &nid); - break; - } - return DefWindowProcW(hMainWnd, message, wParam, lParam); - } - } - return 0; + PostQuitMessage(0); + break; + } + + default: + { + if (message == uMsgRestore) { + Shell_NotifyIconW(NIM_DELETE, &nid); + Shell_NotifyIconW(NIM_ADD, &nid); + break; + } + return DefWindowProcW(hMainWnd, message, wParam, lParam); + } + } + return 0; } -LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam) -{ - if (WM_MBUTTONUP == wParam) bMPressed = FALSE; - if (WM_MBUTTONDOWN == wParam && bLCTRL && bLWIN && !bMPressed) - { - logger.Out(L"%s(%d): Pressed LCTRL + LWIN + MMB", TEXT(__FUNCTION__), __LINE__); +LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam) { + if (WM_MBUTTONUP == wParam) bMPressed = FALSE; + if (WM_MBUTTONDOWN == wParam && bLCTRL && bLWIN && !bMPressed) { + logger.Out(L"%s(%d): Pressed LCTRL + LWIN + MMB", TEXT(__FUNCTION__), __LINE__); - bMPressed = TRUE; - hFgWnd = GetForegroundWindow(); - if (IsWindowApprooved(hFgWnd)) MoveWindowToMonitorCenter(hFgWnd, bWorkArea, FALSE); - else hFgWnd = NULL; - } - return CallNextHookEx(NULL, nCode, wParam, lParam); + bMPressed = TRUE; + hFgWnd = GetForegroundWindow(); + if (IsWindowApprooved(hFgWnd)) MoveWindowToMonitorCenter(hFgWnd, bWorkArea, FALSE); + else hFgWnd = NULL; + } + return CallNextHookEx(NULL, nCode, wParam, lParam); } -LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) -{ - LPKBDLLHOOKSTRUCT pkhs = { 0 }; - pkhs = reinterpret_cast(lParam); - if (WM_KEYUP == wParam) - { - if (VK_LCONTROL == pkhs->vkCode) bLCTRL = FALSE; - if (VK_LWIN == pkhs->vkCode) bLWIN = FALSE; - bKPressed = FALSE; - } +LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) { + LPKBDLLHOOKSTRUCT pkhs = { 0 }; + pkhs = reinterpret_cast(lParam); + 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 (VK_LCONTROL == pkhs->vkCode) bLCTRL = TRUE; - if (VK_LWIN == pkhs->vkCode) bLWIN = TRUE; + 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 - { - logger.Out(L"%s(%d): Pressed LCTRL + LWIN + I", TEXT(__FUNCTION__), __LINE__); + // 'I' key + if (KEY_I == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed) + { + logger.Out(L"%s(%d): Pressed LCTRL + LWIN + I", TEXT(__FUNCTION__), __LINE__); - bKPressed = TRUE; - fShowIcon = !fShowIcon; - HandlingTrayIcon(); - return TRUE; - } + bKPressed = TRUE; + fShowIcon = !fShowIcon; + HandlingTrayIcon(); + return TRUE; + } - if (KEY_C == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed && !bKEYV) // 'C' key - { - logger.Out(L"%s(%d): Pressed LCTRL + LWIN + C", TEXT(__FUNCTION__), __LINE__); + // 'C' key + if (KEY_C == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed && !bKEYV) + { + logger.Out(L"%s(%d): Pressed LCTRL + LWIN + C", TEXT(__FUNCTION__), __LINE__); - bKPressed = TRUE; - hFgWnd = GetForegroundWindow(); - if (IsWindowApprooved(hFgWnd)) MoveWindowToMonitorCenter(hFgWnd, bWorkArea, FALSE); - else hFgWnd = NULL; - return TRUE; - } + bKPressed = TRUE; + hFgWnd = GetForegroundWindow(); + if (IsWindowApprooved(hFgWnd)) MoveWindowToMonitorCenter(hFgWnd, bWorkArea, FALSE); + else hFgWnd = NULL; + return TRUE; + } - if (KEY_V == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed && !bKEYV) // 'V' key - { - logger.Out(L"%s(%d): Pressed LCTRL + LWIN + V", TEXT(__FUNCTION__), __LINE__); + // '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)) - { - logger.Out(L"%s(%d): Opening the 'Manual editing' dialog", TEXT(__FUNCTION__), __LINE__); + bKPressed = TRUE; bKEYV = TRUE; + hFgWnd = GetForegroundWindow(); + 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), 0L); - SetForegroundWindow(hFgWnd); - } - else hFgWnd = NULL; - bKEYV = FALSE; - return TRUE; - } - } - return CallNextHookEx(NULL, nCode, wParam, lParam); + DialogBoxParamW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(IDD_MANUAL_EDITING), hFgWnd, static_cast(DlgProc), 0L); + SetForegroundWindow(hFgWnd); + } + else hFgWnd = NULL; + bKEYV = FALSE; + return TRUE; + } + } + return CallNextHookEx(NULL, nCode, wParam, lParam); } -INT_PTR CALLBACK DlgProc(HWND hDlg, UINT dlgmsg, WPARAM wParam, LPARAM lParam) -{ - RECT rcFW = { 0 }; - int x, y, w, h; - switch (dlgmsg) - { - case WM_INITDIALOG: - { - logger.Out(L"%s(%d): Initializing the 'Manual editing' dialog", TEXT(__FUNCTION__), __LINE__); +INT_PTR CALLBACK DlgProc(HWND hDlg, UINT dlgmsg, WPARAM wParam, LPARAM lParam) { + RECT rcFW = { 0 }; + int x, y, w, h; + switch (dlgmsg) { + case WM_INITDIALOG: + { + logger.Out(L"%s(%d): Initializing the 'Manual editing' dialog", TEXT(__FUNCTION__), __LINE__); - SetWindowTextW(hDlg, szTitle); - GetClassNameW(hFgWnd, static_cast(szWinClassBuffer), MAX_WINTITLE_BUFFER_LENGTH); - 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); - SetDlgItemTextW(hDlg, IDC_EDIT_TITLE, static_cast(szWinTitleBuffer)); - SetDlgItemTextW(hDlg, IDC_EDIT_CLASS, static_cast(szWinClassBuffer)); - UpdateWindow(hDlg); - break; - } + SetWindowTextW(hDlg, szTitle); + GetClassNameW(hFgWnd, static_cast(szWinClassBuffer), MAX_WINTITLE_BUFFER_LENGTH); + 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); + SetDlgItemTextW(hDlg, IDC_EDIT_TITLE, static_cast(szWinTitleBuffer)); + SetDlgItemTextW(hDlg, IDC_EDIT_CLASS, static_cast(szWinClassBuffer)); + UpdateWindow(hDlg); + break; + } - case WM_COMMAND: - { - switch (LOWORD(wParam)) - { - case IDC_BUTTON_SET: - { - logger.Out(L"%s(%d): Pressed the 'Set' button", TEXT(__FUNCTION__), __LINE__); + case WM_COMMAND: + { + switch (LOWORD(wParam)) { + case IDC_BUTTON_SET: + { + logger.Out(L"%s(%d): Pressed the 'Set' button", TEXT(__FUNCTION__), __LINE__); - 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); - SendMessageW(hFgWnd, WM_ENTERSIZEMOVE, NULL, NULL); - MoveWindow(hFgWnd, x, y, w, h, TRUE); - SendMessageW(hFgWnd, WM_EXITSIZEMOVE, NULL, NULL); + 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); + SendMessageW(hFgWnd, WM_ENTERSIZEMOVE, NULL, NULL); + MoveWindow(hFgWnd, x, y, w, h, TRUE); + SendMessageW(hFgWnd, WM_EXITSIZEMOVE, NULL, NULL); - logger.Out(L"%s(%d): Window with handle 0x%p was moved to %d, %d", TEXT(__FUNCTION__), __LINE__, hFgWnd, x, y); + logger.Out(L"%s(%d): Window with handle 0x%p was moved to %d, %d", TEXT(__FUNCTION__), __LINE__, hFgWnd, x, y); - return static_cast(TRUE); - break; - } - case IDC_BUTTON_CENTER: - { - logger.Out(L"%s(%d): Pressed the 'Center' button", TEXT(__FUNCTION__), __LINE__); + return static_cast(TRUE); + break; + } + case IDC_BUTTON_CENTER: + { + logger.Out(L"%s(%d): Pressed the 'Center' button", TEXT(__FUNCTION__), __LINE__); - bKPressed = TRUE; - if (IsWindowApprooved(hFgWnd)) MoveWindowToMonitorCenter(hFgWnd, bWorkArea, FALSE); - else hFgWnd = NULL; - return static_cast(TRUE); - break; - } - case IDCANCEL: - case IDC_BUTTON_CLOSE: - { - logger.Out(L"%s(%d): Closing the 'Manual editing' dialog", TEXT(__FUNCTION__), __LINE__); + bKPressed = TRUE; + if (IsWindowApprooved(hFgWnd)) MoveWindowToMonitorCenter(hFgWnd, bWorkArea, FALSE); + else hFgWnd = NULL; + return static_cast(TRUE); + break; + } + case IDCANCEL: + case IDC_BUTTON_CLOSE: + { + logger.Out(L"%s(%d): Closing the 'Manual editing' dialog", TEXT(__FUNCTION__), __LINE__); - EndDialog(hDlg, LOWORD(wParam)); - break; - } - } - break; - } - } - return static_cast(FALSE); + EndDialog(hDlg, LOWORD(wParam)); + break; + } + } + break; + } + } + return static_cast(FALSE); } -BOOL IsWindowApprooved(HWND hFW) -{ - logger.Out(L"Entering the %s() function", TEXT(__FUNCTION__)); +BOOL IsWindowApprooved(HWND hFW) { + logger.Out(L"Entering the %s() function", TEXT(__FUNCTION__)); - BOOL bApprooved = FALSE; - if (hFW) - { - if (GetWindowTextW(hFW, reinterpret_cast(szWinTitleBuffer), MAX_WINTITLE_BUFFER_LENGTH)) - { - logger.Out(L"%s(%d): Window handle: 0x%p. Title: '%s'", TEXT(__FUNCTION__), __LINE__, hFW, reinterpret_cast(szWinTitleBuffer)); - } + BOOL bApprooved = FALSE; + if (hFW) { + if (GetWindowTextW(hFW, reinterpret_cast(szWinTitleBuffer), MAX_WINTITLE_BUFFER_LENGTH)) { + logger.Out(L"%s(%d): Window handle: 0x%p. Title: '%s'", TEXT(__FUNCTION__), __LINE__, hFW, reinterpret_cast(szWinTitleBuffer)); + } - if (IsIconic(hFW)) - { - logger.Out(L"%s(%d): The window is iconified", TEXT(__FUNCTION__), __LINE__); - } + if (IsIconic(hFW)) { + logger.Out(L"%s(%d): The window is iconified", TEXT(__FUNCTION__), __LINE__); + } - if (IsZoomed(hFW)) - { - logger.Out(L"%s(%d): The window is maximized", TEXT(__FUNCTION__), __LINE__); - } + 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)) - { - logger.Out(L"%s(%d): The window is approved!", TEXT(__FUNCTION__), __LINE__); + LONG_PTR wlp = GetWindowLongPtrW(hFW, GWL_STYLE); + 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 - { - logger.Out(L"%s(%d): The window has no caption!", TEXT(__FUNCTION__), __LINE__); - } - } + bApprooved = TRUE; + } + else ShowError(IDS_ERR_MAXMIN, szTitle); + } + else { + logger.Out(L"%s(%d): The window has no caption!", TEXT(__FUNCTION__), __LINE__); + } + } - if (!bApprooved) - { - logger.Out(L"%s(%d): The window is not approved!", TEXT(__FUNCTION__), __LINE__); - } + if (!bApprooved) { + logger.Out(L"%s(%d): The window is not approved!", TEXT(__FUNCTION__), __LINE__); + } - logger.Out(L"Exit from the %s() function", TEXT(__FUNCTION__)); + logger.Out(L"Exit from the %s() function", TEXT(__FUNCTION__)); - return bApprooved; + return bApprooved; } -VOID HandlingTrayIcon() -{ - logger.Out(L"Entering the %s() function, fShowIcon = %s", TEXT(__FUNCTION__), fShowIcon ? L"True" : L"False"); +VOID HandlingTrayIcon() { + logger.Out(L"Entering the %s() function, fShowIcon = %s", TEXT(__FUNCTION__), fShowIcon ? L"True" : L"False"); - 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"); + 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"); + 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) - { - logger.Out(L"%s(%d): Error creating trayicon!", TEXT(__FUNCTION__), __LINE__); + if (!bResult1 || !bResult2) { + logger.Out(L"%s(%d): Error creating trayicon!", TEXT(__FUNCTION__), __LINE__); - ShowError(IDS_ERR_ICON, szTitle); - Shell_NotifyIconW(NIM_DELETE, &nid); - fShowIcon = FALSE; - } - } - else - { - Shell_NotifyIconW(NIM_DELETE, &nid); - } + ShowError(IDS_ERR_ICON, szTitle); + Shell_NotifyIconW(NIM_DELETE, &nid); + fShowIcon = FALSE; + } + } + else { + Shell_NotifyIconW(NIM_DELETE, &nid); + } - logger.Out(L"Exit from the %s() function", TEXT(__FUNCTION__)); + logger.Out(L"Exit from the %s() function", TEXT(__FUNCTION__)); } -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 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) -{ - SetForegroundWindow(hMainWnd); - UINT uFlags = TPM_RIGHTBUTTON; - GetSystemMetrics(SM_MENUDROPALIGNMENT) != 0 ? uFlags |= TPM_RIGHTALIGN : uFlags |= TPM_LEFTALIGN; - TrackPopupMenuEx(hPopup, uFlags, pt.x, pt.y, hMainWnd, NULL); - PostMessageW(hMainWnd, WM_NULL, 0, 0); +VOID ShowPopupMenu(HWND hMainWnd, POINT pt) { + SetForegroundWindow(hMainWnd); + UINT uFlags = TPM_RIGHTBUTTON; + GetSystemMetrics(SM_MENUDROPALIGNMENT) != 0 ? uFlags |= TPM_RIGHTALIGN : uFlags |= TPM_LEFTALIGN; + TrackPopupMenuEx(hPopup, uFlags, pt.x, pt.y, hMainWnd, NULL); + PostMessageW(hMainWnd, WM_NULL, 0, 0); } -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); +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) - { - case WM_INITDIALOG: - { - logger.Out(L"%s(%d): Initializing the 'About' dialog", TEXT(__FUNCTION__), __LINE__); + switch (message) { + case WM_INITDIALOG: + { + logger.Out(L"%s(%d): Initializing the 'About' dialog", TEXT(__FUNCTION__), __LINE__); - WCHAR szAboutProgName[MAX_LOADSTRING]; - WCHAR szAboutCopyright[MAX_LOADSTRING]; - WCHAR szAboutBuildTime[MAX_LOADSTRING]; - WCHAR szAboutLicense[MAX_LOADSTRING * 22]; - LoadStringW(GetModuleHandleW(NULL), IDS_LICENSE, szAboutLicense, _countof(szAboutLicense)); - MultiByteToWideChar(1251, 0, PRODUCT_NAME_FULL, _countof(PRODUCT_NAME_FULL), szAboutProgName, MAX_LOADSTRING); - MultiByteToWideChar(1251, 0, PRODUCT_COPYRIGHT, _countof(PRODUCT_COPYRIGHT), szAboutCopyright, MAX_LOADSTRING); - MultiByteToWideChar(1251, 0, ABOUT_BUILD, _countof(ABOUT_BUILD), szAboutBuildTime, MAX_LOADSTRING); - SetDlgItemTextW(hDlg, IDC_ABOUT_PROGNAME, szAboutProgName); - SetDlgItemTextW(hDlg, IDC_ABOUT_COPYRIGHT, szAboutCopyright); - SetDlgItemTextW(hDlg, IDC_ABOUT_BUILDTIME, szAboutBuildTime); - SetDlgItemTextW(hDlg, IDC_ABOUTEDIT, szAboutLicense); + WCHAR szAboutProgName[MAX_LOADSTRING]; + WCHAR szAboutCopyright[MAX_LOADSTRING]; + WCHAR szAboutBuildTime[MAX_LOADSTRING]; + WCHAR szAboutLicense[MAX_LOADSTRING * 22]; + LoadStringW(GetModuleHandleW(NULL), IDS_LICENSE, szAboutLicense, _countof(szAboutLicense)); + MultiByteToWideChar(1251, 0, PRODUCT_NAME_FULL, _countof(PRODUCT_NAME_FULL), szAboutProgName, MAX_LOADSTRING); + MultiByteToWideChar(1251, 0, PRODUCT_COPYRIGHT, _countof(PRODUCT_COPYRIGHT), szAboutCopyright, MAX_LOADSTRING); + MultiByteToWideChar(1251, 0, ABOUT_BUILD, _countof(ABOUT_BUILD), szAboutBuildTime, MAX_LOADSTRING); + SetDlgItemTextW(hDlg, IDC_ABOUT_PROGNAME, szAboutProgName); + SetDlgItemTextW(hDlg, IDC_ABOUT_COPYRIGHT, szAboutCopyright); + SetDlgItemTextW(hDlg, IDC_ABOUT_BUILDTIME, szAboutBuildTime); + SetDlgItemTextW(hDlg, IDC_ABOUTEDIT, szAboutLicense); #ifdef NO_DONATION - HWND hLink = GetDlgItem(hDlg, IDC_DONATIONLINK); - if (hLink) DestroyWindow(hLink); - HWND hText = GetDlgItem(hDlg, IDC_DONATIONTEXT); - if (hText) DestroyWindow(hText); + HWND hLink = GetDlgItem(hDlg, IDC_DONATIONLINK); + if (hLink) DestroyWindow(hLink); + HWND hText = GetDlgItem(hDlg, IDC_DONATIONTEXT); + if (hText) DestroyWindow(hText); #endif // !NO_DONATION - logger.Out(L"%s(%d): End of initializing the 'About' dialog", TEXT(__FUNCTION__), __LINE__); + logger.Out(L"%s(%d): End of initializing the 'About' dialog", TEXT(__FUNCTION__), __LINE__); - return static_cast(TRUE); - break; - } + return static_cast(TRUE); + break; + } - case WM_NOTIFY: - { - LPNMHDR pNMHdr = (LPNMHDR)lParam; - 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); + case WM_NOTIFY: + { + LPNMHDR pNMHdr = (LPNMHDR)lParam; + 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); - logger.Out(L"%s(%d): Pressed the donation link! :-)", TEXT(__FUNCTION__), __LINE__); + logger.Out(L"%s(%d): Pressed the donation link! :-)", TEXT(__FUNCTION__), __LINE__); - return static_cast(TRUE); - } - break; - } + return static_cast(TRUE); + } + break; + } - case WM_COMMAND: - { - if (IDOK == LOWORD(wParam) || IDCANCEL == LOWORD(wParam)) - { - EndDialog(hDlg, LOWORD(wParam)); + case WM_COMMAND: + { + if (IDOK == LOWORD(wParam) || IDCANCEL == LOWORD(wParam)) { + EndDialog(hDlg, LOWORD(wParam)); - logger.Out(L"%s(%d): Closing the 'About' dialog", TEXT(__FUNCTION__), __LINE__); + logger.Out(L"%s(%d): Closing the 'About' dialog", TEXT(__FUNCTION__), __LINE__); - return static_cast(TRUE); - } - break; - } - } - return static_cast(FALSE); + return static_cast(TRUE); + } + break; + } + } + return static_cast(FALSE); }