diff --git a/tools/HandlesExperiment/FileLocksmithGUI/MainWindow.xaml.cpp b/tools/HandlesExperiment/FileLocksmithGUI/MainWindow.xaml.cpp index e908b6225b..1e0ba2784f 100644 --- a/tools/HandlesExperiment/FileLocksmithGUI/MainWindow.xaml.cpp +++ b/tools/HandlesExperiment/FileLocksmithGUI/MainWindow.xaml.cpp @@ -7,7 +7,10 @@ #include "../FileLocksmithLib/IPC.h" #include "../FileLocksmithLib/FileLocksmith.h" +#pragma comment(lib, "shcore") // GetDpiForMonitor + using namespace winrt; +using namespace Microsoft::UI; using namespace Microsoft::UI::Xaml; // To learn more about WinUI, the WinUI project structure, @@ -17,6 +20,7 @@ namespace winrt::FileLocksmithGUI::implementation { MainWindow::MainWindow() { + place_and_resize(); InitializeComponent(); find_processes(); } @@ -34,4 +38,47 @@ namespace winrt::FileLocksmithGUI::implementation stackPanel().Children().Append(entry); } } + + void MainWindow::place_and_resize() + { + // Get native handle + auto windowNative{ this->try_as<::IWindowNative>() }; + winrt::check_bool(windowNative); + HWND hwnd{ 0 }; + windowNative->get_WindowHandle(&hwnd); + + // Get mouse cursor position + POINT cursorPosition{0, 0}; + GetCursorPos(&cursorPosition); + ::Windows::Graphics::PointInt32 point{ cursorPosition.x, cursorPosition.y }; + + // Get monitor area for mouse position + auto display_area = Windowing::DisplayArea::GetFromPoint(point, Windowing::DisplayAreaFallback::Nearest); + HMONITOR monitor = MonitorFromPoint(cursorPosition, MONITOR_DEFAULTTOPRIMARY); + MONITORINFOEXW monitor_info; + monitor_info.cbSize = sizeof(MONITORINFOEX); + GetMonitorInfoW(monitor, &monitor_info); + UINT dpi_x, dpi_y; + GetDpiForMonitor(monitor, MONITOR_DPI_TYPE::MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y); + UINT window_dpi = GetDpiForWindow(hwnd); + + int width = 720; + int height = 405; + + winrt::Windows::Graphics::RectInt32 rect; + + // Scale window size + rect.Width = (int32_t)(width * (float)window_dpi / dpi_x); + rect.Height = (int32_t)(height * (float)window_dpi / dpi_y); + + // Center to screen + rect.X = display_area.WorkArea().X + display_area.WorkArea().Width / 2 - width / 2; + rect.Y = display_area.WorkArea().Y + display_area.WorkArea().Height / 2 - height / 2; + + // Get app window + auto window_id = GetWindowIdFromWindow(hwnd); + auto app_window = Windowing::AppWindow::GetFromWindowId(window_id); + + app_window.MoveAndResize(rect); + } } diff --git a/tools/HandlesExperiment/FileLocksmithGUI/MainWindow.xaml.h b/tools/HandlesExperiment/FileLocksmithGUI/MainWindow.xaml.h index 90a4273715..a4329f4d16 100644 --- a/tools/HandlesExperiment/FileLocksmithGUI/MainWindow.xaml.h +++ b/tools/HandlesExperiment/FileLocksmithGUI/MainWindow.xaml.h @@ -12,6 +12,7 @@ namespace winrt::FileLocksmithGUI::implementation private: std::vector m_process_info; void find_processes(); + void place_and_resize(); }; } diff --git a/tools/HandlesExperiment/FileLocksmithGUI/pch.h b/tools/HandlesExperiment/FileLocksmithGUI/pch.h index 2f32fca8c6..60c8a485c3 100644 --- a/tools/HandlesExperiment/FileLocksmithGUI/pch.h +++ b/tools/HandlesExperiment/FileLocksmithGUI/pch.h @@ -3,6 +3,7 @@ #include #include #include +#include // Undefine GetCurrentTime macro to prevent // conflict with Storyboard::GetCurrentTime @@ -12,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -22,4 +24,6 @@ #include #include #include -#include \ No newline at end of file +#include +#include +#include