Windows Forms MessageBox 弹出确定对话框倒计时关闭

1、普通弹框提示

private void btnDialog_Click(object sender, EventArgs e)
{
    DialogResult dr = MessageBox.Show("系统关机提示:\n\n 确认关机请点击【确定】\n\n 继续使用请点击【取消】",
               "系统将于" + 12 + "秒后关机", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
    if (dr == DialogResult.OK)
    {
        MessageBox.Show("您选择的是【确认】");
    }
    else if (dr == DialogResult.Cancel)
    {
        MessageBox.Show("您选择的是【取消】");
    }
}

2、Timer控件,倒计时关闭弹出的确认对话框
注意:Timer控件,Interval属性设置成1000

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace AutomationinWinForms
{
    public partial class TimerControlForm : Form
    {
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string IpClassName, string IpWindowName);
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int SetWindowText(IntPtr hWnd, string text);

        [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        /// <summary>
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        const int WM_CLOSE = 0x10;
        const int BM_CLICK = 0xF5;
        int FunCord;
        IntPtr hwnd;
        int t;

        public TimerControlForm()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            FunCord = 3;
            t = 5;
            timer1.Enabled = true;
            if (MessageBox.Show("系统关机提示:\n\n 确认关机请点击【确定】\n\n 继续使用请点击【取消】",
                "系统将于" + t + "秒后关机",
                MessageBoxButtons.OKCancel,
                MessageBoxIcon.Warning,
                MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                MessageBox.Show("点击了确定");
            }
            timer1.Enabled = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            FunCord = 4;
            t = 5;
            timer1.Enabled = true;
            if (MessageBox.Show("系统关机提示:\n\n 确认关机请点击【确定】\n\n 继续使用请点击【取消】",
                "系统将于" + t + "秒后关机",
                MessageBoxButtons.OKCancel,
                MessageBoxIcon.Warning,
                MessageBoxDefaultButton.Button2) == DialogResult.Cancel)
            {
                MessageBox.Show("点击了取消");
            }
            timer1.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (FunCord == 3)
            {
                hwnd = FindWindow(null, "系统将于" + t.ToString() + "秒后关机");
                t = t - 1;
                SetWindowText(hwnd, "系统将于" + t.ToString() + "秒后关机");
                if (t == 0)
                {
                    IntPtr OKHwnd = FindWindowEx(hwnd, IntPtr.Zero, null, "确定");
                    SendMessage(OKHwnd, BM_CLICK, 0, 0);
                    timer1.Enabled = false;
                }
            }
            else if (FunCord == 4)
            {
                hwnd = FindWindow(null, "系统将于" + t.ToString() + "秒后关机");
                t = t - 1;
                SetWindowText(hwnd, "系统将于" + t.ToString() + "秒后关机");
                if (t == 0)
                {
                    IntPtr OKHwnd = FindWindowEx(hwnd, IntPtr.Zero, null, "取消");
                    SendMessage(OKHwnd, BM_CLICK, 0, 0);
                    timer1.Enabled = false;
                }
            }
        }
    }
}

3、Timer类,倒计时关闭弹出的确认对话框

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace AutomationinWinForms
{
    public partial class TimerClassForm : Form
    {
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string IpClassName, string IpWindowName);
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int SetWindowText(IntPtr hWnd, string text);

        [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        /// <summary>
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        const int WM_CLOSE = 0x10;
        const int BM_CLICK = 0xF5;
        int FunCord;
        IntPtr hwnd;
        int t;

        public TimerClassForm()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            t = 10;
            Timer tm = new Timer();
            tm.Interval = 1000;
            tm.Enabled = true;
            tm.Tick += new EventHandler(tm_Tick);

            FunCord = 3;
            if (MessageBox.Show("系统关机提示:\n\n 确认关机请点击【确定】\n\n 继续使用请点击【取消】",
                "系统将于" + t + "秒后关机",
                MessageBoxButtons.OKCancel,
                MessageBoxIcon.Warning,
                MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                MessageBox.Show("点击了确定");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            t = 10;
            Timer tm = new Timer();
            tm.Interval = 1000;
            tm.Enabled = true;
            tm.Tick += new EventHandler(tm_Tick);

            FunCord = 4;
            if (MessageBox.Show("系统关机提示:\n\n 确认关机请点击【确定】\n\n 继续使用请点击【取消】",
                "系统将于" + t + "秒后关机",
                MessageBoxButtons.OKCancel,
                MessageBoxIcon.Warning,
                MessageBoxDefaultButton.Button2) == DialogResult.Cancel)
            {
                MessageBox.Show("点击了取消");
            }
        }

        public void tm_Tick(object sender, EventArgs e)
        {
            if (FunCord == 3)
            {
                hwnd = FindWindow(null, "系统将于" + t.ToString() + "秒后关机");
                t = t - 1;
                SetWindowText(hwnd, "系统将于" + t.ToString() + "秒后关机");
                if (t == 0)
                {
                    IntPtr OKHwnd = FindWindowEx(hwnd, IntPtr.Zero, null, "确定");
                    SendMessage(OKHwnd, BM_CLICK, 0, 0);
                }
            }
            else if (FunCord == 4)
            {
                hwnd = FindWindow(null, "系统将于" + t.ToString() + "秒后关机");
                t = t - 1;
                SetWindowText(hwnd, "系统将于" + t.ToString() + "秒后关机");
                if (t == 0)
                {
                    IntPtr OKHwnd = FindWindowEx(hwnd, IntPtr.Zero, null, "取消");
                    SendMessage(OKHwnd, BM_CLICK, 0, 0);
                }
            }
        }
    }
}

*
*

  • 0
    点赞
  • 1
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:技术黑板 设计师:CSDN官方博客 返回首页
评论

打赏作者

KingCruel

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值