コンテンツにスキップ

ウィンドウの外観カスタマイズ (モダンな .NET の WinForms 新機能)

Page ID: 1151923649321

最終更新日:

.NET

ウィンドウの外観カスタマイズ
(モダンな .NET の WinForms 新機能)

Section titled “ウィンドウの外観カスタマイズ(モダンな .NET の WinForms 新機能)”
.NET9.0 ~

画面タイトルの文字色を標準で変更可能に

Section titled “画面タイトルの文字色を標準で変更可能に”

いままでは、WinForms アプリ画面の「キャプション(ウィンドウタイトル文字)の文字色」を弄(いじ)るには、Win 32 API を使って自身の ウィンドウハンドル (HWND) から Window ID を取得して、 Windows App SDK (Microsoft.UI.Windowing.AppWindow) を使ってデコる必要があり、かなりの手間でした。

モダンな WinForms では、Form クラスに FormCaptionTextColor プロパティが新たに追加され、とても簡単に画面の「キャプション(ウィンドウタイトル文字)の文字色」を変更することができるようになっています。

this.FormCaptionTextColor = Color.Gold;
.NET9.0 ~

画面タイトルバーの背景色を標準で変更可能に

Section titled “画面タイトルバーの背景色を標準で変更可能に”

いままでは、WinForms アプリ画面の「タイトルバーの背景色」を弄(いじ)るには、Win 32 API を使って自身の ウィンドウハンドル (HWND) から Window ID を取得して、 Windows App SDK (Microsoft.UI.Windowing.AppWindow) を使ってデコる必要があり、かなりの手間でした。

モダンな WinForms では、Form クラスに FormCaptionBackColor プロパティが新たに追加され、とても簡単に画面の「タイトルバーの背景色」を変更することができるようになっています。

this.FormCaptionBackColor = Color.SaddleBrown;
.NET9.0 ~

ウィンドウの角の丸みをある程度は標準で変更可能に

Section titled “ウィンドウの角の丸みをある程度は標準で変更可能に”

モダンな WinForms では、Form クラスに FormCornerPreference プロパティが新たに追加され、ウィンドウの角の丸み具合を3段階で調整できるようになりました。

this.FormCornerPreference = FormCornerPreference.Default;
this.FormCornerPreference = FormCornerPreference.DoNotRound;
this.FormCornerPreference = FormCornerPreference.Round;
this.FormCornerPreference = FormCornerPreference.RoundSmall;

FormCornerPreference 列挙型 フィールド一覧

Section titled “FormCornerPreference 列挙型 フィールド一覧”

デフォルト: FormCornerPreference.Default = 0

Section titled “デフォルト: FormCornerPreference.Default = 0”

デフォルト。 ※Windows 11 では、Round と同等。

this.FormCornerPreference = FormCornerPreference.Default;

角丸なし: FormCornerPreference.DoNotRound = 1

Section titled “角丸なし: FormCornerPreference.DoNotRound = 1”

角の丸みなし。

this.FormCornerPreference = FormCornerPreference.DoNotRound;

角丸あり: FormCornerPreference.Round = 2

Section titled “角丸あり: FormCornerPreference.Round = 2”

角の丸みあり。

this.FormCornerPreference = FormCornerPreference.Round;

小径の角丸あり: FormCornerPreference.RoundSmall = 3

Section titled “小径の角丸あり: FormCornerPreference.RoundSmall = 3”

角の丸みがわずかにあり。

this.FormCornerPreference = FormCornerPreference.RoundSmall;
.NET9.0 ~

画面の境界線色を変えて目立たせたりすることが標準で可能に

Section titled “画面の境界線色を変えて目立たせたりすることが標準で可能に”

モダンな WinForms では、Form クラスに FormBorderColor プロパティが新たに追加され、画面の境界線色を容易に変更することができるようになりました。

this.FormBorderColor = Color.DodgerBlue;

PGコード例: 画面の境界線色を点滅させるように変えて、ウィンドウを目立たせてみる

Section titled “ PGコード例: 画面の境界線色を点滅させるように変えて、ウィンドウを目立たせてみる”
Form1.cs
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace WinFormsDarkTheme;
/// <summary>Form1 クラス</summary>
public partial class Form1 : Form
{
/// <summary>Form1 ビューモデル</summary>
private readonly Form1ViewModel _viewModel = new();
/// <inheritdoc />
public Form1()
{
InitializeComponent();
// ビューモデルをデータバインド
this.viewModelBindingSource.DataSource = _viewModel;
// 画面の境界線を点滅させる処理を、別スレッドにて非同期で実行
Task.Run(async () => await BlinkFormBorder());
}
/// <summary>画面の境界線を点滅させる非同期メソッド</summary>
private async Task BlinkFormBorder()
{
// ビューモデルからキャンセレーショントークンを取得
var token = _viewModel.Cts.Token;
try
{
bool toggle = false;
// キャンセレーションがかかるまでずっと、1秒ごとに画面の境界線の色を切り替える
while (true)
{
token.ThrowIfCancellationRequested();
await Task.Delay(1000, token);
await InvokeAsync(
() => this.FormBorderColor = toggle ? Color.Salmon : Color.Gold
);
toggle = !toggle;
}
}
catch (OperationCanceledException)
{
// キャンセレーションがかかったら、画面の境界線の色をデフォルトへ戻す
await InvokeAsync(
() => this.FormBorderColor = SystemColors.ActiveBorder
);
}
}
}
/// <summary>Form1 ビューモデル クラス</summary>
public partial class Form1ViewModel : ObservableObject
{
/// <summary>キャンセレーショントークンソース</summary>
public CancellationTokenSource Cts { get; } = new();
/// <summary>現在の数値</summary>
[ObservableProperty]
private int currentValue;
/// <summary>カウントアップコマンド</summary>
/// <remarks>現在の数値をインクリメントします。</remarks>
[RelayCommand]
private void Increment()
=> CurrentValue++;
/// <summary>停止コマンド</summary>
/// <remarks>画面の境界線の点滅を停止します。</remarks>
[RelayCommand]
private void Stop()
=> Cts.Cancel();
}
▼ 画面キャプチャ ( gif アニメーション )