「C Sharpの基礎 - タスクトレイ」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「== 概要 == タスクトレイとは、タスクバーの通知領域のことである。<br> <code>NotifyIcon</code>クラスを使用してタスクトレイ常駐ソ…」) |
|||
(同じ利用者による、間の1版が非表示) | |||
12行目: | 12行目: | ||
==== 常駐ソフトウェアの基本 ==== | ==== 常駐ソフトウェアの基本 ==== | ||
以下の例では、ソフトウェアをタスクトレイに常駐させている。<br> | 以下の例では、ソフトウェアをタスクトレイに常駐させている。<br> | ||
<br> | |||
フォームのプロパティから、[ShowInTaskbar]を[False]に変更することにより、タスクバーに非表示にすることもできる。<br> | |||
同様に、[Icon]でアイコンを設定することもできる。<br> | |||
<br> | |||
また、フォームのプロパティからソフトウェアを最小化起動を設定する場合、[WindowState]を[Minimized]に変更する。<br> | |||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
// Program.cs | // Program.cs | ||
68行目: | 73行目: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br> | <br> | ||
==== コンテキストメニュー ==== | ==== コンテキストメニュー ==== | ||
以下の例では、常駐ソフトウェアにコンテキストメニューを追加している。<br> | 以下の例では、常駐ソフトウェアにコンテキストメニューを追加している。<br> | ||
256行目: | 262行目: | ||
} | } | ||
} | } | ||
} | |||
</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> |
2021年7月21日 (水) 21:44時点における最新版
概要
タスクトレイとは、タスクバーの通知領域のことである。
NotifyIcon
クラスを使用してタスクトレイ常駐ソフトウェアを開発することができる。
以下の特徴を持つ常駐ソフトウェアを開発する。
- フォームを表示しない。
- タスクバーに表示しない。
- タスクトレイに常駐させる。
常駐ソフトウェア
常駐ソフトウェアの基本
以下の例では、ソフトウェアをタスクトレイに常駐させている。
フォームのプロパティから、[ShowInTaskbar]を[False]に変更することにより、タスクバーに非表示にすることもできる。
同様に、[Icon]でアイコンを設定することもできる。
また、フォームのプロパティからソフトウェアを最小化起動を設定する場合、[WindowState]を[Minimized]に変更する。
// 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秒間表示
}
}
}
常駐ソフトウェアの例
以下の例では、起動時に画面を最小化して、タスクトレイにソフトウェアを表示している。
また、メニューバーにおいて、起動時の画面の表示 / 非表示を選択する設定を追加することが好ましい。
// 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());
}
}
// 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;
}
}
}