简体   繁体   English

WINUI 3.0 - Reunion 0.5 window 大小///

[英]WINUI 3.0 - Reunion 0.5 window size///

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.我刚开始学习WinUI 3.0 ,在谷歌或Learn WinUI 3.0 how to set default window size of application 之类的书籍中找不到任何信息。 I know in UWP it can be like我知道在 UWP 它可以像

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

But actually it doesn't work in WinUI但实际上它在 WinUI 中不起作用

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.我还不能对答案发表评论,但要添加到 Jonas 的答案中,您可以将他的答案放在主要 window 代码隐藏的 class 构造函数(this.InitializeComponent() 所在的位置)或 App.cs 的 OnLaunched 方法中. 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 });
    }

Take a look at this repository dotMorten/WinUIEx .看看这个存储库dotMorten/WinUIEx

It contains a method to set the window size and position它包含设置 window 大小和 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我还在WinUI3 Samples中找到了一个示例,我在此处添加相关代码以方便参考

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

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.请注意, AppWindow object 还具有其他几个功能,例如MoveAndResizeShowHide以及修改标题栏的功能。

This works.这行得通。 You will need to add the nuget package PInvoke.User32.您需要添加 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM