1

我正在使用 Windows 窗体,如果用户尝试关闭窗口或尝试在进程完成之前关闭应用程序(用户忘记完成进程),我想向用户显示进程未完成的消息,如果用户按下确定,我想阻止窗口关闭并让用户完成该过程,我确实在网上找到了一些代码,但它是在 VB 中而不是在 c#

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason.Equals(CloseReason.WindowsShutDown))
    {
        Microsoft.VisualBasic.Interaction.Shell("shutdown -a", AppWinStyle.MinimizedFocus, false, -1);
        MessageBox.Show("Shutdown process cancelled!");
    }
}
4

1 回答 1

5

最好改用这段代码片段,e.Cancel根据您的要求使用:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason.Equals(CloseReason.WindowsShutDown))
    {
       if (MessageBox.Show("You are closing this app.\n\nAre you sure you wish to exit ?", "Warning: Not Submitted", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Stop) == DialogResult.Yes)
           return;    
       else    
           e.Cancel = true;
    }
}

参考:SystemEvents.SessionEnding 事件

当用户尝试注销或关闭系统时发生。

于 2012-10-29T06:35:19.443 回答