Messagebox自定义计时关闭
Messagebox自定义计时关闭
新建Winform项目WindowsFormsAppTESTMessageBoxAutoClose

主窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsAppTESTMessageBoxAutoClose
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
MessageBoxTimeOut mb = new MessageBoxTimeOut();
mb.Show("123", "提示:");
}
private void Button2_Click(object sender, EventArgs e)
{
FunCord = 1;
StartTimer(1000);
t = 10;
timer1.Enabled = true;
if (MessageBox.Show("系统提示:确认关闭请点击【确定】, 继续使用请点击【取消】", t + "秒后关闭", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2)== DialogResult.OK)
{
MessageBox.Show("哈哈哈,你选择了ok");
}
//MessageBox.Show("系统提示:确认关闭请点击【确定】, 继续使用请点击【取消】", t + "秒后关闭", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
//MessageBox.Show("直接关闭");
}
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int SetWindowText(IntPtr hWnd, string text);
[DllImport("User32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[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;
System.Windows.Forms.Timer timer1;
private void StartTimer(int interval)
{
timer1 = new Timer();
timer1.Interval = interval;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Enabled = true;
}
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;
}
}
}
扩展类MessageBoxTimeOut:
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsAppTESTMessageBoxAutoClose
{
public class MessageBoxTimeOut
{
private string _caption;
public void Show(string text, string caption)
{
Show(3000, text, caption);
}
public void Show(int timeout, string text, string caption)
{
Show(timeout, text, caption, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
public void Show(int timeout, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
this._caption = caption;
StartTimer(timeout);
MessageBox.Show(text, caption, buttons, icon);
}
private void StartTimer(int interval)
{
Timer timer = new Timer();
timer.Interval = interval;
timer.Tick += new EventHandler(Timer_Tick);
timer.Enabled = true;
}
private void Timer_Tick(object sender, EventArgs e)
{
KillMessageBox();
//停止计时器
((Timer)sender).Enabled = false;
}
[DllImport("User32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
public const int WM_CLOSE = 0x10;
private void KillMessageBox()
{
//查找MessageBox的弹出窗口,注意对应标题
IntPtr ptr = FindWindow(null, this._caption);
if (ptr != IntPtr.Zero)
{
//查找到窗口则关闭
PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
}
}
}
Messagebox自定义计时关闭的更多相关文章
- winform 关于Messagebox自动定时关闭
添加一个类库MessageBoxTimeOut public class MessageBoxTimeOut { private string _caption; public void Show(s ...
- 茗洋Easy UI 1.3.5 部分问题解决系列专题[自定义alert关闭时间,自动关]
[评论,楼层数为30的倍数的,我送你我自己的博客园的皮肤,该博客参与活动] 这次我又给大家带来的EasyUI的我研究拓展的新特性 我使用的是 EasyUI 1.3.5版本的,项目是ASP.NET M ...
- 自定义延时关闭弹窗,替代MesssageBox
1,新建一个窗体MessageForm,在里面加一个label控件和timer 2,代码如下: public partial class MessageForm : Form { int t; str ...
- WPF 自定义 MessageBox (相对完善版)
WPF 自定义 MessageBox (相对完善版) 基于WPF的自定义 MessageBox. 众所周知WPF界面美观.大多数WPF元素都可以简单的修改其样式,从而达到程序的风格统一.可是当 ...
- WPF 自定义 MessageBox (相对完善版 v1.0.0.6)
基于WPF的自定义 MessageBox. 众所周知WPF界面美观.大多数WPF元素都可以简单的修改其样式,从而达到程序的风格统一.可是当你不得不弹出一个消息框通知用户消息时(虽然很不建议在程序中频繁 ...
- 自定义 MessageBox 组件
效果: 公共组件页面: js部分: <script> export default { props: { title: { type: String ...
- DELPHI中MDI子窗口的关闭和打开
DELPHI中MDI子窗口的关闭 和打开 Delphi中MDI子窗口的关闭方式默认为缩小而不是关闭,所以当你单击子窗口右上角的关闭按钮时会 发觉该子窗口只是最小化,而不是你预期的那样被 ...
- MessageBox页面消息弹出框类
MessageBox页面消息弹出框类: public class MessageBox { /// <summary> /// 自定义弹出窗口内容,不跳转 /// </summary ...
- MessageBox 弹框
模拟系统的消息提示框而实现的一套模态对话框组件,用于消息提示.确认消息和提交内容. 从场景上说,MessageBox 的作用是美化系统自带的 alert.confirm 和 prompt,因此适合展 ...
随机推荐
- jenkins中的流水线( pipeline)的理解(未完)
目录 一.理论概述 Jenkins流水线的发展历程 什么是Jenkins流水线 一.理论概述 pipeline是流水线的英文释义,文档中统一称为流水线 Jenkins流水线的发展历程 在Jenki ...
- JAVA笔记整理(六),JAVA中的多态
JAVA引用变量有两个类型:一个是编译时类型,一个运行时类型 编译时类型由声明该变量时使用的类型决定,运行时类型由实际赋给该变量的对象决定.如果编译时类型和运行时类型不一样,就形成了多态. 因为子类其 ...
- Windows 10安装
使用U盘安装操作系统教程 本教程介绍如何使用U盘安装操作系统,以安装Windows 10过程作为举例. 1 获取操作系统iso镜像文件 获取操作系统ISO镜像文件有很多途径,此处介绍从微软官网下载wi ...
- 解密Redis持久化【翻译】
本文来自Redis的作者,他在论坛看到大家对Redis持久化误解较大,所以写此文章论述持久化 写操作的流程 首先我们来看一下数据库在进行写操作时到底做了哪些事,主要有下面五个过程. 客户端向服务端发送 ...
- Scala配置环境变量windows
scala下载官网网址:http://www.scala-lang.org/download/ 1.下载scala-2.10.4.msi 2.点击安装scala,默认安装路径 3.配置环境变量 ( ...
- k8s的Pod状态和生命周期管理
Pod状态和生命周期管理 一.什么是Pod? 二.Pod中如何管理多个容器? 三.使用Pod 四.Pod的持久性和终止 五.Pause容器 六.init容器 七.Pod的生命周期 (1)Pod p ...
- 并发编程大师系列之:CountDownLatch和Join
业务场景描述:假设一条流水线上有三个工作者:worker1,worker2,worker3.有一个任务的完成需要他们三者协作完成,worker3可以开始这个任务的前提是worker1和worker2完 ...
- @EnableCircuitBreaker熔断超时机制
客户端请求服务端的时候总是报超时,默认熔断机制是1S
- Selenium常用API的使用java语言之13-多表单切换
在 Web 应用中经常会遇到 frame/iframe 表单嵌套页面的应用, WebDriver 只能在一个页面上对元素识别与 定位, 对于 frame/iframe 表单内嵌页面上的元素无法直接定位 ...
- python 单引号与双引号的转义
import simplejson a = """{"a":"\\""}""" b = & ...