---
# System prepended metadata

title: WinUI 3 ウインドウサイズを設定する 2022-07-29
tags: [WinUI 3, Resize, ウインドウサイズ]

---

---
lang: ja-jp
breaks: true
---
# WinUI 3 ウインドウサイズを設定する 2022-07-29

## MainWindow.xaml

```xml=
<Window
	x:Class="App25.MainWindow"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:local="using:App25"
	xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
	mc:Ignorable="d"
	xmlns:viewModels="clr-namespace:App25.viewModels"
	>

    <Grid
        Loading="Grid_Loading"
        >
        <StackPanel 
        Orientation="Vertical" 
        HorizontalAlignment="Center" 
        VerticalAlignment="Center"
        >
・・・
        </StackPanel>
    </Grid>
    
</Window>
```

## MainWindow.xaml.cs

```csharp=
namespace App25;

/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
    private AppWindow m_appWindow;

    public MainWindow()
    {
        this.InitializeComponent();

        m_appWindow = GetAppWindowForCurrentWindow(this);
    }
    
    private AppWindow GetAppWindowForCurrentWindow(object target)
    {
        IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(target);
        WindowId myWndId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);
        return AppWindow.GetFromWindowId(myWndId);
    }
    
    private void Grid_Loading(FrameworkElement sender, object args)
    {
        if (m_appWindow != null)
        {
            //m_appWindow.SetPresenter(AppWindowPresenterKind.Overlapped);
            m_appWindow.Resize(new SizeInt32(800, 600));
        }
    }    
}
```



###### tags: `WinUI 3` `ウインドウサイズ` `Resize`
