先给窗口添加两个组件: notifyIcon 和 contextMenuStrip
并且把contextMenuStrip 绑定到 notifyIcon 上面
再给contextMenuStrip添加两个子菜单: 恢复 和 退出
然后依次实现下面的事件
private void Form1_Load(object sender, EventArgs e)
{
int x = Screen.PrimaryScreen.WorkingArea.Right - this.Width - 10;
int y = Screen.PrimaryScreen.WorkingArea.Bottom - this.Height;
this.Location = new Point(x, y);//设置窗体在屏幕右下角显示
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Visible = false;
if (Screen.PrimaryScreen.WorkingArea.Right - this.Right < 3)
{
this.Left = Screen.PrimaryScreen.WorkingArea.Right - this.Width - 10;
}
if (this.Top < 3)
{
this.Top = 5;
}
this.Visible = true;
this.WindowState = FormWindowState.Normal;
}
private void Form1_Deactivate(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Visible = false;
}
}
private bool _isClose = false;
private void 恢复ToolStripMenuItem_Click(object sender, EventArgs e)
{
notifyIcon1_DoubleClick(sender, null);
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
_isClose = true;
this.notifyIcon1.Visible = false;
Application.Exit();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (!_isClose)
{
e.Cancel = true;
}
this.Visible = false;
}