1、托盘显示的标题、内容、图标等信息在界面设置。
2、后台代码
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (this.Visible == true) { e.Cancel = true; this.Visible = false; } } private void notifyIcon1_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { this.Visible = true; this.WindowState = FormWindowState.Normal; } } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { this.Visible = false; Application.Exit(); } }
static class Program { [DllImport("user32.dll")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll ")] static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab); [DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("User32.dll")] private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); public static IntPtr formhwnd; ////// 应用程序的主入口点。 /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); bool createdNew; Mutex mutex = new Mutex(false, "Notify", out createdNew); if (!createdNew) { Process instance; Process currentProcess = Process.GetCurrentProcess(); Process[] process = Process.GetProcessesByName(currentProcess.ProcessName); for (int i = 0; i < process.Length; i++) { if (process[i].Id != currentProcess.Id) { instance = process[i]; if (instance.MainWindowHandle != IntPtr.Zero) { SwitchToThisWindow(instance.MainWindowHandle, true); } else { formhwnd = FindWindow(null, "Form1"); ShowWindowAsync(formhwnd, 1); SetForegroundWindow(formhwnd); } return; } } } Form1 form = new Form1(); Application.Run(form); } }