WinUI 3でウィンドウのサイズをリサイズできないダイアログスタイルのウィンドウを作成する手順を紹介します。
概要
WinUI 3アプリケーションのアプリケーションのデフォルトの設定では、ウィンドウサイズがリサイズできる状態になります。
アプリケーションによっては、ウィンドウサイズがリサイズできないダイアログスタイルでのウィンドウにしたい場合があります。
この記事では WinUI 3アプリケーションでウィンドウサイズがリサイズできないダイアログスタイルのアプリケーションを実装する手順を紹介します。
実装方法
ダイアログスタイルのウィンドウに変更するには、AppWindowオブジェクトを取得し SetPresenterメソッドの第一引数に
Microsoft.UI.Windowing.AppWindowPresenterKind.CompactOverlay
を与えて呼び出します。
プログラム
WinUI 3アプリケーションを作成します。
コード
<Window
x:Class="WindowStyleSet.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WindowStyleSet"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
</StackPanel>
</Window>
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace WindowStyleSet
{
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
IntPtr hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
Microsoft.UI.WindowId windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hwnd);
Microsoft.UI.Windowing.AppWindow appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);
appWindow.SetPresenter(Microsoft.UI.Windowing.AppWindowPresenterKind.CompactOverlay);
appWindow.Resize(new Windows.Graphics.SizeInt32(320, 200));
}
private void myButton_Click(object sender, RoutedEventArgs e)
{
myButton.Content = "Clicked";
}
}
}
解説
メインウィンドウのクラスのコンストラクタにコードを追記します。
以下のコードで、ウィンドウハンドルからウィンドウIDを取得して、AppWindowオブジェクトを取得します。
IntPtr hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
Microsoft.UI.WindowId windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hwnd);
Microsoft.UI.Windowing.AppWindow appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);
AppWindowオブジェクトのSetPresenterメソッドを呼び出します。引数に
Microsoft.UI.Windowing.AppWindowPresenterKind.CompactOverlay
を与えてダイアログスタイルのウィンドウに設定します。
appWindow.SetPresenter(Microsoft.UI.Windowing.AppWindowPresenterKind.CompactOverlay);
AppWindowオブジェクトのResizeメソッドを呼び出し、ウィンドウのサイズを設定します。詳しくは
こちらの記事を参照してください。
appWindow.Resize(new Windows.Graphics.SizeInt32(320, 200));
実行結果
プロジェクトを実行します。下図のウィンドウが表示されます。
タイトルバーに最大化ボタン、最小化ボタンが表示されていないことが確認できます。
また、マウスポインタをウィンドウ協会のいちいちに移動してもポインタ形状は変化せず、
ウィンドウのリサイズもできない動作が確認できます。
WinUI 3 アプリケーションでダイアログスタイルのウィンドウを作成できました。
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2022-08-11
作成日: 2022-08-11