WINUI 3.0 - Reunion 0.5 window size///
Asked Answered
M

6

12

I just start learning WinUI 3.0 and can't find any information in google or books like Learn WinUI 3.0 how to set default window size of application. I know in UWP it can be like

ApplicationView.PreferredLaunchViewSize = new Size(480, 800);
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;

But actually it doesn't work in WinUI

Milkmaid answered 19/4, 2021 at 21:44 Comment(2)
Have you tested this method: github.com/microsoft/microsoft-ui-xaml/issues/2564Discordant
Here is some info on how to maximize and minimize the Window: https://mcmap.net/q/910856/-how-to-maximize-minimize-restore-winui-window-from-codeAddiction
L
12

No need to do these interop calls on your own or use third-party packages for this.

Try this trifecta:

// Use 'this' rather than 'window' as variable if this is about the current window.
IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
var windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);
var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);

Then you can finally set the size with:

appWindow.Resize(new Windows.Graphics.SizeInt32 { Width = 480, Height = 800 });

Note that an AppWindow object has several other functions as well, like MoveAndResize, Show, Hide, and features to modify the title bar.

Luehrmann answered 19/1, 2022 at 22:24 Comment(0)
I
8

Take a look at this repository dotMorten/WinUIEx.

It contains a method to set the window size and position

myWindow.SetWindowPositionAndSize(100, 100, 1024, 768);

I also found an example in the WinUI3 Samples I'm adding the relevant code here for easy reference

private void SetWindowSize(IntPtr hwnd, int width, int height)
{
    var dpi = PInvoke.User32.GetDpiForWindow(hwnd);
    float scalingFactor = (float)dpi / 96;
    width = (int)(width * scalingFactor);
    height = (int)(height * scalingFactor);

    PInvoke.User32.SetWindowPos(hwnd, PInvoke.User32.SpecialWindowHandles.HWND_TOP,
                                0, 0, width, height,
                                PInvoke.User32.SetWindowPosFlags.SWP_NOMOVE);
}
Ietta answered 29/9, 2021 at 10:17 Comment(2)
To get hWnd: var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(m_window);Shellfish
The second example also requires the PInvoke.User32 nuget package to be installed.Supination
P
4

I can't comment on answers yet but to add to Jonas' answer, you can put his answer in the class constructor (where this.InitializeComponent() is) of your main window code-behind or in the OnLaunched method of App.cs. I'm sure that seems obvious to a lot of people but to those coming from other languages/platforms it might not be. Example:

    public MainWindow()
    {
        this.InitializeComponent();
        
        IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this); // m_window in App.cs
        WindowId windowId = Win32Interop.GetWindowIdFromWindow(hWnd);
        AppWindow appWindow = AppWindow.GetFromWindowId(windowId);

        var size = new Windows.Graphics.SizeInt32();
        size.Width = 480;
        size.Height = 800;

        appWindow.Resize(size);
    // or like Jonas said:
    // appWindow.Resize(new Windows.Graphics.SizeInt32 { Width = 480, Height = 800 });
    }
Planar answered 11/2, 2022 at 1:44 Comment(0)
D
2

In the most recent versions of WinUI, Window now has the AppWindow property. This means the solution offered by @Jonas can be simplified:

   protected override void OnLaunched(LaunchActivatedEventArgs args)
   {
        MainWindow = new Window();
        MainWindow.AppWindow.Resize(new Windows.Graphics.SizeInt32 { Width = 480, Height = 800 });

        ...
   

   }
Delicacy answered 2/12, 2023 at 18:13 Comment(0)
M
1

As of WinUI3 1.4, this.AppWindow.Resize(new(800, 400)); in the Window's constructor, after InitizeComponent(), has the same effect

with the "new" being a Windows.Graphics.SizeInt32 object, WidthxHeight

Momentarily answered 15/9, 2023 at 16:0 Comment(0)
M
0

This works. You will need to add the nuget package PInvoke.User32.

protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
    m_window = new MainWindow();
    m_window.Activate();

    //Get the Window's HWND
    var windowNative = m_window.As<IWindowNative>();
    m_windowHandle = windowNative.WindowHandle;

    m_window.Activate();

    SetWindowBounds(0,0,100,100);
}

private Window m_window;
private IntPtr m_windowHandle;
public IntPtr WindowHandle { get { return m_windowHandle; } }

[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("EECDBF0E-BAE9-4CB6-A68E-9598E1CB57BB")]
internal interface IWindowNative
{
    IntPtr WindowHandle { get; }
}


public void SetWindowBounds(int x, int y, int width, int height)
{
    var dpi = PInvoke.User32.GetDpiForWindow(m_windowHandle);
    float scalingFactor = (float)dpi / 96;
    width = (int)(width * scalingFactor);
    height = (int)(height * scalingFactor);

    PInvoke.User32.SetWindowPos(m_windowHandle, PInvoke.User32.SpecialWindowHandles.HWND_TOP,
                                x, y, width, height,
                                PInvoke.User32.SetWindowPosFlags.SWP_NOMOVE);
}
Macri answered 13/12, 2021 at 21:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.