MochiuWiki : SUSE, EC, PCB
案内
メインページ
最近の更新
おまかせ表示
MediaWiki についてのヘルプ
ツール
リンク元
関連ページの更新状況
特別ページ
ページ情報
We ask for
Donations
検索
個人用ツール
ログイン
Toggle dark mode
名前空間
ページ
議論
表示
閲覧
ソースを閲覧
履歴を表示
C Sharpの基礎 - タスクトレイのソースを表示
提供: MochiuWiki : SUSE, EC, PCB
←
C Sharpの基礎 - タスクトレイ
あなたには「このページの編集」を行う権限がありません。理由は以下の通りです:
この操作は、次のグループのいずれかに属する利用者のみが実行できます:
管理者
、new-group。
このページのソースの閲覧やコピーができます。
== 概要 == タスクトレイとは、タスクバーの通知領域のことである。<br> <code>NotifyIcon</code>クラスを使用してタスクトレイ常駐ソフトウェアを開発することができる。<br> <br> 以下の特徴を持つ常駐ソフトウェアを開発する。<br> * フォームを表示しない。 * タスクバーに表示しない。 * タスクトレイに常駐させる。 <br><br> == 常駐ソフトウェア == ==== 常駐ソフトウェアの基本 ==== 以下の例では、ソフトウェアをタスクトレイに常駐させている。<br> <syntaxhighlight lang="c#"> // 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(); } } } </syntaxhighlight> <br> <syntaxhighlight lang="c#"> // 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テスト"; // アイコンにマウスポインタを合わせた時のテキスト } } } </syntaxhighlight> <br> ==== コンテキストメニュー ==== 以下の例では、常駐ソフトウェアにコンテキストメニューを追加している。<br> <br> 常駐ソウフトウェアのアイコンを右クリックして[終了]を選択する時、ソフトウェアが終了する。<br> <syntaxhighlight lang="c#"> // 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(); } } } </syntaxhighlight> <br> ==== クリックイベント ==== 以下の例では、常駐ソフトウェアのアイコンを選択する時、フォームの表示・非表示を切り替えている。<br> <syntaxhighlight lang="c#"> // 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()); // フォームを表示して実行する } } } </syntaxhighlight> <br> <syntaxhighlight lang="c#"> // 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; } } } </syntaxhighlight> <br> ==== バルーンヒント ==== バルーンヒントとは、タスクトレイのアイコンから表示されるポップアップメッセージである。<br> <br> 以下の例では、NotifyIconにバルーンヒントを追加している。<br> フォームにあるボタンを押下する時、バルーンヒントが5秒間表示している。<br> <syntaxhighlight lang="c#"> // 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秒間表示 } } } </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> <br><br> __FORCETOC__ [[カテゴリ:C_Sharp]]
C Sharpの基礎 - タスクトレイ
に戻る。
案内
メインページ
最近の更新
おまかせ表示
MediaWiki についてのヘルプ
ツール
リンク元
関連ページの更新状況
特別ページ
ページ情報
We ask for
Donations
Collapse