Application.Run(new Form1());
を次のように変更
new Form1(); Application.Run();
notifyIcon1 をフォームに追加して、notifyIcon1_DoubleClick を以下のように定義
private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
{
if ( this.Visible == false )
{
this.Visible = true;
this.Activate();
}
}
Closing イベントをキャンセルして、Visible = false してやればいい
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true; // 終了処理をキャンセル
if(this.Visible == true)
{
this.Visible = false;
}
}
フォームコンストラクタかForm1_Loadに以下のように追加
SystemEvents.SessionEnding += new SessionEndingEventHandler(SystemEvents_SessionEnding);
で、次のように処理を定義。
private void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
{
// 処理を追加
}
さらに、終了時にはイベントを開放しておかないとだめ。ということで Form1_Closed に
SystemEvents.SessionEnding -= new SessionEndingEventHandler(SystemEvents_SessionEnding);
とか追加しておく。
タスクトレイから起動するようにした場合、Form1_LoadやForm1_Closingといっ たイベントハンドラにイベントが関連付けられない。試しに Form1_Load に MessageBox.Show("hoge") 等書いて実行してみれば、Form1_Load が実行され ていないことがわかる。ちなみに、notifyIcon をダブルクリックして初めて Form1_Load が実行されると思う。