Set window size

This commit is contained in:
Ivan Stošić 2022-09-17 11:32:22 +02:00 committed by Ivan Stosic
parent defb4e2223
commit d985f8d987
3 changed files with 53 additions and 1 deletions

View File

@ -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);
}
}

View File

@ -12,6 +12,7 @@ namespace winrt::FileLocksmithGUI::implementation
private:
std::vector<ProcessResult> m_process_info;
void find_processes();
void place_and_resize();
};
}

View File

@ -3,6 +3,7 @@
#include <unknwn.h>
#include <restrictederrorinfo.h>
#include <hstring.h>
#include <ShellScalingApi.h>
// Undefine GetCurrentTime macro to prevent
// conflict with Storyboard::GetCurrentTime
@ -12,6 +13,7 @@
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.ApplicationModel.Activation.h>
#include <winrt/Microsoft.UI.Composition.h>
#include <winrt/Microsoft.UI.Interop.h>
#include <winrt/Microsoft.UI.Xaml.h>
#include <winrt/Microsoft.UI.Xaml.Controls.h>
#include <winrt/Microsoft.UI.Xaml.Controls.Primitives.h>
@ -22,4 +24,6 @@
#include <winrt/Microsoft.UI.Xaml.Navigation.h>
#include <winrt/Microsoft.UI.Xaml.Shapes.h>
#include <winrt/Microsoft.UI.Dispatching.h>
#include <wil/cppwinrt_helpers.h>
#include <winrt/Microsoft.UI.Windowing.h>
#include <wil/cppwinrt_helpers.h>
#include <microsoft.ui.xaml.window.h>