[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;
        private void button1_Click(object sender, EventArgs e)
        {
            FunCord = 1;
            t = 5;
            timer1.Enabled = true;
            MessageBox.Show("系统关机提示:\n\n 确认关机请点击【确定】\n\n 继续使用请点击【取消】", "系统将于" + t + "秒后关机", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
            MessageBox.Show("直接关闭");

}
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (FunCord == 1)
            {
                hwnd = FindWindow(null, "系统将于" + t.ToString() + "秒后关机");
                t = t - 1;
                SetWindowText(hwnd, "系统将于" + t.ToString() + "秒后关机");
                if (t == 0)
                {
                    timer1.Enabled = false;
                    SendMessage(hwnd, WM_CLOSE, 0, 0);
                }
            }
            else if (FunCord == 2)
            {
                hwnd = FindWindow(null, "关机提示");
                IntPtr a = FindWindowEx(hwnd, (IntPtr)null, null, "系统将于" + t.ToString() + "秒后关机");
                t = t - 1;
                SetWindowText(a, "系统将于" + t.ToString() + "秒后关机");
                if (t == 0)
                {
                    timer1.Enabled = false;
                    SendMessage(hwnd, WM_CLOSE, 0, 0);
                }
            }
            else 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;
                }
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            FunCord = 2;
            t = 5;
            timer1.Enabled = true;
            MessageBox.Show("系统将于" + t + "秒后关机", "关机提示");
            timer1.Enabled = false;
        }

private void Form1_Load(object sender, EventArgs e)
        {

}

private void button3_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 button4_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;
        }

原文链接:https://www.itsvse.com/thread-4806-1-4.html

C# WinForm MessageBox弹窗倒计时的自动关闭的更多相关文章

  1. 对类HelloWorld程序中添加一个MessageBox弹窗

    对类HelloWorld程序中添加一个MessageBox弹窗 分析: 任一程序运行的时候都会加载kernel32.dll的,但MessageBoxA()这个API却是在user32.dll中的.所以 ...

  2. 【C#】使用user32.dll的MessageBox弹窗消息

    要使用user32.dll的MessageBox弹窗消息,自然需要引入user32.dll到项目中. 一个最简单的实例如下: using System; using System.Runtime.In ...

  3. layer.msg弹窗后没有自动关闭——问题解决

    layer.msg弹窗后没有自动关闭,里面也没有配time: -1 layer.msg("信息体", {time: -1}); 后面发现是jquer和layer版本不对,之前自己用 ...

  4. winform messagebox自动关闭

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. 玩转控件:重写/重绘Dev中MessageBox弹窗控件

    很久没有更新博客了,本想着直接发一篇<手撕ERP>系列,从控件重写.重绘,到框架搭建,再到部分模块实现+业务的.但是每次动手的时候,都觉得难以下手.直接从数据库设计开始吧,模块设计还没定下 ...

  6. winform右下角弹窗

    网页是否经常在电脑右下角弹窗显示消息?其实Winform也是可以实现的.下面介绍两种方法. 第一步:设计窗体 第二步:实现代码 第一种方法 引用user32 声明常量 窗体Load事件 窗体FormC ...

  7. C# WinForm MessageBox.Show显示在窗体中间

    一.新建MessageBoxEx类,并添加以下代码. using System; using System.Windows.Forms; using System.Text; using System ...

  8. WinForm MessageBox 提示对话框

    public class MessageUtil { /// <summary> /// 显示一般的提示信息 /// </summary> /// <param name ...

  9. C# Winform右下角弹窗方式

    [方法一] 第一步:winform项目创建完成后,添加一个窗口,命名为:Messages .(加上最开始的Form1,一共为两个窗口),双击主窗口进入后台代码 . 第二步:在Messages 窗口中添 ...

随机推荐

  1. Mysql 存储过程 + python调用存储过程 (内置函数讲解及定义摘抄)

    定义 存储过程:就是为以后的使用而保存的一条或多条 MySQL语句的集合.可将其视为批文件,虽然它们的作用不仅限于批处理. 个人使用存储过程的原因就是因为 存储过程比使用单独的SQL语句要快 有如下表 ...

  2. tomcat的根路径设置

    今天使用postman进行springmvc的测试发现提示404错误,检查原因发现是tomcat的配置问题.这里的发布的war包的访问地址 Application Context设置为了根路径”/” ...

  3. python 编码设置

    py 文件设置编码: # -*- coding: utf-8 -*- #coding=utf-8 两种方式任选一种即可 输出到浏览器设置编码: import io import sys sys.std ...

  4. 交互式计算引擎REOLAP篇

    交互式计算引擎ROLAP篇   摘自:<大数据技术体系详解:原理.架构与实践> 一.Impala Impala最初由Cloudera公司开发的,其最初设计动机是充分结合传统数据库与大数据系 ...

  5. Spring Boot+STOMP解决消息乱序问题

    当我们使用Spring Boot+websocket进行前后端进行通信时,我们需要注意:服务器可以随时向客户端发送消息.默认的情况下,不保证:服务器发送的消息与到达客户端的消息的顺序是一致的.可能先发 ...

  6. 让Jupyter Notebook个性化

    Win下更改jupyter主题 Themes地址 本人环境  Win+Conda 开始使用pip 安装,发现无法使用pip,修改环境变量,将D:\Program Files\Conda\Scripts ...

  7. Redis的竞争并发该如何解决?

    这个也是线上非常常见的一个问题,就是多客户端同时并发写一个key,可能本来应该先到的数据后到了,导致数据版本错了.或者是多客户端同时获取一个key,修改值之后再写回去,只 要顺序错了,数据就错了. 而 ...

  8. 《发际线总是和我作对》第九次团队作业:【Beta】Scrum meeting1

    项目 内容 这个作业属于哪个课程 软件工程 这个作业的要求在哪里 实验十三 团队作业9:Beta冲刺与团队项目冲刺 团队名称 发际线总和我作队 作业学习目标 (1)掌握软件黑盒测试技术:(2)掌握软件 ...

  9. canvans知识点

    1.绘制圆的角度示意图: 2 倒计时中,时钟数字的渲染逻辑: 3 直线边缘样式的设置 context.lineCap = "butt"; context.lineCap = &qu ...

  10. linux 下查看进程占用端口和端口号占用进程命令

    linux 下查看进程占用端口:(1)查看程序对应的进程号: ps -ef | grep 进程名字 (2)查看进程号所占用的端口号: netstat -nltp | grep  进程号 ubuntu ...