C#でオープニング画面

C#でオープニング画面のサンプルが見当たらなかったので作成してみた。
下記はFormOpenerというSystem.Windows.Forms.Formです。
表示時間を制御するためのタイマーコントロールを登録しておいてください。
timerMain_Tickはそのコールバックです。
デザインはお好みで作成します。

public partial class FormOpener : Form
{
  private static long DisplayTime = 50000000;
  private long startTime;
  private System.Windows.Forms.Form mainform;

  public FormOpener(System.Windows.Forms.Form mainform)
  {
    InitializeComponent();
    this.startTime = System.DateTime.Now.Ticks;
    this.mainform = mainform;
    this.mainform.Opacity = 0;
    this.Location = new Point(
      this.mainform.Location.X +
      (this.mainform.Width - this.Width) / 2,
      this.mainform.Location.Y +
      (this.mainform.Height - this.Height) / 2);
  }

  private void timerMain_Tick(object sender, EventArgs e)
  {
    long tm = System.DateTime.Now.Ticks - this.startTime;
    if (tm > DisplayTime)
    {
      this.Close();
      this.mainform.Opacity = 1.0;
    }
    else
    {
      this.Opacity = 
       (1.0 * (DisplayTime * 1.0 - tm) / DisplayTime) * 2;
    }
  }
}

使い方はスタートアップフォームあたりで以下の通り。

public partial class MainFrame : System.Windows.Forms.Form
{
  public MainFrame()
  {
    FormOpener opener = new FormOpener(this);
    opener.Show(this);
  }
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です