ページの作成:「== 概要 == タスクトレイとは、タスクバーの通知領域のことである。<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> | ||