NotifyIcon 图标
namespace SystemTrayTest{ public partial class Form1 : Form { // Components. public NotifyIcon notifyIcon1 = new NotifyIcon(); public Form1() { InitializeComponent(); } // Form resize event. private void Form1_Resize(object sender, EventArgs e) { // Do this when the windows gets minimized. if (this.WindowState == FormWindowState.Minimized) { this.ShowInTaskbar = false; this.WindowState = FormWindowState.Minimized; this.notifyIcon1.Visible = true; this.notifyIcon1.Text = "KlausurServer v 2.1"; } } // Initialise the icon on form load. private void Form1_Load(object sender, EventArgs e) { this.notifyIcon1 = new NotifyIcon(); this.notifyIcon1.Visible = false; try { notifyIcon1.Icon = new System.Drawing.Icon(@"D:\klausurServer.ico"); // Tell the delegate which method to call when // the icon click event occurs. this.notifyIcon1.Click += new System.EventHandler(notifyIcon1_Click); } catch (Exception ee) { Console.WriteLine(ee.Message); } } // Icon click event. private void notifyIcon1_Click(object sender, System.EventArgs e) { this.ShowInTaskbar = true; this.notifyIcon1.Visible = false; this.WindowState = FormWindowState.Normal; } }}This will send your application to the systemtray when you minimize the window. It wont be shown in the taskbar. If you click on the icon in the systemtray the application will be shown in its original size.
Ow yeah, in case you are trying to put this into a existing project make sure to add this into your Form1.Designer.cs :
|
1
2
|
this.Load += new System.EventHandler(this.Form1_Load);this.Resize += new System.EventHandler(this.Form1_Resize); |
This bit of code goes into the InitializeComponent() method.
浙公网安备 33010602011771号