WinUI3修改默认窗口大小
基于PInvoke.User32.GetDpiForWindow(hwnd)
,可以适应不同的DPI
首先需要通过NuGet添加包:PInvoke.User32
然后在App.xaml.cs
中定义函数:
1 | private void SetWindowSize(IntPtr hwnd, int width, int height) |
然后在App.xaml.cs
的OnLaunched
下使用:
1 | IntPtr hwnd = WinRT.Interop.WindowNative.GetWindowHandle(window); |
基于appWindow.Resize
,无法适应不同的DPI
只需要在App.xaml.cs
的OnLaunched
下使用:
1 | // Use 'this' rather than 'window' as variable if this is about the current window. |
然后即可对窗口默认大小进行设定:
1 | appWindow.Resize(new Windows.Graphics.SizeInt32 { Width = 480, Height = 800 }); |
AppWindow
对象还有其他几个功能,如MoveAndResize
、Show
、Hide
和修改标题栏的功能。
最后吐槽一下Microsoft Doc是个什么垃圾玩意。
参考资料
1、c# - WINUI 3.0 - Reunion 0.5 window size/// - Stack Overflow