トレイアイコン(NotifyIcon)

タスクトレイにアイコンを表示するサンプル。NotifyIconクラスを使う。同じディレクトリにtest.icoを置いておく。

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WinApp {
  public class TestForm : Form {
    NotifyIcon notifyicon = new NotifyIcon();
    public TestForm() {
      ClientSize = new Size(200, 200);
      Text = "TestForm";
      notifyicon.Icon = new System.Drawing.Icon("test.ico");
      notifyicon.Visible = true;
    }

    [STAThread]
    static void Main() {
      Application.Run(new TestForm());
    }
  }
}

タスクトレイのアイコンをクリックしたりしたい場合は、TNotifyIconのイベントハンドラを書けばいいだけのようだ。また、NotifyIconの親コンポーネントを指定しなきゃいけないかと思っていたのだが、しなくて良いみたい。登録されたイベントハンドラがたまたまTestFormのメソッドだった、みたいなイメージなのかな。

次は、ホットキーで非表示にしたいところだな。