C Sharpの基礎 - タスクトレイ
ナビゲーションに移動
検索に移動
概要
タスクトレイとは、タスクバーの通知領域のことである。
NotifyIcon
クラスを使用してタスクトレイ常駐ソフトウェアを開発することができる。
以下の特徴を持つ常駐ソフトウェアを開発する。
- フォームを表示しない。
- タスクバーに表示しない。
- タスクトレイに常駐させる。
常駐ソフトウェア
常駐ソフトウェアの基本
以下の例では、ソフトウェアをタスクトレイに常駐させている。
// Program.cs
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Application.Run(new Form1()); // フォームを非表示にする
// フォームを非表示にしてソフトウェアを実行する
new Form1();
Application.Run();
}
}
}
// Form1.cs
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テスト"; // アイコンにマウスポインタを合わせた時のテキスト
}
}
}
コンテキストメニュー
以下の例では、常駐ソフトウェアにコンテキストメニューを追加している。
常駐ソウフトウェアのアイコンを右クリックして[終了]を選択する時、ソフトウェアが終了する。
// Form1.cs
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テスト";
// コンテキストメニュー
var Menu = new ContextMenuStrip();
var MenuItem = new ToolStripMenuItem();
MenuItem.Text = "&終了";
MenuItem.Click += MenuItemClick;
Menu.Items.Add(MenuItem);
notifyIcon.ContextMenuStrip = Menu;
}
private void MenuItemClick(object sender, EventArgs e)
{
// アプリケーションの終了
Application.Exit();
}
}
}
クリックイベント
以下の例では、常駐ソフトウェアのアイコンを選択する時、フォームの表示・非表示を切り替えている。
// Program.cs
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); // フォームを表示して実行する
}
}
}
// Form1.cs
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テスト";
var Menu = new ContextMenuStrip();
var MenuItem = new ToolStripMenuItem();
MenuItem.Text = "&終了";
MenuItem.Click += MenuItemClick;
MenuStrip.Items.Add(MenuItem);
notifyIcon.ContextMenuStrip = Menu;
// NotifyIconのクリックイベント
notifyIcon.Click += NotifyIconClick;
}
private void MenuItemClick(object sender, EventArgs e)
{
Application.Exit();
}
private void NotifyIconClick(object sender, EventArgs e)
{
// フォームの表示・非表示を切り替える
this.Visible = !this.Visible;
}
}
}
バルーンヒント
バルーンヒントとは、タスクトレイのアイコンから表示されるポップアップメッセージである。
以下の例では、NotifyIconにバルーンヒントを追加している。
フォームにあるボタンを押下する時、バルーンヒントが5秒間表示している。
// Form1.cs
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 += ButtonClick;
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テスト";
var Menu = new ContextMenuStrip();
var MenuItem = new ToolStripMenuItem();
MenuItem.Text = "&終了";
MenuItem.Click += MenuItemClick;
Menu.Items.Add(MenuItem);
notifyIcon.ContextMenuStrip = Menu;
notifyIcon.Click += NotifyIconClick;
}
private void MenuItemClick(object sender, EventArgs e)
{
Application.Exit();
}
private void NotifyIconClick(object sender, EventArgs e)
{
this.Visible = !this.Visible;
}
private void ButtonClick(object sender, EventArgs e)
{
// バルーンヒントを表示する
notifyIcon.BalloonTipTitle = "お知らせタイトル";
notifyIcon.BalloonTipText = "お知らせメッセージ";
notifyIcon.ShowBalloonTip(5000); // 5秒間表示
}
}
}