简体   繁体   English

使用CANCEL选项C#防止窗口关闭

[英]Prevent windows Shutdown with CANCEL option C#

I'm trying to include in my Windows Form App a way that when user tries to shutdown windows it opens a dialog box with CANCEL option....that office classic one. 我试图在我的Windows窗体应用程序中包含一种方式,当用户尝试关闭窗口时,它会打开一个带有CANCEL选项的对话框....该办公室经典之一。

In other topics, people describes how to prevent windows shutdown. 在其他主题中,人们描述了如何防止Windows关闭。 They use a dialog box for this. 他们使用一个对话框。 It helps but if the user immediately clicks in any option in this box, windows closes the application. 它有帮助但是如果用户立即点击此框中的任何选项,则Windows会关闭应用程序。

You can understand what I'm meaning, doing the following test: 你可以理解我的意思,做以下测试:

In windows Vista or 7 Open Paint, Word or any office soft and begin writing something. 在Windows Vista或7中打开Paint,Word或任何办公软件并开始写东西。 Do not save it. 不要保存它。

Try to shutdown windows and when the classic save dialog box appears IMMEDIATELY click in "cancel". 尝试关闭窗口,当出现经典保存对话框时, 立即单击“取消”。

You will see that the application continues to work and windows is asking you what you want to do. 您将看到应用程序继续工作,Windows正在询问您要执行的操作。

I tried to follow this Microsoft Link but if I click "ok" in Message Box, the App closes. 我尝试按照此Microsoft链接,但如果我在消息框中单击“确定”,应用程序将关闭。

   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;
        }
    }

Although delay windows shutdown is not recommended it is possible using shutdown scripts. 虽然不推荐延迟窗口关闭,但可以使用关闭脚本。 You can configure your own script file using gpedit.msc configuration file. 您可以使用gpedit.msc配置文件配置自己的脚本文件。 I have read that these scripts can delay the windows shutdown up to 10 minutes. 我已经读过这些脚本可以将窗口关闭延迟10分钟。

I hope it helps 我希望它有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM