![](/img/trans.png)
[英]What are my options for selecting/opening files and folders when using WinUI3/Project Reunion 0.5?
[英]WINUI 3.0 - Reunion 0.5 window size///
我剛開始學習WinUI 3.0 ,在谷歌或Learn WinUI 3.0 how to set default window size of application 之類的書籍中找不到任何信息。 我知道在 UWP 它可以像
ApplicationView.PreferredLaunchViewSize = new Size(480, 800);
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
但實際上它在 WinUI 中不起作用
我還不能對答案發表評論,但要添加到 Jonas 的答案中,您可以將他的答案放在主要 window 代碼隱藏的 class 構造函數(this.InitializeComponent() 所在的位置)或 App.cs 的 OnLaunched 方法中. 我敢肯定這對很多人來說似乎是顯而易見的,但對於那些來自其他語言/平台的人來說可能並非如此。 例子:
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 });
}
看看這個存儲庫dotMorten/WinUIEx 。
它包含設置 window 大小和 position 的方法
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);
}
無需您自己進行這些互操作調用或為此使用第三方包。
試試這個三連擊:
// 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
object 還具有其他幾個功能,例如MoveAndResize
、 Show
、 Hide
以及修改標題欄的功能。
這行得通。 您需要添加 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.