LoginSignup
1
1

More than 1 year has passed since last update.

【WinUI3】Windowのサイズを変更不可にする

Posted at

概要

WPFではWindowクラスのResizeModeプロパティNoResizeなどと指定することで、ウィンドウサイズを変更不可にすることができました。
WinUI3でも同様のことを行おうと思ったのですが、ResizeModeプロパティが存在しなかったので、少し調べました。

<Window x:Class="Hoge.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Hoge"
        mc:Ignorable="d"
        ResizeMode="NoResize"
        Title="Hoge Title"
        Height="200" Width="500">
        <Grid><Grid/>
</Window>

WinUI3でのやり方

現状のWinUI3(1.3系)ではXAMLではなく、コードビハインドで指定する必要があるようです。
Window(MainWindow)クラスのコンストラクタで下記のように記載すればよいです。

public MainWindow()
{
    this.InitializeComponent();

    var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
    var mainWindowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);
    var appWindow = AppWindow.GetFromWindowId(mainWindowId);

    // ウィンドウサイズを指定する
    appWindow.Resize(new(600, 300));

    // ウィンドウサイズを変更不能にする
    var presenter = appWindow.Presenter as OverlappedPresenter;
    presenter.IsResizable = false;
}

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1