📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

325行目: 325行目:
     {
     {
       Console.WriteLine("Window closed");
       Console.WriteLine("Window closed");
    }
}
</syntaxhighlight>
<br><br>
== [最大化]ボタン / [最小化]ボタンの無効化 ==
[閉じる]ボタンのみ有効化する場合は、<code>SystemDecorations</code>プロパティの設定を変更して、カスタムタイトルバーを実装する必要がある。<br>
<br>
具体的には、以下に示す設定を行う。<br>
<br>
まず、<code>SystemDecorations</code>プロパティに<code>SystemDecorations.Full</code>を指定して、標準のウィンドウ装飾を維持する。<br>
これにより、[閉じる]ボタンが有効になる。<br>
<br>
次に、<code>ExtendClientAreaTitleBarHeightHint</code>プロパティにカスタムタイトルバーの高さ (例: 30ピクセル) を指定する。<br>
<br>
カスタムタイトルバーを生成して、ウインドウのタイトルを表示する。<br>
<br>
メインコンテンツ領域を生成して、DockPanelを使用してレイアウトを構成する。<br>
<br>
カスタムタイトルバーとメインコンテンツ領域を組み合わせて、ウインドウの全体的なレイアウトを設定する。<br>
<br>
これにより、[閉じる]ボタンは標準の位置に表示されて機能する。([最大化]ボタン、[最小化]ボタンは非表示)<br>
ただし、[最大化]ボタン、[最小化]ボタンの機能は完全に無効になっているわけではないため、プログラムで制御する必要がある。<br>
例えば、<code>CanResize</code>プロパティを<code>false</code>に指定、<code>WindowState</code>の変更をハンドリングする等を実装する。<br>
<br>
以下の例では、ウインドウの[最大化]ボタンおよび[最小化]ボタンを非表示にして、[閉じる]ボタンのみ表示している。<br>
<br>
<syntaxhighlight lang="c#">
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using System;
public class MainWindow : Window
{
    public MainWindow()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      AvaloniaXamlLoader.Load(this);
      try
      {
          // ウインドウのリサイズを無効化
          // 最大化ボタンも無効化される
          this.CanResize = false;
          // 最小化の無効化
          this.PropertyChanged += (sender, e) =>
          {
            if (e.Property == WindowStateProperty)
            {
                if ((WindowState)e.NewValue == WindowState.Minimized)
                {
                  this.WindowState = WindowState.Normal;
                }
            }
          };
          // カスタムタイトルバーの実装
          this.ExtendClientAreaToDecorationsHint = true;
          this.ExtendClientAreaTitleBarHeightHint = 30;    // タイトルバーの高さを30ピクセルに設定
          this.SystemDecorations = SystemDecorations.Full;  // フルシステム装飾を維持
          // カスタムタイトルバーの生成
          var titleBar = new Grid
          {
            Height = 30,
            Background = new SolidColorBrush(Colors.LightGray)
          };
          var titleText = new TextBlock
          {
            Text = this.Title,
            VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center,
            Margin = new Thickness(10, 0, 0, 0)
          };
          titleBar.Children.Add(titleText);
          // メインコンテンツ領域の生成
          // この例では、TextBlockを使用する
          var mainContent = new TextBlock
          {
            Text                = "アプリケーションのメインコンテンツ",
            VerticalAlignment  = Avalonia.Layout.VerticalAlignment.Center,
            HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center
          };
          // レイアウトの設定
          var mainLayout = new DockPanel();
          DockPanel.SetDock(titleBar, Dock.Top);
          mainLayout.Children.Add(titleBar);
          mainLayout.Children.Add(mainContent);
          this.Content = mainLayout;
          // ウインドウのサイズを固定して、中央に配置
          this.Width  = 800;
          this.Height = 600;
          this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
      }
      catch (Exception ex)
      {
          new Window().ShowDialog(new ContentDialog()
          {
            Title = "エラー",
            Content = $"ウインドウの初期化中にエラーが発生: {ex.Message}",
            PrimaryButtonText = "OK"
          });
      }
     }
     }
  }
  }