2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

サンプル: C# でタスクトレイに常駐

Last updated at Posted at 2021-05-15
  • VS 2017 Express
  • Form で作成、.Net Framework 4.6を指定した。
  • app.ico が別途必要。https://icon-icons.com/ja/ などからアイコンを準備

キャプチャ2.PNG

キャプチャ.PNG

Program.cs
using System;
using System.Diagnostics; // ProcessStartInfoで必要
using System.Drawing;
using System.Windows.Forms;

class MainWindow
{
	static void Main()
	{
		Launcher launcher = new Launcher();
		Application.Run();
	}
}

class Launcher : Form
{
	public Launcher()
	{
		this.ShowInTaskbar = false;
		this.setComponents();
	}

	private void cmdHello()
	{
		MessageBox.Show("こんにちは。");
	}

	private void cmdIe()
	{
		ProcessStartInfo processStartInfo = new ProcessStartInfo("C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe", "http://yahoo.co.jp");
		Process process = Process.Start(processStartInfo);
	}

	private void Close_Click()
	{
		Application.Exit();
	}

	private void setComponents()
	{
		NotifyIcon icon = new NotifyIcon();
		icon.Icon = new Icon("app.ico");
		icon.Visible = true;
		icon.Text = "常駐アプリテスト";
		ContextMenuStrip menu = new ContextMenuStrip();
		ToolStripMenuItem menuItem = new ToolStripMenuItem();

		menu.Items.AddRange(new ToolStripMenuItem[]{
			new ToolStripMenuItem("命令cmdHello", null, (s,e)=>{cmdHello();}),
			new ToolStripMenuItem("命令cmdIe", null, (s,e)=>{cmdIe();}),
			new ToolStripMenuItem("終了", null, (s,e)=>{Close_Click();})
		});

		icon.ContextMenuStrip = menu;
	}
}

いろんなページを参考にしました。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?