You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中复用Windows 10关机提示窗口(无需关机)

可行吗?答案是:直接通过shutdown.exe做不到,但可以用C#自行实现一个模拟的提示窗口

为什么直接调用shutdown.exe不行?

  • shutdown.exe的核心功能是触发系统关机、重启、注销等操作,它弹出的提示窗口是系统内置的固定组件:
    • 仅能通过-c参数设置窗口内的描述文本,没有任何参数支持自定义窗口标题;
    • 只要执行了带关机触发的命令(比如shutdown -s -c "提示内容"),系统就会进入关机倒计时流程,无法单独剥离出这个窗口而不执行关机操作。

替代方案:用C#自行实现模拟窗口

既然系统自带的提示窗口无法满足需求,最直接的方式是用C#的UI框架(WinForms/WPF)创建一个自定义窗口,模拟系统提示的样式,完全可控:

WinForms示例代码

using System;
using System.Windows.Forms;

namespace CustomShutdownPrompt
{
    public partial class ShutdownPromptForm : Form
    {
        public ShutdownPromptForm(string title, string message)
        {
            InitializeComponent();
            this.Text = title;
            this.labelMessage.Text = message;
            // 模拟系统提示窗口的样式
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.TopMost = true;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }

    // 调用示例
    class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            using (var prompt = new ShutdownPromptForm("自定义提示标题", "这是自定义的提示内容,不会触发系统关机"))
            {
                prompt.ShowDialog();
            }
        }
    }
}

关键优势

  • 完全自定义窗口标题、提示文本、按钮样式;
  • 不会触发任何系统关机操作,窗口的显示和关闭逻辑完全由代码控制;
  • 可根据需求扩展功能,比如添加倒计时显示、取消按钮等。

内容的提问来源于stack exchange,提问作者Jonathan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 08:35:57