-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathMainWindow.xaml.cpp
89 lines (74 loc) · 3.16 KB
/
MainWindow.xaml.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "MainWindow.xaml.h"
#if __has_include("MainWindow.g.cpp")
#include "MainWindow.g.cpp"
#include "microsoft.ui.xaml.window.h"
#include "SampleConfiguration.h"
#endif
namespace winrt
{
using namespace Microsoft::UI::Xaml;
}
namespace winrt::DeploymentManagerSample::implementation
{
MainWindow::MainWindow()
{
InitializeComponent();
Title(winrt::DeploymentManagerSample::SampleConfig::FeatureName);
HWND hwnd = GetWindowHandle();
LoadIcon(hwnd, L"Assets/windows-sdk.ico");
SetWindowSize(hwnd, 1050, 800);
PlacementCenterWindowInMonitorWin32(hwnd);
}
HWND MainWindow::GetWindowHandle()
{
if (_hwnd == nullptr)
{
Window window = *this;
window.as<IWindowNative>()->get_WindowHandle(&_hwnd);
}
return _hwnd;
}
void MainWindow::LoadIcon(HWND hwnd, wchar_t const* iconPath)
{
HANDLE hSmallIcon = LoadImageW(nullptr, iconPath, IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
LR_LOADFROMFILE | LR_SHARED);
SendMessageW(hwnd, WM_SETICON, ICON_SMALL, reinterpret_cast<LPARAM>(hSmallIcon));
HANDLE hBigIcon = LoadImageW(nullptr, iconPath, IMAGE_ICON,
GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON),
LR_LOADFROMFILE | LR_SHARED);
SendMessageW(hwnd, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(hBigIcon));
}
void MainWindow::SetWindowSize(HWND hwnd, int width, int height)
{
// Win32 uses pixels and WinUI 3 uses effective pixels, so you should apply the DPI scale factor
const UINT dpi = GetDpiForWindow(hwnd);
const float scalingFactor = static_cast<float>(dpi) / 96;
const int widthScaled = static_cast<int>(width * scalingFactor);
const int heightScaled = static_cast<int>(height * scalingFactor);
SetWindowPos(hwnd, nullptr, 0, 0, widthScaled, heightScaled, SWP_NOMOVE | SWP_NOZORDER);
}
void MainWindow::PlacementCenterWindowInMonitorWin32(HWND hwnd)
{
RECT windowMontiorRectToAdjust;
GetWindowRect(hwnd, &windowMontiorRectToAdjust);
ClipOrCenterRectToMonitorWin32(windowMontiorRectToAdjust);
SetWindowPos(hwnd, nullptr, windowMontiorRectToAdjust.left,
windowMontiorRectToAdjust.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}
void MainWindow::ClipOrCenterRectToMonitorWin32(RECT& adjustedWindowRect)
{
MONITORINFO mi{ sizeof(mi) };
GetMonitorInfoW(MonitorFromRect(&adjustedWindowRect, MONITOR_DEFAULTTONEAREST), &mi);
const auto& rcWork = mi.rcWork;
const int w = adjustedWindowRect.right - adjustedWindowRect.left;
const int h = adjustedWindowRect.bottom - adjustedWindowRect.top;
adjustedWindowRect.left = rcWork.left + (rcWork.right - rcWork.left - w) / 2;
adjustedWindowRect.top = rcWork.top + (rcWork.bottom - rcWork.top - h) / 2;
adjustedWindowRect.right = adjustedWindowRect.left + w;
adjustedWindowRect.bottom = adjustedWindowRect.top + h;
}
}