1.拖入控件notifyIcon1到窗口
2.拖入ContextMenuStrip到窗口
3.选中notifyIcon1右键属性,右侧属性窗口栏找到ContextMenuStrip,选择contextMenuStrip1
4.选择ContextMenuStrip,添加菜单选项
右键图标:
Form1主界面关闭按钮事件添加代码:
void Min() { if (WindowState == FormWindowState.Normal && this.Visible == true) { notifyIcon1.Visible = true;//在通知区显示Form的Icon WindowState = FormWindowState.Minimized; // Visible = false; ShowInTaskbar = false;//使Form不在任务栏上显示 } // Close(); // GC.Collect(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing)//当用户点击窗体右上角X按钮或(Alt + F4)时 发生 { e.Cancel = true; //notifyIcon1.Visible = true;//在通知区显示Form的Icon //WindowState = FormWindowState.Minimized; //Visible = true; //ShowInTaskbar = false;//使Form不在任务栏上显示 Min(); } }
托盘控件的双击事件
public void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { contextMenuStrip1.Show(); } if (e.Button == MouseButtons.Left) { this.Visible = true; this.WindowState = FormWindowState.Normal; } }