C#のタスクトレイ常駐アプリの作り方のご紹介!

- SE
- タスクトレイの常駐アプリについて詳しく教えてください。
- PM
- まずタスクとレイとは何かからみていきましょう。
C#のタスクトレイ常駐アプリの作り方
今回は、C#のタスクトレイ常駐アプリの作り方について説明します。タスクトレイとは、タスクバーの通知領域のことです。
NotifyIconクラスを使ってタスクトレイ常駐アプリを作ることができます。C#のタスクトレイ常駐アプリに興味のある方はぜひご覧ください。
基本的な作り方
C#で以下のアプリケーションを作成してみます。
・Formを表示しない
・タスクバーに表示しない
・タスクトレイに常駐する
Visual Studioで作成したFormアプリケーションの場合、そのままではFormが表示されてしまいますので、Program.csも修正します。
実際のソースコードを見てみましょう。アイコンファイルは事前に準備しておいてください。
Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; using System.Windows.Forms; namespace WindowsFormsApp1 { static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //Application.Run(new Form1()); // Formが表示されてしまうのでコメントアウト // Formを表示しないで実行する new Form1(); Application.Run(); } } } |
Form1.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { NotifyIcon notifyIcon; public Form1() { // タスクバーに表示しない this.ShowInTaskbar = false; this.setComponents(); } private void setComponents() { notifyIcon = new NotifyIcon(); // アイコンの設定 notifyIcon.Icon = new Icon(@"C:\test\favicon.ico"); // アイコンを表示する notifyIcon.Visible = true; // アイコンにマウスポインタを合わせたときのテキスト notifyIcon.Text = "NotifyIconテスト"; } } } |
実行すると、タスクトレイにアイコンが表示されることが分かります。
コンテキストメニュー
C#では、タスクトレイ常駐アプリにコンテキストメニューを追加できます。
実際のソースコードを見てみましょう。
Form1.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { NotifyIcon notifyIcon; public Form1() { this.ShowInTaskbar = false; this.setComponents(); } private void setComponents() { notifyIcon = new NotifyIcon(); notifyIcon.Icon = new Icon(@"C:\test\favicon.ico"); notifyIcon.Visible = true; notifyIcon.Text = "NotifyIconテスト"; // コンテキストメニュー ContextMenuStrip contextMenuStrip = new ContextMenuStrip(); ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem(); toolStripMenuItem.Text = "&終了"; toolStripMenuItem.Click += ToolStripMenuItem_Click; contextMenuStrip.Items.Add(toolStripMenuItem); notifyIcon.ContextMenuStrip = contextMenuStrip; } private void ToolStripMenuItem_Click(object sender, EventArgs e) { // アプリケーションの終了 Application.Exit(); } } } |
アイコンを右クリックして「終了」を選択すると、アプリケーションが終了します。
このように、C#ではタスクトレイ常駐アプリにコンテキストメニューを追加できます。
クリックイベント
C#では、NotifyIconのクリックイベントを扱えます。今回はFormを表示し、アイコンをクリックしてFormの表示/非表示を切り替えてみます。
実際のソースコードを見てみましょう。
Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
uusing System; using System.Windows.Forms; namespace WindowsFormsApp1 { static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); // Formを表示しないで実行 //new Form1(); //Application.Run(); } } } |
Form1.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { NotifyIcon notifyIcon; public Form1() { this.ShowInTaskbar = false; this.setComponents(); } private void setComponents() { notifyIcon = new NotifyIcon(); notifyIcon.Icon = new Icon(@"C:\test\favicon.ico"); notifyIcon.Visible = true; notifyIcon.Text = "NotifyIconテスト"; ContextMenuStrip contextMenuStrip = new ContextMenuStrip(); ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem(); toolStripMenuItem.Text = "&終了"; toolStripMenuItem.Click += ToolStripMenuItem_Click; contextMenuStrip.Items.Add(toolStripMenuItem); notifyIcon.ContextMenuStrip = contextMenuStrip; // NotifyIconのクリックイベント notifyIcon.Click += NotifyIcon_Click; } private void ToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } private void NotifyIcon_Click(object sender, EventArgs e) { // Formの表示/非表示を反転 this.Visible = !this.Visible; } } } |
アイコンをクリックするとFormの表示/非表示が切り替わることが分かります。
このように、C#ではNotifyIconのクリックイベントを扱えます。
バルーンヒント
C#では、NotifyIconにバルーンヒントを追加できます。バルーンヒントとは、タスクトレイのアイコンから表示されるポップアップメッセージです。
実際のソースコードを見てみましょう。
Form1.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { NotifyIcon notifyIcon; Button button; public Form1() { button = new Button(); button.Location = new Point(10, 10); button.Text = "button"; button.Click += Button_Click; this.Controls.Add(button); this.ShowInTaskbar = false; this.setComponents(); } private void setComponents() { notifyIcon = new NotifyIcon(); notifyIcon.Icon = new Icon(@"C:\test\favicon.ico"); notifyIcon.Visible = true; notifyIcon.Text = "NotifyIconテスト"; ContextMenuStrip contextMenuStrip = new ContextMenuStrip(); ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem(); toolStripMenuItem.Text = "&終了"; toolStripMenuItem.Click += ToolStripMenuItem_Click; contextMenuStrip.Items.Add(toolStripMenuItem); notifyIcon.ContextMenuStrip = contextMenuStrip; notifyIcon.Click += NotifyIcon_Click; } private void ToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } private void NotifyIcon_Click(object sender, EventArgs e) { this.Visible = !this.Visible; } private void Button_Click(object sender, EventArgs e) { // バルーンヒントを表示する notifyIcon.BalloonTipTitle = "お知らせタイトル"; notifyIcon.BalloonTipText = "お知らせメッセージ"; notifyIcon.ShowBalloonTip(5000); } } } |
Formのボタンをクリックすると、バルーンヒントが5秒間表示されることが分かります。
このように、C#ではNotifyIconにバルーンヒントを追加できます。
- SE
- バルーンヒントも追加できるのですね。とても興味が湧いてきました。
- PM
- そうですね。見る側としても便利な機能です。是非マスターしましょう。
まとめ
いかがでしたでしょうか。C#のタスクトレイ常駐アプリの作り方について説明しました。
タスクトレイ常駐アプリには、コンテキストメニューやバルーンヒントを追加できます。ぜひご自身でソースコードを書いて、理解を深めていきましょう。