「C Sharpの基礎 - タスクトレイ」の版間の差分

ナビゲーションに移動 検索に移動
(ページの作成:「== 概要 == タスクトレイとは、タスクバーの通知領域のことである。<br> <code>NotifyIcon</code>クラスを使用してタスクトレイ常駐ソ…」)
 
256行目: 256行目:
       }
       }
     }
     }
}
</syntaxhighlight>
<br>
==== 常駐ソフトウェアの例 ====
以下の例では、起動時に画面を最小化して、タスクトレイにソフトウェアを表示している。<br>
また、メニューバーにおいて、起動時の画面の表示 / 非表示を選択する設定を追加することが好ましい。<br>
<syntaxhighlight lang="c#">
// Program.cs
static class Program
{
    /// <summary>
    ///  The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
      Application.SetHighDpiMode(HighDpiMode.SystemAware);
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      // フォームを非表示にしてソフトウェアを実行する
      Application.Run(new Form1());
    }
}
</syntaxhighlight>
<br>
<syntaxhighlight lang="c#">
// Form1.cs
public partial class Form1 : Form
{
    private NotifyIcon notifyIcon = null;
    private ContextMenuStrip Menu;
    private ToolStripMenuItem MenuItemShow;
    private ToolStripMenuItem MenuItemClose;
    public Form1()
    {
      InitializeComponent();
      // タスクバーに非表示
      this.ShowInTaskbar = false;
      this.SetComponents();
    }
    private void SetComponents()
    {
      notifyIcon = new NotifyIcon();
      notifyIcon.Icon = new Icon(@"TaskTray.ico");  // アイコンの設定
      notifyIcon.Visible = true;                    // アイコンの表示
      notifyIcon.Text = "常駐ソフトウェア";        // アイコンにマウスポインタを合わせた時のポップアップ
      // コンテキストメニュー
      Menu = new ContextMenuStrip();
      MenuItemShow = new ToolStripMenuItem();
      MenuItemShow.Text = "&表示";
      MenuItemShow.Click += ((object sender, EventArgs e) =>
                            {
                                // フォームの表示・非表示を切り替える
                                if (this.Visible == false)
                                {
                                  MenuItemShow.Text = "&非表示";
                                }
                                else
                                {
                                  MenuItemShow.Text = "&表示";
                                }
                                this.Visible = !this.Visible;
                                if (this.WindowState != 0)
                                {
                                  this.WindowState = System.Windows.Forms.FormWindowState.Normal;
                                }
                            });
      Menu.Items.Add(MenuItemShow);
      MenuItemClose = new ToolStripMenuItem();
      MenuItemClose.Text = "&終了";
      MenuItemClose.Click += ((object sender, EventArgs e) =>
                              {
                                // アプリケーションの終了
                                Application.Exit();
                              });
      Menu.Items.Add(MenuItemClose);
      notifyIcon.ContextMenuStrip = Menu;
      // NotifyIconのダブルクリックイベント
      notifyIcon.DoubleClick += ((object sender, EventArgs e) =>
                                {
                                    // フォームの表示・非表示を切り替える
                                    if (this.Visible == false)
                                    {
                                      MenuItemShow.Text = "&非表示";
                                    }
                                    else
                                    {
                                      MenuItemShow.Text = "&表示";
                                    }
                                    this.Visible = !this.Visible;
                                    if (this.WindowState != 0)
                                    {
                                      this.WindowState = System.Windows.Forms.FormWindowState.Normal;
                                    }
                                });
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        this.Visible = false;
        this.WindowState = FormWindowState.Minimized;
    }
    private void MainMenuItemCloseClick(object sender, EventArgs e)
    {
        // アプリケーションの終了
        Application.Exit();
    }
    private void MainMenuItemShowClick(object sender, EventArgs e)
    {
        // フォームの表示・非表示を切り替える
        if (this.Visible == false)
        {
          MenuItemShow.Text = "&非表示";
        }
        else
        {
          MenuItemShow.Text = "&表示";
        }
        this.Visible = !this.Visible;
        if (this.WindowState != 0)
        {
          this.WindowState = System.Windows.Forms.FormWindowState.Normal;
        }
    }
  }
  }
  </syntaxhighlight>
  </syntaxhighlight>

案内メニュー