任务栏按钮右键单击:Windows窗体中的自定义菜单

时间:2012-07-18 20:25:23

标签: c# windows winforms

当在右键单击任务栏按钮时弹出的菜单中单击“关闭窗口”选项时,我不希望表单退出。相反,我希望应用程序最小化到系统托盘。 如何更改“关闭窗口”的行为?

1 个答案:

答案 0 :(得分:1)

添加OnFormClosing的覆盖,并查看事件参数参数的CloseReason。也许是这样的:

protected override OnFormClosing(FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing)
    {
        e.Cancel = true;
        this.Hide();
    }
    else
    {
        this.Close();
    }
}

这样,用户无法关闭您的表单(仅隐藏它),但Windows仍然可以出于其他原因(例如关闭)。