【问题标题】:WINUI 3.0 - Reunion 0.5 window size///WINUI 3.0 - Reunion 0.5 窗口大小///
【发布时间】:2021-07-14 02:32:19
【问题描述】:

我刚开始学习WinUI 3.0,在谷歌或Learn WinUI 3.0之类的书籍中找不到任何信息如何设置应用程序的默认窗口大小。我知道在 UWP 中可能是这样的

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

但实际上它在 WinUI 中不起作用

【问题讨论】:

标签: c# winui winui-3


【解决方案1】:

看看这个存储库dotMorten/WinUIEx

它包含一个设置窗口大小和位置的方法

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

我还在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);
}

【讨论】:

  • 获取 hWnd:var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(m_window);
【解决方案2】:

无需您自己进行这些互操作调用,也无需为此使用第三方包。

试试这个三连击:

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

然后你终于可以设置大小了:

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

请注意,AppWindow 对象还有其他几个功能,例如 MoveAndResizeShowHide,以及修改标题栏的功能。

【讨论】:

    【解决方案3】:

    这行得通。您将需要添加 nuget 包 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);
    }
    

    【讨论】:

      【解决方案4】:

      我还不能评论答案,但要添加到 Jonas 的答案中,您可以将他的答案放在主窗口代码隐藏的类构造函数(this.InitializeComponent() 所在的位置)或 OnLaunched 方法中应用程序.cs。我敢肯定这对很多人来说似乎很明显,但对于那些来自其他语言/平台的人来说可能不是。示例:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-27
        • 1970-01-01
        • 2022-12-15
        • 1970-01-01
        • 2011-07-14
        • 2014-06-22
        相关资源
        最近更新 更多