gpt4 book ai didi

c# - 防止 Windows 关闭

转载 作者:太空宇宙 更新时间:2023-11-03 20:21:39 25 4
gpt4 key购买 nike

我正在使用 windows 窗体,如果用户试图关闭 windows 或试图在进程完成之前关闭应用程序(用户忘记完成process),如果用户按下 OK 我想停止关闭窗口并让用户完成该过程,我确实在网上找到了一些代码来这样做,但它是在 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!");
}
}

最佳答案

最好改用这段代码片段,根据您的要求使用 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 Event

Occurs when the user is trying to log off or shut down the system.

关于c# - 防止 Windows 关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13116856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com