我将首先截取负责 会话当前正在关闭 并附加一个事件处理程序，然后从该事件中，我将检查您的小复选框是否已选中，如果已选中（true），则可以尝试 AbortSystemShutdownA通过那个电话 拦截然后取消关机过程。您还可以在文档中找到有关以下内容的更多信息： 关机事件。这也可以使用 平沃. - Search Shutdown on the Pinvoke page. If you have a specific question regarding implementing any of these events, post up what you've tried and we can try to help you from there.
The e.Cancel = true; will prevent your application from closing, but if someone tries to log off, only your application will prevent windows from logging off. You should note; it won't stop the closure of other applications on the system while the logoff is commencing. When windows shuts down, it collects a list of running applications in need of being closed before the log off can commence. Depending on the order of the opened applications being cued for closure, whatever applications are cued before yours, will also be closed regardless of your applications current state, and windows will only halt when your application is being processed. Any applications cued after your application will also not be shut down. However, a user can still persist to ignore your applications state and force the log off to continue by way of interacting with log off screen. See pic :
我认为完全防止关机的唯一方法是（可能的话）更改一些安全策略，以防止用户组具有手动注销的能力。这不是我以前尝试过的东西，但是有可能。 根据这个, there is an option to remove the button from the Start menu. So in theory, you could include using Microsoft.GroupPolicy; and get to work on rewriting the registry values. But yuck, and I would highly recommend you backup your registry in full before playing with a major part of the operating system if you're on a development machine or not. The Microsoft.GroupPolicy will give you much more control over a users ability to log off VIA the UI, but if they know how to reboot and log off via cmd, you may have a new problem on your hands to prevent that. Some here might argue that group policy might not be the best way to go about it.