C# WinUI3 動態調整顯示模式(全螢幕、緊湊、重疊、預設)

首先先建立一個 method 用來取得 AppWindow ,方便未來復用:
    
using Microsoft.UI;
using WinRT.Interop;

private AppWindow GetAppWindowForCurrentWindow() 
{ 
    IntPtr hWnd = WindowNative.GetWindowHandle(this);
    WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd); 
    return AppWindow.GetFromWindowId(wndId);
}
    

然後就可以設定顯示模式,這裡示範以全螢幕顯示:
    
AppWindow appWindow = GetAppWindowForCurrentWindow();
appWindow.SetPresenter(AppWindowPresenterKind.FullScreen);
    

AppWindowPresenterKind 列舉細項:
  • Default: 預設
  • CompactOverlay: 緊湊
  • FullScreen: 全螢幕
  • Overlapped: 重疊


參考資料:
Microsoft.Learn - Windowing functionality migration#Full-screen
Microsoft.Learn - AppWindowPresenterKind Enum
Github Discussions - How can I set App title in winui3 desktop app
stack overflow - How To Set Default Window Mode to Full Screen in WinUI3?

留言