📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
| (同じ利用者による、間の5版が非表示) | |||
| 32行目: | 32行目: | ||
<br> | <br> | ||
<center> | <center> | ||
{| class="wikitable" style="background-color:#fefefe;" | {| class="wikitable" | style="background-color:#fefefe;" | ||
|+ Windowクラスの主要なプロパティ | |+ Windowクラスの主要なプロパティ | ||
|- | |- | ||
| 70行目: | 70行目: | ||
|} | |} | ||
</center> | </center> | ||
<br><br> | |||
== Windowクラスの主要なメソッド == | |||
Avalonia UIにおいて、Windowクラスで使用可能な主要なメソッドを以下に示す。<br> | |||
<br> | |||
以下に示すメソッドを使用することにより、ウインドウの動作をカスタマイズして、ユーザインターフェースの制御を細かく行うことができる。<br> | |||
また、多くのメソッドはオーバーライド可能であり、独自の処理を追加することができる。<br> | |||
<br> | |||
* Show() | |||
*: ウインドウを表示する。 | |||
*: 例: this.Show(); | |||
*: <br> | |||
* Hide() | |||
*: ウインドウを非表示にする。 | |||
*: 例: this.Hide(); | |||
*: <br> | |||
* Close() | |||
*: ウイィンドウを閉じる。 | |||
*: 例: this.Close(); | |||
*: <br> | |||
* Activate() | |||
*: ウインドウをアクティブにして、前面に持ってくる。 | |||
*: 例: this.Activate(); | |||
*: <br> | |||
* ShowDialog<T>(Window owner) | |||
*: モーダルダイアログとしてウインドウを表示する。 | |||
*: 例: var result = await this.ShowDialog<bool>(owner); | |||
*: <br> | |||
* InitializeComponent() | |||
*: XAMLで定義されたコンポーネントを初期化する。 | |||
*: 例: this.InitializeComponent(); | |||
*: <br> | |||
* OnOpened(EventArgs e) | |||
*: ウインドウが開く時に呼び出される。 | |||
*: オーバーライド可能である。 | |||
*: <br> | |||
*: 例: | |||
*: <syntaxhighlight lang="c#"> | |||
protected override void OnOpened(EventArgs e) | |||
{ | |||
base.OnOpened(e); | |||
// カスタム処理 | |||
} | |||
</syntaxhighlight> | |||
*: <br> | |||
* OnClosed(EventArgs e) | |||
*: ウインドウが閉じられた時に呼び出される。 | |||
*: オーバーライド可能である。 | |||
*: 例: 上記のOnOpenedメソッドと同様である。 | |||
*: <br> | |||
* OnClosing(CancelEventArgs e) | |||
*: ウインドウが閉じられる直前に呼び出される。 | |||
*: キャンセル可能である。 | |||
*: <br> | |||
*: 例: | |||
*: <syntaxhighlight lang="c#"> | |||
protected override void OnClosing(CancelEventArgs e) | |||
{ | |||
if (UnsavedChanges) | |||
{ | |||
var result = MessageBox.Show("保存されていない変更があります。閉じますか?", "確認", MessageBoxButton.YesNo); | |||
e.Cancel = (result == MessageBoxResult.No); | |||
} | |||
base.OnClosing(e); | |||
} | |||
</syntaxhighlight> | |||
*: <br> | |||
* EnsureInitialized() | |||
*: ウインドウが初期化されていることを確認する。 | |||
*: 例: this.EnsureInitialized(); | |||
*: <br> | |||
* LayoutUpdated() | |||
*: レイアウトが更新されたときに呼び出される。 | |||
*: 例: this.LayoutUpdated += OnLayoutUpdated; | |||
*: <br> | |||
* GetPlatformHandle() | |||
*: プラットフォーム固有のウインドウハンドルを取得する。 | |||
*: 例: var handle = this.GetPlatformHandle(); | |||
*: <br> | |||
* SetSystemDecorations(SystemDecorations decorations) | |||
*: システムデコレーション (タイトルバー、ボーダー等) を動的に設定する。 | |||
*: 例: this.SetSystemDecorations(SystemDecorations.Full); | |||
*: <br> | |||
* BeginMoveDrag() | |||
*: ウインドウの移動操作を開始する。 | |||
*: 例: this.BeginMoveDrag(); | |||
*: <br> | |||
* BeginResizeDrag(WindowEdge edge) | |||
*: ウインドウのリサイズ操作を開始する。 | |||
*: 例: this.BeginResizeDrag(WindowEdge.SouthEast); | |||
*: <br> | |||
* SetPosition(PixelPoint point) | |||
*: ウインドウの位置を設定する。 | |||
*: 例: this.SetPosition(new PixelPoint(100, 100)); | |||
*: <br> | |||
* Maximize()、Minimize()、Restore() | |||
*: それぞれウインドウの最大化、最小化、元のサイズへの復元を行う。 | |||
*: 例: this.Maximize(); | |||
*: <br> | |||
* GetRenderer() | |||
*: ウインドウのレンダラーを取得する。 | |||
*: 例: var renderer = this.GetRenderer(); | |||
<br><br> | <br><br> | ||
| 197行目: | 299行目: | ||
== ウインドウイベントを処理する例 == | == ウインドウイベントを処理する例 == | ||
以下の例では、ウインドウの主要なイベントである[開く]ボタンおよび[閉じる]ボタンを押下した時のイベント処理を示している。<br> | |||
<br> | |||
* Openedイベント (ウインドウが開いた時) | |||
*: Openedイベントは、ウインドウが画面に表示された直後に発生する。 | |||
*: OnWindowOpenedメソッドで処理される。 | |||
*: <br> | |||
*: 例: ウインドウを開いた直後に初期化処理を行う場合に使用する。(データの読み込み、UIコンポーネントの設定、外部リソースとの接続等) | |||
*: <br> | |||
* Closingイベント (ウインドウが閉じる直前) | |||
*: Closingイベントは、ウインドウを閉じる直前 (例: [閉じる]ボタンを押下した直後) に発生する。 | |||
*: OnWindowClosingメソッドで処理される。 | |||
*: <br> | |||
*: 例: ユーザが誤ってウインドウを閉じることを防ぐ、または、保存されていない変更がある場合に警告を表示する等のロジックに適している。 | |||
*: <br> | |||
* Closedイベント (ウインドウが閉じられた後) | |||
*: ウインドウが完全に閉じられた後に発生する。 | |||
*: OnWindowClosedメソッドで処理される。 | |||
*: <br> | |||
*: 例: リソースの解放、ログの記録、他のウインドウへの通知等、ウインドウが閉じられた後のクリーンアップ処理に適している。 | |||
<br> | |||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
using Avalonia.Controls; | |||
using Avalonia.Interactivity; | |||
using MsBox.Avalonia; | |||
using MsBox.Avalonia.Enums; | |||
public class EventHandlingWindow : Window | public class EventHandlingWindow : Window | ||
{ | { | ||
| 214行目: | 341行目: | ||
} | } | ||
private void OnWindowClosing(object sender, WindowClosingEventArgs e) | private async void OnWindowClosing(object sender, WindowClosingEventArgs e) | ||
{ | { | ||
var | // 一時的にキャンセルして、ダイアログの結果を待つ | ||
e.Cancel = true; | |||
// ウインドウの閉じる操作を一時的に中断する場合、ユーザに終了確認ダイアログを表示する | |||
var messageBox = MessageBoxManager.GetMessageBoxStandardWindow("Confirm", | |||
"Are you sure you want to close?", | |||
ButtonEnum.YesNo); | |||
var result = await messageBox.Show(); | |||
if (result == ButtonResult.Yes) | |||
{ // [Yes]ボタンを押下した場合、ウインドウを閉じる | |||
e.Cancel = false; | |||
} | |||
} | } | ||
| 228行目: | 367行目: | ||
<br><br> | <br><br> | ||
== [最大化]ボタンの無効化 == | |||
まず、<code>CanResize</code>プロパティを<code>true</code>に指定することにより、ウインドウのリサイズを許可する。<br> | |||
これにより、[最小化]ボタンも有効になる。<br> | |||
<br> | |||
次に、<code>WindowState</code>プロパティの変更をハンドリングして、ウインドウが最大化される場合はすぐに通常の状態に戻すことにより、ウインドウの最大化を防ぐ。<br> | |||
<br> | |||
タイトルバーのダブルクリックによる最大化を防ぐために、<code>PointerPressed</code>イベントをハンドリングして、<br> | |||
ウインドウの初期サイズを設定して、画面中央に表示するように設定する。<br> | |||
<br> | |||
<u>※注意</u><br> | |||
<u>[最大化]ボタンは視覚的には表示されるが機能しない。</u><br> | |||
<u>ウインドウの最大サイズを制限する場合は、<code>MaxWidth</code>および<code>MaxHeight</code>プロパティを指定することができる。</u><br> | |||
<br> | |||
以下の例では、ウインドウの[最大化]ボタンのみを非表示にして、[最小化]ボタンおよび[閉じる]ボタンのみ表示している。<br> | |||
<br> | |||
<syntaxhighlight lang="c#"> | |||
using Avalonia; | |||
using Avalonia.Controls; | |||
using Avalonia.Interactivity; | |||
using Avalonia.Markup.Xaml; | |||
using System; | |||
public class MainWindow : Window | |||
{ | |||
public MainWindow() | |||
{ | |||
InitializeComponent(); | |||
} | |||
private void InitializeComponent() | |||
{ | |||
AvaloniaXamlLoader.Load(this); | |||
try | |||
{ | |||
// ウインドウの最大化ボタンを無効化 | |||
this.CanResize = true; // リサイズを有効化 | |||
this.WindowState = WindowState.Normal; // 初期状態を通常に設定 | |||
// WindowStateが変更された時のイベントハンドラを追加 | |||
this.PropertyChanged += (sender, e) => | |||
{ | |||
if (e.Property == WindowStateProperty) | |||
{ | |||
// 最大化する場合、通常の状態に戻す | |||
if ((WindowState)e.NewValue == WindowState.Maximized) | |||
{ | |||
this.WindowState = WindowState.Normal; | |||
} | |||
} | |||
}; | |||
// ダブルクリックでの最大化を防ぐ | |||
this.PointerPressed += (sender, e) => | |||
{ | |||
if (e.ClickCount == 2) | |||
{ | |||
e.Handled = true; // イベントを処理済みとしてマーク | |||
} | |||
}; | |||
// 初期サイズとウインドウの位置を設定 | |||
this.Width = 800; | |||
this.Height = 600; | |||
this.WindowStartupLocation = WindowStartupLocation.CenterScreen; | |||
// サンプルコンテンツの追加 | |||
var content = new StackPanel(); | |||
content.Children.Add(new TextBlock { Text = "最大化ボタンが無効化されたウインドウ" }); | |||
content.Children.Add(new Button { Content = "テストボタン" }); | |||
this.Content = content; | |||
} | |||
catch (Exception ex) | |||
{ | |||
// エラーが発生した場合、メッセージボックスでエラーを表示 | |||
new Window().ShowDialog(new ContentDialog() | |||
{ | |||
Title = "エラー", | |||
Content = $"ウインドウの初期化中にエラーが発生: {ex.Message}", | |||
PrimaryButtonText = "OK" | |||
}); | |||
} | |||
} | |||
} | |||
</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" | |||
}); | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
<br><br> | |||
{{#seo: | |||
|title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki | |||
|keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,電気回路,電子回路,基板,プリント基板 | |||
|description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This page is {{PAGENAME}} in our wiki about electronic circuits and SUSE Linux | |||
|image=/resources/assets/MochiuLogo_Single_Blue.png | |||
}} | |||
__FORCETOC__ | __FORCETOC__ | ||
[[カテゴリ:C_Sharp]] | [[カテゴリ:C_Sharp]] | ||