Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

Windows Forms でフェードインするスプラッシュ画面を実装する

0
Posted at

はじめに

Windows Forms でフェードインするスプラッシュ画面を実装します。

改訂履歴

  • 2026/06/13 : 初版公開。

本文

1. 環境

  • Visual Studio Community 2022
  • .NET 8.0

2. ソース

SplashForm はスプラッシュ画面本体のフォームです。このクラスでフェードインのアニメーションを制御しています。

SplashForm.cs
namespace WindowsFormsSplashExampleNet8;

/// <summary>
/// スプラッシュ画面を表示するフォームを表します。
/// </summary>
public partial class SplashForm : Form
{
    private readonly TaskCompletionSource<bool>? _tcs = new();

    /// <summary>
    /// <see cref="SplashForm"/> の表示が完了したことを示すタスクを取得します。
    /// </summary>
    public Task CompletedTask => _tcs!.Task;

    /// <summary>
    /// <see cref="SplashForm"/> クラスの新しいインスタンスを初期化します。
    /// </summary>
    public SplashForm()
    {
        InitializeComponent();

        // スプラッシュ画面をフェードインで表示する。
        Opacity = 0;
        FadeInTimer.Interval = 50;
        FadeInTimer.Tick += async (s, e) =>
        {
            Opacity += 0.05;
            if (Opacity >= 1)
            {
                Opacity = 1;
                FadeInTimer.Stop();
                await Task.Delay(500);
                _tcs?.TrySetResult(true);
            }
        };

        // フェードインタイマーを開始する。
        FadeInTimer.Start();
    }
}

CustomApplicationContext では、スプラッシュ画面からメイン画面への遷移を制御しています。また設定ファイルの読込など、アプリケーションの初期化処理が必要な場合は InitializeApplicationAsync() に実装できます。

CustomApplicationContext.cs
namespace WindowsFormsSplashExampleNet8;

/// <summary>
/// アプリケーションスレッドに関するコンテキスト情報を指定します。
/// </summary>
internal class CustomApplicationContext : ApplicationContext
{
    /// <summary>
    /// <see cref="CustomApplicationContext"/> クラスの新しいインスタンスを初期化します。
    /// </summary>
    public CustomApplicationContext()
    {
        RunAsync();
    }

    /// <summary>
    /// 非同期でスプラッシュ画面の表示と初期化処理を実行します。
    /// </summary>
    private async void RunAsync()
    {
        // スプラッシュ画面を表示する。
        var splashForm = new SplashForm();
        splashForm.Show();

        // アプリケーションの初期化処理を実行する。
        var initializedTask = InitializeApplicationAsync();

        // 両処理の完了を待機する。
        await Task.WhenAll(splashForm.CompletedTask, initializedTask);

        // スプラッシュ画面を閉じる。
        splashForm.Close();

        // メイン画面を表示する。
        var mainForm = new MainForm();
        mainForm.FormClosed += (s, e) => ExitThread();
        mainForm.Show();
    }

    /// <summary>
    /// 初期化処理
    /// </summary>
    private static async Task InitializeApplicationAsync()
    {
        // ここでアプリケーションの初期化処理を実行する。
        await Task.Delay(1000);
    }
}

Application.Run() の引数に new CustomApplicationContext() を指定します。

Program.cs
namespace WindowsFormsSplashExampleNet8;

/// <summary>
/// アプリケーションのエントリーポイントを定義します。
/// </summary>
internal static class Program
{
    /// <summary>
    /// アプリケーションのメインエントリポイントです。
    /// </summary>
    [STAThread]
    internal static void Main()
    {
        ApplicationConfiguration.Initialize();
        Application.Run(new CustomApplicationContext());
    }
}

おわりに

地味ですが、少しだけ華やかになるかもですね。

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?