diff --git a/.gitignore b/.gitignore index bb50ac6..142eac4 100644 --- a/.gitignore +++ b/.gitignore @@ -437,3 +437,5 @@ lightgbm.Rcheck/ # GraphViz artifacts *.gv *.gv.* +/VersionInfo.h +/wCenterWindow/VersionInfo.h diff --git a/Update_Version.ps1 b/Update_Version.ps1 new file mode 100644 index 0000000..b8c72d9 --- /dev/null +++ b/Update_Version.ps1 @@ -0,0 +1,173 @@ +# Update Version v2.1 +# Required module: PSIni +# Installation: Install-Module -Name PsIni + +$date = Get-Date + +function addLeadingZero([int]$nvar) { + [string]$svar = $null + if ($nvar -lt 10) { $svar = '0' + $nvar.ToString() } + else { $svar = $nvar.ToString() } + return $svar +} + +function getValue([string]$var) { + switch -CaseSensitive ($var) { + "%y" { $var = $currentYear; break } + "%m" { $var = $currentMonth; break } + "%d" { $var = $currentDay; break } + "%t" { $var = -join ((addLeadingZero($currentHour)), (addLeadingZero($currentMinute)), (addLeadingZero($currentSecond))); break } + "%D" { $var = $spanDays; break } + "%S" { $var = $spanSecs; break } + "%C" { $var = $gitCommitCount; break } + "%c" { $var = $gitRevCount; break } + } + return $var +} + +function makeVersionString([string]$vmaj, [string]$vmin, [string]$vbld, [string]$vrev) { + [string]$vstr = $null + if ($vmin -eq "") { $vmin = $vbld = $vrev = 0; $vstr = ($vmaj) -join '.' } + else { + if ($vbld -eq "") { $vbld = $vrev = 0; $vstr = ($vmaj, $vmin) -join '.' } + else { + if ($vrev -eq "") { $vrev = 0; $vstr = ($vmaj, $vmin, $vbld) -join '.' } + else { $vstr = ($vmaj, $vmin, $vbld, $vrev) -join '.' } + } + } + [string]$nstr = ($vmaj, $vmin, $vbld, $vrev) -join ',' + return @($vstr, $nstr) +} + +#region Initializing variables +[string]$verMajor = [string]$verMinor = [string]$verBuild = [string]$verRevision = $null +[string]$pn = [string]$pa = [string]$aboutBuild = [string]$pnf = [string]$pcf = $null +[string]$buildDateTime = [string]$vs = [string]$vn = [string]$intName = [string]$origName = $null +[int]$pys = [int]$spanDays = [int]$spanSecs = $null +[int]$currentYear = [int]$currentMonth = [int]$currentDay = [int]$currentHour = [int]$currentMinute = [int]$currentSecond = $null +[string]$iniFile = ".\Version.ini" +#endregion + +if (-not (Test-Path $iniFile)) { + Write-Error "Can't find file '$iniFile'" + # Start-Sleep -Seconds 3 + exit 1 +} + +[bool]$isGit = $false +if (Test-Path ".\.git") { + $isGit = $true + [int]$gitCommitCount = [int]$gitRevCount = $null + [string]$gitRevDate = [string]$gitVerStr = [string]$gitAHash = $null + $gitCommitCount = $(git rev-list --count HEAD) + $gitRevDate = $(git log -1 HEAD --date=rfc --pretty=format:%ad%n) + $gitVerStr = $(git describe HEAD --long) + if ($LastExitCode -eq 0) { + $gitVerStr = $gitVerStr.Replace('-g', '-') + $gitRevCount = $gitVerStr.Split('-')[-2] + $gitAHash = $gitVerStr.Split('-')[-1] + } + else { + $gitVerStr = "" + $gitRevCount = 0 + $gitAHash = $(git describe HEAD --always) + } +} + +#region Reading values from INI file +$config = $null +$config = (Get-IniContent -FilePath $iniFile)["Config"] +$verMajor = $config.Major +$verMinor = $config.Minor +$verBuild = $config.Build +$verRevision = $config.Revision +$pys = $config.ProductYearStart +$pn = $config.ProductName +$pa = $config.ProductAutors +$pd = $config.ProductDescription +#endregion + +if ($verMajor -eq "") { + Write-Error "Major version cannot be empty!" + # Start-Sleep -Seconds 3 + exit 2 +} + +#region Variables preparation +$currentYear = $date.Year +$currentMonth = $date.Month +$currentDay = $date.Day +$currentHour = $date.Hour +$currentMinute = $date.Minute +$currentSecond = $date.Second +$buildDateTime = "Build date: $($date.GetDateTimeFormats('u').Replace('Z', ''))" +$spanDays = [math]::Round((New-TimeSpan -Start $(Get-Date -Month 1 -Day 1 -Year 2000) -End $date).TotalDays) +$spanSecs = [math]::Round((New-TimeSpan -Start $($date.Date) -End $($date.DateTime)).TotalSeconds) +if ($pys -eq $currentYear) { $pcf = "Copyright (C) $pys by $pa" } else { $pcf = "Copyright (C) $pys-$currentYear by $pa" } +$intName = "$pn-C++" +$origName = "$pn.exe" +$verMajor = getValue $verMajor +$verMinor = getValue $verMinor +$verBuild = getValue $verBuild +$verRevision = getValue $verRevision +#endregion + +$vs = (makeVersionString $verMajor $verMinor $verBuild $verRevision)[0] +$vn = (makeVersionString $verMajor $verMinor $verBuild $verRevision)[1] + +# if ([string]::IsNullOrEmpty($gitVerStr)) { $pnf = "$pn v$vs" } else { $pnf = "$pn $gitVerStr" } +if ($isGit) { + $aboutBuild = "Git date: $gitRevDate" + $pnf = "$pn v$vs ($gitAHash)" +} +else { + $aboutBuild = $buildDateTime + $pnf = "$pn v$vs" +} + +#region Save all variables to file +"// $pn" | Out-File -FilePath ".\VersionInfo.h" -Encoding unicode +"// VersionInfo.h" | Out-File -FilePath ".\VersionInfo.h" -Encoding unicode -Append +"#pragma once" | Out-File -FilePath ".\VersionInfo.h" -Encoding unicode -Append +"#define PRODUCT_NAME `"$pn`"" | Out-File -FilePath ".\VersionInfo.h" -Encoding unicode -Append +"#define PRODUCT_NAME_FULL `"$pnf`"" | Out-File -FilePath ".\VersionInfo.h" -Encoding unicode -Append +"#define PRODUCT_AUTHORS `"$pa`"" | Out-File -FilePath ".\VersionInfo.h" -Encoding unicode -Append +"#define PRODUCT_DESCRIPTION `"$pd`"" | Out-File -FilePath ".\VersionInfo.h" -Encoding unicode -Append +"#define ABOUT_BUILD `"$aboutBuild`"" | Out-File -FilePath ".\VersionInfo.h" -Encoding unicode -Append +"#define INTERNAL_NAME `"$intName`"" | Out-File -FilePath ".\VersionInfo.h" -Encoding unicode -Append +"#define ORIG_FILE_NAME `"$origName`"" | Out-File -FilePath ".\VersionInfo.h" -Encoding unicode -Append +"#define PRODUCT_COPYRIGHT `"$pcf`"" | Out-File -FilePath ".\VersionInfo.h" -Encoding unicode -Append +"#define VERSION_STR `"$vs`"" | Out-File -FilePath ".\VersionInfo.h" -Encoding unicode -Append +"#define VERSION_NUM $vn" | Out-File -FilePath ".\VersionInfo.h" -Encoding unicode -Append +# "#define SPAN_DAYS $spanDays" | Out-File -FilePath ".\VersionInfo.h" -Encoding unicode -Append +# "#define SPAN_SECS $spanSecs" | Out-File -FilePath ".\VersionInfo.h" -Encoding unicode -Append + +# if ($isGit) { +# # "#define GIT_VERSION_STR `"$gitVerStr`"" | Out-File -FilePath ".\VersionInfo.h" -Encoding unicode -Append +# # "#define GIT_REV_DATE `"Git date: $gitRevDate`"" | Out-File -FilePath ".\VersionInfo.h" -Encoding unicode -Append +# # "#define GIT_REV_COUNT $gitRevCount" | Out-File -FilePath ".\VersionInfo.h" -Encoding unicode -Append +# # "#define GIT_COMMIT_COUNT $gitCommitCount" | Out-File -FilePath ".\VersionInfo.h" -Encoding unicode -Append +# } +#endregion + +#region Print out all variables +# echo " verMajor: [$verMajor]" +# echo " verMinor: [$verMinor]" +# echo " verBuild: [$verBuild]" +# echo " verRevision: [$verRevision]" +# echo "" +# echo " aboutBuild: [$aboutBuild]" +# echo " productNameFull: [$pnf]" +# echo "productCopyrightFull: [$pcf]" +# echo " internalName: [$intName]" +# echo " originalName: [$origName]" +# echo " versionStr: [$vs]" +# echo " versionNum: [$vn]" +# echo " spanDays: [$spanDays]" +# echo " spanSecs: [$spanSecs]" +# echo "" +# echo " gitCommitCount: [$gitCommitCount]" +# echo " gitRevCount: [$gitRevCount]" +# echo " gitRevDate: [$gitRevDate]" +# echo " gitVerStr: [$gitVerStr]" +#endregion diff --git a/Update_version.bat b/Update_version.bat deleted file mode 100644 index 7f79ddb..0000000 --- a/Update_version.bat +++ /dev/null @@ -1,81 +0,0 @@ -@ECHO OFF -SETLOCAL ENABLEDELAYEDEXPANSION - -SET CURRENT_TIME=%TIME% -SET CURRENT_DATE=%DATE% - -SET BUILDTIME=%CURRENT_TIME:~0,8% -SET BUILDDATE=%CURRENT_DATE% -SET BUILD_DATETIME=Build time: %BUILDDATE% %BUILDTIME% -SET CURRENT_YEAR=%CURRENT_DATE:~6,4% - -SET BUILDSECS=0 -SET GIT_COUNT=0 -SET GIT_TIME=0 -SET GIT_DATE=0 -SET GIT_DATETIME=0 - -SET VerMajor=0 -SET VerMinor=0 -SET VerPatch=0 - -SET INT_NAME=0 -SET PN=0 -SET VS=0 -SET VSF=0 -SET PCF=0 -SET PYS=0 -SET PA=0 - -CD /D %~dp0 -IF NOT EXIST "VersionInfo.h" ( - ECHO Can't find file 'VersionInfo.h' - TIMEOUT /T 3 - EXIT /B 1 -) -COPY /Y "VersionInfo.h" "version.h" >nul - -FOR /F "tokens=3" %%A IN ('FINDSTR /I /L /C:"define V_MAJOR" "version.h"') DO (SET "VerMajor=%%A") -FOR /F "tokens=3" %%A IN ('FINDSTR /I /L /C:"define V_MINOR" "version.h"') DO (SET "VerMinor=%%A") -FOR /F "tokens=3" %%A IN ('FINDSTR /I /L /C:"define V_PATCH" "version.h"') DO (SET "VerPatch=%%A") -FOR /F "tokens=3" %%A IN ('FINDSTR /I /L /C:"define PRODUCT_NAME" "version.h"') DO (SET "PN=%%~A") -FOR /F "tokens=3" %%A IN ('FINDSTR /I /L /C:"define PRODUCT_YEAR_START" "version.h"') DO (SET "PYS=%%A") -FOR /F "tokens=2*" %%A IN ('FINDSTR /I /L /C:"define PRODUCT_AUTHORS" "version.h"') DO (SET "PA=%%~B") -FOR /F "tokens=1-4 delims=:., " %%A IN ("%BUILDTIME%") DO (SET /A "BUILDSECS=%%A * 3600 + %%B * 60 + %%C") -FOR /F "delims=" %%A IN ('git rev-list --count HEAD') DO (SET /A GIT_COUNT=%%A) -FOR /F "tokens=1,2 delims= " %%A IN ('git log -1 --date=format:%%d.%%m.%%Y ^| find /I "Date:"') DO (SET "GIT_DATE=%%B") -FOR /F "tokens=2-4 delims=, " %%A IN ('git log -1 --date=format:"%%a,%%d-%%h-%%Y,%%T" ^| find /I "Date:"') DO ( - SET "GIT_DATETIME=Git time: %%A, %%B %%C" - SET "GIT_TIME=%%C" -) - -SET VSF=%VerMajor%.%VerMinor%.%VerPatch%.%GIT_COUNT% -SET VS=%VerMajor%.%VerMinor%.%VerPatch% -SET VNF=%VerMajor%,%VerMinor%,%VerPatch%,%GIT_COUNT% -SET VN=%VerMajor%,%VerMinor%,%VerPatch% - -SET PNF=%PN% v%VS% (C++) -SET PCF=Copyright (C) %PYS%-%CURRENT_YEAR% by %PA% - -SET INT_NAME=%PN%C++ -SET ORIG_NAME=%PN%.exe - -ECHO #define BUILD_DATE "%BUILDDATE%">> version.h -ECHO #define BUILD_TIME "%BUILDTIME%">> version.h -ECHO #define BUILD_DATETIME "%BUILD_DATETIME%">> version.h -ECHO #define GIT_DATE "%GIT_DATE%">> version.h -ECHO #define GIT_TIME "%GIT_TIME%">> version.h -ECHO #define GIT_DATETIME "%GIT_DATETIME%">> version.h -ECHO #define GIT_COUNT %GIT_COUNT% >> version.h -ECHO #define V_SECS %BUILDSECS% >> version.h -ECHO #define INTERNAL_NAME "%INT_NAME%">> version.h -ECHO #define ORIG_FILE_NAME "%ORIG_NAME%">> version.h -ECHO #define PRODUCT_NAME_FULL "%PNF%">> version.h -ECHO #define PRODUCT_COPYRIGHT "%PCF%">> version.h -ECHO #define VERSION_NUM %VN% >> version.h -ECHO #define VERSION_STR "%VS%">> version.h -ECHO #define VERSION_NUM_FULL %VNF% >> version.h -ECHO #define VERSION_STR_FULL "%VSF%">> version.h - -ENDLOCAL -EXIT diff --git a/Version.ini b/Version.ini new file mode 100644 index 0000000..5fdfc1a Binary files /dev/null and b/Version.ini differ diff --git a/VersionInfo.h b/VersionInfo.h deleted file mode 100644 index 1b74b01..0000000 --- a/VersionInfo.h +++ /dev/null @@ -1,12 +0,0 @@ -// wCenterWindow -// version.h -// -#pragma once - -#define V_MAJOR 2 -#define V_MINOR 3 -#define V_PATCH 4 -#define PRODUCT_NAME "wCenterWindow" -#define PRODUCT_AUTHORS "W0LF aka 'dreamforce'" -#define PRODUCT_YEAR_START 2020 -#define PRODUCT_DESCRIPTION "Centers windows by hotkey" diff --git a/pre-build.cmd b/pre-build.cmd new file mode 100644 index 0000000..f063674 --- /dev/null +++ b/pre-build.cmd @@ -0,0 +1,19 @@ +@ECHO OFF + +REM In VisualStudio add to Pre-Build events: +REM "$(SolutionDir)pre-build.cmd" "$(SolutionDir)" "$(ProjectDir)" +REM Then in "Resource Includes..." add '#include "VersionInfo.rc"' into 'Compile-time Directives' + +IF "%~1" == "" GOTO :no_args +IF "%~2" == "" GOTO :no_args +SET solutionDir=%1 +SET projectDir=%2 + +CD /D %solutionDir% +powershell -ExecutionPolicy RemoteSigned -File Update_Version.ps1 +MOVE /Y %solutionDir%VersionInfo.h %projectDir%VersionInfo.h +EXIT + +:no_args +ECHO Not enough arguments! +EXIT 1 diff --git a/wCenterWindow.sln b/wCenterWindow.sln index df62922..f277e52 100644 --- a/wCenterWindow.sln +++ b/wCenterWindow.sln @@ -1,10 +1,15 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.31112.23 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.33516.290 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "wCenterWindow", "wCenterWindow\wCenterWindow.vcxproj", "{F1A1603A-F5D0-47B8-8E4B-CF17747BCFBA}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C7B4FD1E-DC1B-46A4-842C-5130F09EFCB5}" + ProjectSection(SolutionItems) = preProject + Version.ini = Version.ini + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 diff --git a/wCenterWindow/Logger.cpp b/wCenterWindow/Logger.cpp deleted file mode 100644 index bbcabeb..0000000 --- a/wCenterWindow/Logger.cpp +++ /dev/null @@ -1,70 +0,0 @@ -// wCenterWindow -// Logger.cpp -// -#include "framework.h" - -#define TS_LEN 30 - -std::ofstream logfile; -extern WCHAR szTitle[]; -extern LPVOID szBuffer; - -std::string GetTimeStamp() -{ - SYSTEMTIME lt; - GetLocalTime(<); - CHAR ts[TS_LEN]; - StringCchPrintfA(ts, TS_LEN, "%d-%02d-%02d %02d:%02d:%02d.%03d - ", lt.wYear, lt.wMonth, lt.wDay, lt.wHour, lt.wMinute, lt.wSecond, lt.wMilliseconds); - return ts; -} - -void OpenLogFile() -{ - WCHAR lpszPath[MAX_PATH + 1] = { 0 }; - DWORD dwPathLength = GetModuleFileNameW(NULL, lpszPath, MAX_PATH); - DWORD dwError = GetLastError(); - if (ERROR_INSUFFICIENT_BUFFER == dwError) - { - MessageBoxW(NULL, L"Path to logfile is too long! Working without logging", (LPCWSTR)szTitle, MB_OK | MB_ICONWARNING); - return; - } - if (NULL == dwPathLength) - { - MessageBoxW(NULL, L"Can't get module filename! Working without logging", (LPCWSTR)szTitle, MB_OK | MB_ICONWARNING); - return; - } - - std::filesystem::path log_path = lpszPath; - log_path.replace_extension(L".log"); - std::filesystem::path bak_path = log_path; - bak_path.replace_extension(L".bak"); - - if (std::filesystem::exists(log_path)) std::filesystem::rename(log_path, bak_path); - -#ifdef _DEBUG - log_path = L"d:\\test.log"; -#endif - - logfile.open(log_path); - if (logfile.is_open()) - { - logfile << std::boolalpha; - diag_log("Start logging"); - diag_log("Logfile: ", log_path); - diag_log("Logfile was successfully opened"); - } - else - { - MessageBoxW(NULL, L"Can't open logfile! Working without logging", (LPCWSTR)szTitle, MB_OK | MB_ICONWARNING); - } - return; -} - -void CloseLogFile() -{ - if (logfile) - { - diag_log("End logging"); - logfile.close(); - } -} diff --git a/wCenterWindow/Logger.h b/wCenterWindow/Logger.h index 8d15d2f..d51aa14 100644 --- a/wCenterWindow/Logger.h +++ b/wCenterWindow/Logger.h @@ -1,59 +1,71 @@ -// wCenterWindow -// Logger.h -// +// wLogger v3.1 (Edited version from RBTray project [https://github.com/benbuck/rbtray]) +// logger.h +// Usage: LOG_TO_FILE(L"%s(%d): Log message", TEXT(__FUNCTION__), __LINE__); #pragma once #include "framework.h" -extern std::ofstream logfile; -std::string GetTimeStamp(); +#define DBUFLEN 256 +#define LOG_TO_FILE(fmt, ...) do { StringCchPrintfW(debugBuffer, DBUFLEN, fmt, ##__VA_ARGS__); logfile << GetTimeStamp() << debugBuffer << std::endl; } while (0) -template -void diag_log(T1 arg1) +SYSTEMTIME lt; +wchar_t debugTimeBuffer[32]; +wchar_t debugBuffer[DBUFLEN]; +std::wofstream logfile; + +extern wchar_t szTitle[]; + +wchar_t* GetTimeStamp() { - logfile << GetTimeStamp() << arg1 << std::endl; + GetLocalTime(<); + StringCchPrintfW(debugTimeBuffer, 32, L"%d-%02d-%02d %02d:%02d:%02d.%03d | ", lt.wYear, lt.wMonth, lt.wDay, lt.wHour, lt.wMinute, lt.wSecond, lt.wMilliseconds); + return debugTimeBuffer; } -template -void diag_log(T1 arg1, T2 arg2) +void OpenLogFile() { - logfile << GetTimeStamp() << arg1 << arg2 << std::endl; + WCHAR lpszPath[MAX_PATH + 1] = { 0 }; + DWORD dwPathLength = GetModuleFileNameW(NULL, lpszPath, MAX_PATH); + DWORD dwError = GetLastError(); + if (ERROR_INSUFFICIENT_BUFFER == dwError) + { + MessageBoxW(NULL, L"Path to logfile is too long! Working without logging", szTitle, MB_OK | MB_ICONWARNING); + return; + } + if (NULL == dwPathLength) + { + MessageBoxW(NULL, L"Can't get module filename! Working without logging", szTitle, MB_OK | MB_ICONWARNING); + return; + } + + std::filesystem::path log_path = lpszPath; + log_path.replace_extension(L".log"); + std::filesystem::path bak_path = log_path; + bak_path.replace_extension(L".bak"); + + if (std::filesystem::exists(log_path)) std::filesystem::rename(log_path, bak_path); +#ifdef _DEBUG + log_path = L"D:\\test.log"; +#endif + logfile.open(log_path, std::ios::trunc); + if (logfile.is_open()) + { + logfile << "\xEF\xBB\xBF"; // (0xEF, 0xBB, 0xBF) - UTF-8 BOM + logfile.imbue(std::locale("en-US.utf8")); + logfile << GetTimeStamp() << "[ " << szTitle << " ] Start log." << std::endl; + logfile << GetTimeStamp() << "Logfile: \"" << log_path.native() << "\"" << std::endl; + } + else + { + MessageBoxW(NULL, L"Can't open logfile! Working without logging", szTitle, MB_OK | MB_ICONWARNING); + } + return; } -template -void diag_log(T1 arg1, T2 arg2, T3 arg3) +void CloseLogFile() { - logfile << GetTimeStamp() << arg1 << arg2 << arg3 << std::endl; + if (logfile) + { + logfile << GetTimeStamp() << "[ " << szTitle << " ] Stop log." << std::endl; + logfile.close(); + } } - -template -void diag_log(T1 arg1, T2 arg2, T3 arg3, T4 arg4) -{ - logfile << GetTimeStamp() << arg1 << arg2 << arg3 << arg4 << std::endl; -} - -template -void diag_log(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) -{ - logfile << GetTimeStamp() << arg1 << arg2 << arg3 << arg4 << arg5 << std::endl; -} - -template -void diag_log(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) -{ - logfile << GetTimeStamp() << arg1 << arg2 << arg3 << arg4 << arg5 << arg6 << std::endl; -} - -template -void diag_log(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7) -{ - logfile << GetTimeStamp() << arg1 << arg2 << arg3 << arg4 << arg5 << arg6 << arg7 << std::endl; -} - -template -void diag_log(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8) -{ - logfile << GetTimeStamp() << arg1 << arg2 << arg3 << arg4 << arg5 << arg6 << arg7 << arg8 << std::endl; -} - -void OpenLogFile(); -void CloseLogFile(); diff --git a/wCenterWindow/VersionInfo.rc b/wCenterWindow/VersionInfo.rc new file mode 100644 index 0000000..bf98286 Binary files /dev/null and b/wCenterWindow/VersionInfo.rc differ diff --git a/wCenterWindow/framework.h b/wCenterWindow/framework.h index f8f264b..9da34e8 100644 --- a/wCenterWindow/framework.h +++ b/wCenterWindow/framework.h @@ -6,7 +6,6 @@ #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers // Windows Header Files -#include #include #include #include @@ -16,4 +15,9 @@ #include // Project Specific Header Files -#include "Logger.h" +#include "logger.h" +#include "VersionInfo.h" + +// Extern variables +#define MAX_LOADSTRING 50 +WCHAR szTitle[MAX_LOADSTRING]; // wCenterWindow's title diff --git a/wCenterWindow/resource.h b/wCenterWindow/resource.h index 1f65376..541fa76 100644 --- a/wCenterWindow/resource.h +++ b/wCenterWindow/resource.h @@ -33,6 +33,10 @@ #define IDC_BUTTON_CLOSE 1007 #define IDC_ABOUTHELP 1008 #define IDC_DONATIONLINK 1009 +#define IDC_ABOUT_PROGNAME 1010 +#define IDC_ABOUT_COPYRIGHT 1011 +#define IDC_ABOUT_BUILDTIME 1012 +#define IDC_ABOUTEDIT 1013 #define IDC_STATIC -1 // Next default values for new objects @@ -42,7 +46,7 @@ #define _APS_NO_MFC 1 #define _APS_NEXT_RESOURCE_VALUE 130 #define _APS_NEXT_COMMAND_VALUE 32771 -#define _APS_NEXT_CONTROL_VALUE 1010 +#define _APS_NEXT_CONTROL_VALUE 1014 #define _APS_NEXT_SYMED_VALUE 122 #endif #endif diff --git a/wCenterWindow/wCenterWindow.cpp b/wCenterWindow/wCenterWindow.cpp index f2ce305..e8182db 100644 --- a/wCenterWindow/wCenterWindow.cpp +++ b/wCenterWindow/wCenterWindow.cpp @@ -7,32 +7,30 @@ // TODO: Make x64 version. // #include "framework.h" +#include "logger.h" #include "wCenterWindow.h" +#define NO_DONATION #define KEY_I 0x49 #define KEY_C 0x43 #define KEY_V 0x56 #define BUF_LEN 1024 -#define MAX_LOADSTRING 50 #define WM_WCW 0x8F00 // Global variables: HINSTANCE hInst; // Instance -WCHAR szTitle[MAX_LOADSTRING]; // Window's title +extern WCHAR szTitle[]; WCHAR szClass[MAX_LOADSTRING]; // Window's class -WCHAR szAbout[MAX_LOADSTRING * 12]; // Description text WCHAR szWinTitle[256]; WCHAR szWinClass[256]; -WCHAR szWinCore[] = L"Windows.UI.Core.CoreWindow"; -WCHAR szWorkerW[] = L"WorkerW"; HANDLE hHeap = NULL; HHOOK hMouseHook = NULL, hKbdHook = NULL; // Hook's handles HICON hIcon = NULL; HMENU hMenu = NULL, hPopup = NULL; -HWND hWnd = NULL, hFgWnd = NULL, hTaskBar = NULL, hDesktop = NULL, hProgman = NULL; -bool bKPressed = FALSE, bMPressed = FALSE, bShowIcon = TRUE, bWorkArea = TRUE; -bool bLCTRL = FALSE, bLWIN = FALSE, bKEYV = FALSE; +HWND hWnd = NULL, hFgWnd = NULL; +BOOL bKPressed = FALSE, bMPressed = FALSE, bShowIcon = TRUE, bWorkArea = TRUE; +BOOL bLCTRL = FALSE, bLWIN = FALSE, bKEYV = FALSE; RECT rcFW = { 0 }; NOTIFYICONDATAW nid = { 0 }; @@ -47,26 +45,23 @@ static const GUID guid = { 0x2d7b7f30, 0x4b5f, 0x4380, { 0x98, 0x7, 0x57, 0xd7, // Forward declarations of functions included in this code module: VOID HandlingTrayIcon(); VOID ShowError(UINT); -bool IsWindowApprooved(HWND); +BOOL IsWindowApprooved(HWND); BOOL CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK KeyboardHookProc(int, WPARAM, LPARAM); LRESULT CALLBACK MouseHookProc(int, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); -std::string ConvertWideToUtf8(const std::wstring&); - - VOID MoveWindowToMonitorCenter(HWND hwnd, BOOL bWorkArea, BOOL bResize) { - diag_log("Entering MoveWindowToMonitorCenter(), handle = 0x", hwnd); + LOG_TO_FILE(L"Entering the %s() function", TEXT(__FUNCTION__)); RECT fgwrc = { 0 }; GetWindowRect(hwnd, &fgwrc); LONG nWidth = fgwrc.right - fgwrc.left; LONG nHeight = fgwrc.bottom - fgwrc.top; - diag_log("Moving window from x = ", fgwrc.left, ", y = ", fgwrc.top); + LOG_TO_FILE(L"%s(%d): Moving the window from %d, %d", TEXT(__FUNCTION__), __LINE__, fgwrc.left, fgwrc.top); MONITORINFO mi = { 0 }; mi.cbSize = sizeof(MONITORINFO); @@ -100,12 +95,10 @@ VOID MoveWindowToMonitorCenter(HWND hwnd, BOOL bWorkArea, BOOL bResize) MoveWindow(hwnd, x, y, nWidth, nHeight, TRUE); SendMessageW(hwnd, WM_EXITSIZEMOVE, NULL, NULL); - diag_log("Moving window to x = ", x, ", y = ", y); - diag_log("Quiting MoveWindowToMonitorCenter()"); + LOG_TO_FILE(L"%s(%d): Moving the window to %d, %d", TEXT(__FUNCTION__), __LINE__, x, y); + LOG_TO_FILE(L"Exit from the %s() function", TEXT(__FUNCTION__)); } - - int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) { hInst = hInstance; @@ -120,13 +113,15 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd } OpenLogFile(); - diag_log("Entering WinMain()"); + + LOG_TO_FILE(L"Entering the %s() function", TEXT(__FUNCTION__)); WNDCLASSEX wcex = { 0 }; wcex.cbSize = sizeof(WNDCLASSEX); wcex.lpfnWndProc = WndProc; wcex.hInstance = hInstance; wcex.hIcon = LoadIconW(hInstance, MAKEINTRESOURCE(IDI_TRAYICON)); + LoadIconMetric(hInst, MAKEINTRESOURCE(IDI_TRAYICON), LIM_LARGE, &(wcex.hIcon)); wcex.hCursor = LoadCursorW(nullptr, IDC_ARROW); wcex.lpszClassName = szClass; wcex.hIconSm = wcex.hIcon; @@ -146,13 +141,12 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd int nArgs = 0; LPWSTR* szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs); - std::string arg; - diag_log("Arguments: ", nArgs - 1); + LOG_TO_FILE(L"Arguments count: %d", nArgs - 1); + for (int i = 1; i < nArgs; i++) { - arg = ConvertWideToUtf8(szArglist[i]); - diag_log("Argument #", i, ": ", arg); + LOG_TO_FILE(L"Argument %d: %s", i, szArglist[i]); } (nArgs >= 2 && 0 == lstrcmpiW(szArglist[1], L"/hide")) ? bShowIcon = FALSE : bShowIcon = TRUE; @@ -162,10 +156,6 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd hHeap = GetProcessHeap(); szBuffer = HeapAlloc(hHeap, HEAP_ZERO_MEMORY, BUF_LEN); - hTaskBar = FindWindowW(L"Shell_TrayWnd", NULL); - hProgman = FindWindowW(L"Progman", NULL); - hDesktop = GetDesktopWindow(); - MSG msg; BOOL bRet; @@ -187,8 +177,10 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd if (hKbdHook) UnhookWindowsHookEx(hKbdHook); if (hMenu) DestroyMenu(hMenu); Shell_NotifyIconW(NIM_DELETE, &nid); + DestroyIcon(hIcon); + + LOG_TO_FILE(L"Exit from the %s() function, msg.wParam = %d", TEXT(__FUNCTION__), (int)msg.wParam); - diag_log("Quiting WinMain(), msg.wParam = ", (int)msg.wParam); CloseLogFile(); HeapFree(hHeap, NULL, szBuffer); @@ -199,125 +191,131 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { - case WM_CREATE: + case WM_CREATE: + { + LOG_TO_FILE(L"%s(%d): Recived WM_CREATE message", TEXT(__FUNCTION__), __LINE__); + + hMenu = LoadMenuW(hInst, MAKEINTRESOURCE(IDR_MENU)); + if (!hMenu) { - diag_log("Recived WM_CREATE message"); - hMenu = LoadMenuW(hInst, MAKEINTRESOURCE(IDR_MENU)); - if (!hMenu) - { - diag_log("Loading context menu failed!"); - ShowError(IDS_ERR_MENU); - PostMessageW(hWnd, WM_CLOSE, NULL, NULL); - } - diag_log("Context menu successfully loaded"); + LOG_TO_FILE(L"%s(%d): Loading context menu failed!", TEXT(__FUNCTION__), __LINE__); + ShowError(IDS_ERR_MENU); + PostMessageW(hWnd, WM_CLOSE, NULL, NULL); + } + LOG_TO_FILE(L"%s(%d): Context menu successfully loaded", TEXT(__FUNCTION__), __LINE__); - hPopup = GetSubMenu(hMenu, 0); - if (!hPopup) - { - diag_log("Creating popup menu failed!"); - ShowError(IDS_ERR_POPUP); - PostMessageW(hWnd, WM_CLOSE, NULL, NULL); - } - diag_log("Popup menu successfully created"); + hPopup = GetSubMenu(hMenu, 0); + if (!hPopup) + { + LOG_TO_FILE(L"%s(%d): Creating popup menu failed!", TEXT(__FUNCTION__), __LINE__); + ShowError(IDS_ERR_POPUP); + PostMessageW(hWnd, WM_CLOSE, NULL, NULL); + } + LOG_TO_FILE(L"%s(%d): Popup menu successfully created", TEXT(__FUNCTION__), __LINE__); - mii.cbSize = sizeof(MENUITEMINFO); - mii.fMask = MIIM_STATE; - bWorkArea ? mii.fState = MFS_CHECKED : mii.fState = MFS_UNCHECKED; - SetMenuItemInfoW(hPopup, ID_POPUPMENU_AREA, FALSE, &mii); + mii.cbSize = sizeof(MENUITEMINFO); + mii.fMask = MIIM_STATE; + bWorkArea ? mii.fState = MFS_CHECKED : mii.fState = MFS_UNCHECKED; + SetMenuItemInfoW(hPopup, ID_POPUPMENU_AREA, FALSE, &mii); - nid.cbSize = sizeof(NOTIFYICONDATAW); - nid.hWnd = hWnd; - nid.uVersion = NOTIFYICON_VERSION; - nid.uCallbackMessage = WM_WCW; - nid.hIcon = hIcon; - nid.uID = IDI_TRAYICON; - nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; - nid.dwInfoFlags = NIIF_NONE; - nid.dwState = NIS_HIDDEN; - nid.dwStateMask = NIS_HIDDEN; - StringCchCopyW(nid.szTip, _countof(nid.szTip), szTitle); + nid.cbSize = sizeof(NOTIFYICONDATAW); + nid.hWnd = hWnd; + nid.uVersion = NOTIFYICON_VERSION; + nid.uCallbackMessage = WM_WCW; + nid.hIcon = hIcon; + nid.uID = IDI_TRAYICON; + nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; + StringCchCopyW(nid.szTip, _countof(nid.szTip), szTitle); #ifndef _DEBUG - hMouseHook = SetWindowsHookExW(WH_MOUSE_LL, MouseHookProc, hInst, NULL); - if (!hMouseHook) - { - diag_log("Creating mouse hook failed!"); - ShowError(IDS_ERR_HOOK); - PostMessageW(hWnd, WM_CLOSE, NULL, NULL); - } - diag_log("Mouse hook was successfully set"); + hMouseHook = SetWindowsHookExW(WH_MOUSE_LL, MouseHookProc, hInst, NULL); + if (!hMouseHook) + { + LOG_TO_FILE(L"%s(%d): Mouse hook creation failed!", TEXT(__FUNCTION__), __LINE__); + + ShowError(IDS_ERR_HOOK); + PostMessageW(hWnd, WM_CLOSE, NULL, NULL); + } + LOG_TO_FILE(L"%s(%d): The mouse hook was successfully installed", TEXT(__FUNCTION__), __LINE__); #endif // !_DEBUG - hKbdHook = SetWindowsHookExW(WH_KEYBOARD_LL, KeyboardHookProc, hInst, NULL); - if (!hKbdHook) + hKbdHook = SetWindowsHookExW(WH_KEYBOARD_LL, KeyboardHookProc, hInst, NULL); + if (!hKbdHook) + { + LOG_TO_FILE(L"%s(%d): Keyboard hook creation failed!", TEXT(__FUNCTION__), __LINE__); + + ShowError(IDS_ERR_HOOK); + PostMessageW(hWnd, WM_CLOSE, NULL, NULL); + } + LOG_TO_FILE(L"%s(%d): The keyboard hook was successfully installed", TEXT(__FUNCTION__), __LINE__); + break; + } + + case WM_WCW: // Popup menu handler + { + if (IDI_TRAYICON == wParam && (WM_RBUTTONDOWN == lParam || WM_LBUTTONDOWN == lParam)) + { + LOG_TO_FILE(L"%s(%d): Entering the WM_WCW message handler", TEXT(__FUNCTION__), __LINE__); + + SetForegroundWindow(hWnd); + POINT pt; + GetCursorPos(&pt); + int idMenu = TrackPopupMenu(hPopup, TPM_RETURNCMD, pt.x, pt.y, 0, hWnd, NULL); + if (ID_POPUPMENU_ICON == idMenu) { - diag_log("Creating keyboard hook failed!"); - ShowError(IDS_ERR_HOOK); + LOG_TO_FILE(L"%s(%d): Pressed the 'Hide icon' menuitem", TEXT(__FUNCTION__), __LINE__); + + bShowIcon = FALSE; + HandlingTrayIcon(); + } + if (ID_POPUPMENU_AREA == idMenu) + { + LOG_TO_FILE(L"%s(%d): Pressed the 'Use workarea' menuitem", TEXT(__FUNCTION__), __LINE__); + + bWorkArea = !bWorkArea; + bWorkArea ? mii.fState = MFS_CHECKED : mii.fState = MFS_UNCHECKED; + SetMenuItemInfoW(hPopup, ID_POPUPMENU_AREA, FALSE, &mii); + LOG_TO_FILE(L"%s(%d): Changed 'Use workarea' option to %s", TEXT(__FUNCTION__), __LINE__, bWorkArea ? L"True" : L"False"); + } + if (ID_POPUPMENU_ABOUT == idMenu && !bKPressed) + { + LOG_TO_FILE(L"%s(%d): Pressed the 'About' menuitem", TEXT(__FUNCTION__), __LINE__); + + bKPressed = TRUE; + DialogBoxW(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, (DLGPROC)About); + bKPressed = FALSE; + } + if (ID_POPUPMENU_EXIT == idMenu) + { + LOG_TO_FILE(L"%s(%d): Pressed the 'Exit' menuitem", TEXT(__FUNCTION__), __LINE__); + PostMessageW(hWnd, WM_CLOSE, NULL, NULL); } - diag_log("Keyboard hook was successfully set"); - LoadStringW(hInst, IDS_ABOUT, szAbout, _countof(szAbout)); - break; + LOG_TO_FILE(L"%s(%d): Exit from the WM_WCW message handler", TEXT(__FUNCTION__), __LINE__); } + break; + } - case WM_WCW: - { - if (IDI_TRAYICON == wParam && (WM_RBUTTONDOWN == lParam || WM_LBUTTONDOWN == lParam)) - { - diag_log("Entering menu handler"); - SetForegroundWindow(hWnd); - POINT pt; - GetCursorPos(&pt); - int idMenu = TrackPopupMenu(hPopup, TPM_RETURNCMD, pt.x, pt.y, 0, hWnd, NULL); - if (ID_POPUPMENU_ICON == idMenu) - { - diag_log("Pressed 'Hide icon' menuitem"); - bShowIcon = FALSE; - HandlingTrayIcon(); - } - if (ID_POPUPMENU_AREA == idMenu) - { - diag_log("Pressed 'Use workarea' menuitem"); - bWorkArea = !bWorkArea; - bWorkArea ? mii.fState = MFS_CHECKED : mii.fState = MFS_UNCHECKED; - SetMenuItemInfoW(hPopup, ID_POPUPMENU_AREA, FALSE, &mii); - diag_log("Changed 'Use workarea' option to ", bWorkArea); - } - if (ID_POPUPMENU_ABOUT == idMenu && !bKPressed) - { - diag_log("Pressed 'About' menuitem"); - bKPressed = TRUE; - DialogBoxW(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, (DLGPROC)About); - bKPressed = FALSE; - } - if (ID_POPUPMENU_EXIT == idMenu) - { - diag_log("Pressed 'Exit' menuitem"); - PostMessageW(hWnd, WM_CLOSE, NULL, NULL); - } - diag_log("Quiting menu handler"); - } - break; - } + case WM_QUERYENDSESSION: + { + LOG_TO_FILE(L"%s(%d): Recieved the WM_QUERYENDSESSION message, lParam = 0x%08X", TEXT(__FUNCTION__), __LINE__, (long)lParam); - case WM_QUERYENDSESSION: - { - diag_log("Recieved WM_QUERYENDSESSION message, lParam = ", lParam); - CloseLogFile(); - return TRUE; - break; - } + CloseLogFile(); + return TRUE; + break; + } - case WM_DESTROY: - { - diag_log("Recived WM_DESTROY message"); - PostQuitMessage(0); - break; - } + case WM_DESTROY: + { + LOG_TO_FILE(L"%s(%d): Recieved the WM_DESTROY message", TEXT(__FUNCTION__), __LINE__); - default: - return DefWindowProcW(hWnd, message, wParam, lParam); + PostQuitMessage(0); + break; + } + + default: + return DefWindowProcW(hWnd, message, wParam, lParam); } return 0; } @@ -327,7 +325,8 @@ LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam) if (WM_MBUTTONUP == wParam) bMPressed = FALSE; if (WM_MBUTTONDOWN == wParam && bLCTRL && bLWIN && !bMPressed) { - diag_log("Pressed LCTRL + LWIN + MMB"); + LOG_TO_FILE(L"%s(%d): Pressed LCTRL + LWIN + MMB", TEXT(__FUNCTION__), __LINE__); + bMPressed = TRUE; hFgWnd = GetForegroundWindow(); if (IsWindowApprooved(hFgWnd)) MoveWindowToMonitorCenter(hFgWnd, bWorkArea, FALSE); @@ -339,7 +338,6 @@ LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam) LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) { pkhs = (KBDLLHOOKSTRUCT*)lParam; - if (WM_KEYUP == wParam) { if (VK_LCONTROL == pkhs->vkCode) bLCTRL = FALSE; @@ -354,7 +352,8 @@ LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) if (KEY_I == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed) // 'I' key { - diag_log("Pressed LCTRL + LWIN + I"); + LOG_TO_FILE(L"%s(%d): Pressed LCTRL + LWIN + I", TEXT(__FUNCTION__), __LINE__); + bKPressed = TRUE; bShowIcon = !bShowIcon; HandlingTrayIcon(); @@ -363,7 +362,8 @@ LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) if (KEY_C == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed && !bKEYV) // 'C' key { - diag_log("Pressed LCTRL + LWIN + C"); + LOG_TO_FILE(L"%s(%d): Pressed LCTRL + LWIN + C", TEXT(__FUNCTION__), __LINE__); + bKPressed = TRUE; hFgWnd = GetForegroundWindow(); if (IsWindowApprooved(hFgWnd)) MoveWindowToMonitorCenter(hFgWnd, bWorkArea, FALSE); @@ -373,12 +373,14 @@ LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) if (KEY_V == pkhs->vkCode && bLCTRL && bLWIN && !bKPressed && !bKEYV) // 'V' key { - diag_log("Pressed LCTRL + LWIN + V"); + LOG_TO_FILE(L"%s(%d): Pressed LCTRL + LWIN + V", TEXT(__FUNCTION__), __LINE__); + bKPressed = TRUE; bKEYV = TRUE; hFgWnd = GetForegroundWindow(); if (IsWindowApprooved(hFgWnd)) { - diag_log("Opening 'Manual editing' dialog"); + LOG_TO_FILE(L"%s(%d): Opening the 'Manual editing' dialog", TEXT(__FUNCTION__), __LINE__); + DialogBoxW(hInst, MAKEINTRESOURCE(IDD_MANUAL_EDITING), hFgWnd, (DLGPROC)DlgProc); SetForegroundWindow(hFgWnd); } @@ -395,105 +397,141 @@ BOOL CALLBACK DlgProc(HWND hDlg, UINT dlgmsg, WPARAM wParam, LPARAM lParam) int x, y, w, h; switch (dlgmsg) { - case WM_INITDIALOG: + case WM_INITDIALOG: + { + LOG_TO_FILE(L"%s(%d): Initializing the 'Manual editing' dialog", TEXT(__FUNCTION__), __LINE__); + + SetWindowTextW(hDlg, szTitle); + GetWindowTextW(hFgWnd, szWinTitle, _countof(szWinTitle)); + GetClassNameW(hFgWnd, szWinClass, _countof(szWinClass)); + GetWindowRect(hFgWnd, &rcFW); + x = rcFW.left; + y = rcFW.top; + w = rcFW.right - rcFW.left; + h = rcFW.bottom - rcFW.top; + SetDlgItemInt(hDlg, IDC_EDIT_X, x, TRUE); + SetDlgItemInt(hDlg, IDC_EDIT_Y, y, TRUE); + SetDlgItemInt(hDlg, IDC_EDIT_WIDTH, w, FALSE); + SetDlgItemInt(hDlg, IDC_EDIT_HEIGHT, h, FALSE); + SetDlgItemTextW(hDlg, IDC_EDIT_TITLE, szWinTitle); + SetDlgItemTextW(hDlg, IDC_EDIT_CLASS, szWinClass); + UpdateWindow(hDlg); + break; + } + + case WM_COMMAND: + switch (LOWORD(wParam)) { - diag_log("Initializing 'Manual editing' dialog"); - SetWindowTextW(hDlg, szTitle); - GetWindowTextW(hFgWnd, szWinTitle, _countof(szWinTitle)); - GetClassNameW(hFgWnd, szWinClass, _countof(szWinClass)); - GetWindowRect(hFgWnd, &rcFW); - x = rcFW.left; - y = rcFW.top; - w = rcFW.right - rcFW.left; - h = rcFW.bottom - rcFW.top; - SetDlgItemInt(hDlg, IDC_EDIT_X, x, TRUE); - SetDlgItemInt(hDlg, IDC_EDIT_Y, y, TRUE); - SetDlgItemInt(hDlg, IDC_EDIT_WIDTH, w, FALSE); - SetDlgItemInt(hDlg, IDC_EDIT_HEIGHT, h, FALSE); - SetDlgItemTextW(hDlg, IDC_EDIT_TITLE, szWinTitle); - SetDlgItemTextW(hDlg, IDC_EDIT_CLASS, szWinClass); - UpdateWindow(hDlg); + case IDC_BUTTON_SET: + { + LOG_TO_FILE(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); + + LOG_TO_FILE(L"%s(%d): Window with handle 0x%08X was moved to %d, %d", TEXT(__FUNCTION__), __LINE__, hFgWnd, x, y); + + return TRUE; break; } + case IDCANCEL: + case IDC_BUTTON_CLOSE: + { + LOG_TO_FILE(L"%s(%d): Closing the 'Manual editing' dialog", TEXT(__FUNCTION__), __LINE__); - case WM_COMMAND: - switch (LOWORD(wParam)) - { - case IDC_BUTTON_SET: - { - diag_log("Pressed 'Set' button"); - 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); - diag_log("Window with handle 0x", hFgWnd, " was moved to x = ", x, ", y = ", y); - return TRUE; - break; - } - case IDCANCEL: - case IDC_BUTTON_CLOSE: - { - diag_log("Closing 'Manual editing' dialog"); - EndDialog(hDlg, LOWORD(wParam)); - break; - } - } + EndDialog(hDlg, LOWORD(wParam)); + break; + } + } } return FALSE; } -bool IsWindowApprooved(HWND hFW) +BOOL IsWindowApprooved(HWND hFW) { - diag_log("Entering IsWindowApprooved(), handle = 0x", hFW); + LOG_TO_FILE(L"Entering the %s() function, handle = 0x%08X", TEXT(__FUNCTION__), hFW); + bool bApprooved = FALSE; if (hFW) { - GetClassNameW(hFW, szWinClass, _countof(szWinClass)); - if (GetWindowTextW(hFW, (LPWSTR)szBuffer, BUF_LEN - sizeof(WCHAR))) diag_log("Title: '", ConvertWideToUtf8((LPWSTR)szBuffer), "'"); - if (IsIconic(hFW)) diag_log("Window is iconic"); - if (IsZoomed(hFW)) diag_log("Window is maximized"); - if ((wcscmp(szWinClass, szWinCore) != 0) && - (wcscmp(szWinClass, szWorkerW) != 0) && - (hFW != hDesktop && hFW != hTaskBar && hFW != hProgman)) + if (GetWindowTextW(hFW, (LPWSTR)szBuffer, BUF_LEN - sizeof(WCHAR))) + { + LOG_TO_FILE(L"%s(%d): Window title: '%s'", TEXT(__FUNCTION__), __LINE__, (LPWSTR)szBuffer); + } + + if (IsIconic(hFW)) + { + LOG_TO_FILE(L"%s(%d): The window is iconified", TEXT(__FUNCTION__), __LINE__); + } + + if (IsZoomed(hFW)) + { + LOG_TO_FILE(L"%s(%d): The window is maximized", TEXT(__FUNCTION__), __LINE__); + } + + LONG_PTR wlp = GetWindowLongPtrW(hFW, GWL_STYLE); + if (wlp & WS_CAPTION) { if (!IsIconic(hFW) && !IsZoomed(hFW)) { - diag_log("Window is approved"); + LOG_TO_FILE(L"%s(%d): The window is approved!", TEXT(__FUNCTION__), __LINE__); + bApprooved = TRUE; } else ShowError(IDS_ERR_MAXMIN); } - else diag_log("The window belongs to the Windows environment"); + else + { + LOG_TO_FILE(L"%s(%d): The window has no caption!", TEXT(__FUNCTION__), __LINE__); + } } - if (!bApprooved) diag_log("Window is not approved!"); - diag_log("Quiting IsWindowApprooved()"); + + if (!bApprooved) + { + LOG_TO_FILE(L"%s(%d): The window is not approved!", TEXT(__FUNCTION__), __LINE__); + } + + LOG_TO_FILE(L"Exit from the %s() function", TEXT(__FUNCTION__)); + return bApprooved; } VOID HandlingTrayIcon() { - diag_log("Entering HandlingTrayIcon(), bShowIcon = ", bShowIcon); + LOG_TO_FILE(L"Entering the %s() function, bShowIcon = %s", TEXT(__FUNCTION__), bShowIcon ? L"True" : L"False"); + if (bShowIcon) { bool bResult1 = Shell_NotifyIconW(NIM_ADD, &nid); - diag_log("Shell_NotifyIconW(NIM_ADD): ", bResult1); + LOG_TO_FILE(L"%s(%d): Shell_NotifyIconW(NIM_ADD): %s", TEXT(__FUNCTION__), __LINE__, bResult1 ? L"True" : L"False"); + bool bResult2 = Shell_NotifyIconW(NIM_SETVERSION, &nid); - diag_log("Shell_NotifyIconW(NIM_SETVERSION): ", bResult2); + LOG_TO_FILE(L"%s(%d): Shell_NotifyIconW(NIM_SETVERSION): %s", TEXT(__FUNCTION__), __LINE__, bResult2 ? L"True" : L"False"); + if (!bResult1 || !bResult2) { - diag_log("Error creating trayicon!"); + LOG_TO_FILE(L"%s(%d): Error creating trayicon!", TEXT(__FUNCTION__), __LINE__); + ShowError(IDS_ERR_ICON); + Shell_NotifyIconW(NIM_DELETE, &nid); bShowIcon = FALSE; } + else + { + Shell_NotifyIconW(NIM_MODIFY, &nid); + } } else { Shell_NotifyIconW(NIM_DELETE, &nid); } - diag_log("Quiting HandlingTrayIcon()"); + + LOG_TO_FILE(L"Exit from the %s() function", TEXT(__FUNCTION__)); } VOID ShowError(UINT uID) @@ -512,46 +550,61 @@ INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) switch (message) { - case WM_INITDIALOG: + case WM_INITDIALOG: + { + LOG_TO_FILE(L"%s(%d): Initializing the 'About' dialog", TEXT(__FUNCTION__), __LINE__); + + WCHAR szAboutProgName[MAX_LOADSTRING]; + WCHAR szAboutCopyright[MAX_LOADSTRING]; + WCHAR szAboutBuildTime[MAX_LOADSTRING]; + WCHAR szAboutHelp[MAX_LOADSTRING * 12]; + 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); + LoadStringW(hInst, IDS_ABOUT, szAboutHelp, _countof(szAboutHelp)); + SetDlgItemTextW(hDlg, IDC_ABOUT_PROGNAME, szAboutProgName); + SetDlgItemTextW(hDlg, IDC_ABOUT_COPYRIGHT, szAboutCopyright); + SetDlgItemTextW(hDlg, IDC_ABOUT_BUILDTIME, szAboutBuildTime); + SetDlgItemTextW(hDlg, IDC_ABOUTEDIT, szAboutHelp); +#ifdef NO_DONATION + HWND hLink = GetDlgItem(hDlg, IDC_DONATIONLINK); + if (hLink) DestroyWindow(hLink); +#endif // !NO_DONATION + + LOG_TO_FILE(L"%s(%d): End of initializing the 'About' dialog", TEXT(__FUNCTION__), __LINE__); + + return (INT_PTR)TRUE; + break; + } + + case WM_NOTIFY: + { + LPNMHDR pNMHdr = (LPNMHDR)lParam; + if ((NM_CLICK == pNMHdr->code || NM_RETURN == pNMHdr->code) && IDC_DONATIONLINK == pNMHdr->idFrom) { - diag_log("Initializing 'About' dialog"); - SetDlgItemTextW(hDlg, IDC_ABOUTHELP, szAbout); + PNMLINK pNMLink = (PNMLINK)pNMHdr; + LITEM item = pNMLink->item; + ShellExecuteW(NULL, L"open", item.szUrl, NULL, NULL, SW_SHOW); + + LOG_TO_FILE(L"%s(%d): Pressed the donation link! :-)", TEXT(__FUNCTION__), __LINE__); + return (INT_PTR)TRUE; - break; } + break; + } - case WM_NOTIFY: + case WM_COMMAND: + { + if (IDOK == LOWORD(wParam) || IDCANCEL == LOWORD(wParam)) { - 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); - diag_log("Pressed donation link"); - return (INT_PTR)TRUE; - } - break; - } + EndDialog(hDlg, LOWORD(wParam)); - case WM_COMMAND: - { - if (IDOK == LOWORD(wParam) || IDCANCEL == LOWORD(wParam)) - { - EndDialog(hDlg, LOWORD(wParam)); - diag_log("Closing 'About' dialog"); - return (INT_PTR)TRUE; - } - break; + LOG_TO_FILE(L"%s(%d): Closing the 'About' dialog", TEXT(__FUNCTION__), __LINE__); + + return (INT_PTR)TRUE; } + break; + } } return (INT_PTR)FALSE; } - -std::string ConvertWideToUtf8(const std::wstring& wstr) -{ - int count = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), wstr.length(), NULL, 0, NULL, NULL); - std::string str(count, 0); - WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, &str[0], count, NULL, NULL); - return str; -} diff --git a/wCenterWindow/wCenterWindow.rc b/wCenterWindow/wCenterWindow.rc index 303d390..d2ebf43 100644 Binary files a/wCenterWindow/wCenterWindow.rc and b/wCenterWindow/wCenterWindow.rc differ diff --git a/wCenterWindow/wCenterWindow.vcxproj b/wCenterWindow/wCenterWindow.vcxproj index b1e9ac9..266f294 100644 --- a/wCenterWindow/wCenterWindow.vcxproj +++ b/wCenterWindow/wCenterWindow.vcxproj @@ -73,16 +73,25 @@ true + false false false + false + false + false + false true false + false + false + false + false @@ -103,7 +112,7 @@ - "$(SolutionDir)Update_version.bat" + "$(SolutionDir)pre-build.cmd" "$(SolutionDir)" "$(ProjectDir)" @@ -113,8 +122,8 @@ true true WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) - true - MultiThreaded + false + MultiThreadedDLL stdcpp17 @@ -128,7 +137,7 @@ %(AdditionalManifestFiles) - "$(SolutionDir)Update_version.bat" + powershell -ExecutionPolicy RemoteSigned -File "$(SolutionDir)Update_Version.ps1" && move /y "$(SolutionDir)VersionInfo.h" "$(ProjectDir)" @@ -148,7 +157,7 @@ %(AdditionalManifestFiles) - "$(SolutionDir)Update_version.bat" + powershell -File "$(SolutionDir)Update_Version.ps1" move /y "$(SolutionDir)VersionInfo.h" "$(ProjectDir)" @@ -160,31 +169,30 @@ NDEBUG;_WINDOWS;%(PreprocessorDefinitions) true stdcpp17 - MultiThreaded + MultiThreadedDLL Windows true true - true + false comctl32.lib;%(AdditionalDependencies) %(AdditionalManifestFiles) - "$(SolutionDir)Update_version.bat" + powershell -File "$(SolutionDir)Update_Version.ps1" move /y "$(SolutionDir)VersionInfo.h" "$(ProjectDir)" - + - diff --git a/wCenterWindow/wCenterWindow.vcxproj.filters b/wCenterWindow/wCenterWindow.vcxproj.filters index 8b73153..47aa13c 100644 --- a/wCenterWindow/wCenterWindow.vcxproj.filters +++ b/wCenterWindow/wCenterWindow.vcxproj.filters @@ -27,7 +27,7 @@ Header Files - + Header Files @@ -35,9 +35,6 @@ Source Files - - Source Files -