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自定义计时关闭的更多相关文章

  1. winform 关于Messagebox自动定时关闭

    添加一个类库MessageBoxTimeOut public class MessageBoxTimeOut { private string _caption; public void Show(s ...

  2. 茗洋Easy UI 1.3.5 部分问题解决系列专题[自定义alert关闭时间,自动关]

    [评论,楼层数为30的倍数的,我送你我自己的博客园的皮肤,该博客参与活动] 这次我又给大家带来的EasyUI的我研究拓展的新特性 我使用的是  EasyUI 1.3.5版本的,项目是ASP.NET M ...

  3. 自定义延时关闭弹窗,替代MesssageBox

    1,新建一个窗体MessageForm,在里面加一个label控件和timer 2,代码如下: public partial class MessageForm : Form { int t; str ...

  4. WPF 自定义 MessageBox (相对完善版)

    WPF 自定义 MessageBox (相对完善版)     基于WPF的自定义 MessageBox. 众所周知WPF界面美观.大多数WPF元素都可以简单的修改其样式,从而达到程序的风格统一.可是当 ...

  5. WPF 自定义 MessageBox (相对完善版 v1.0.0.6)

    基于WPF的自定义 MessageBox. 众所周知WPF界面美观.大多数WPF元素都可以简单的修改其样式,从而达到程序的风格统一.可是当你不得不弹出一个消息框通知用户消息时(虽然很不建议在程序中频繁 ...

  6. 自定义 MessageBox 组件

    效果: 公共组件页面: js部分: <script>   export default {     props: {       title: {         type: String ...

  7. DELPHI中MDI子窗口的关闭和打开

    DELPHI中MDI子窗口的关闭 和打开       Delphi中MDI子窗口的关闭方式默认为缩小而不是关闭,所以当你单击子窗口右上角的关闭按钮时会   发觉该子窗口只是最小化,而不是你预期的那样被 ...

  8. MessageBox页面消息弹出框类

    MessageBox页面消息弹出框类: public class MessageBox { /// <summary> /// 自定义弹出窗口内容,不跳转 /// </summary ...

  9. MessageBox 弹框

    模拟系统的消息提示框而实现的一套模态对话框组件,用于消息提示.确认消息和提交内容.  从场景上说,MessageBox 的作用是美化系统自带的 alert.confirm 和 prompt,因此适合展 ...

随机推荐

  1. 使用abapGit在ABAP On-Premises系统和SAP云平台ABAP环境之间进行代码传输

    SAP ABAP顾问朋友们,应该都使用过SAPLink这个工具.如果两个ABAP Netweaver系统没有建立起传输路径时,我们无法使用标准的SE10事务码创建传输请求的方式进行这两个系统间的代码传 ...

  2. 昨天521表白失败,我想用Python分析一下...表白记录和聊天记录

    昨天跟喜欢的妹子表白了. 失败了!.下面是表白的聊天记录: (跟妹子已经认识一段时间) 我:灭嘤嘤,我喜欢你. 妹子:你干嘛? 我:今天520,跟你表白鸭. 妹子:那....有多喜欢? 我: 有很多很 ...

  3. [dev][ipsec][distributed] strongswan如何做热迁移/高可用/High Availability

    问题描述: 原生的基于kernel 的 strongswan 如何做高可用,HA,High Availability 问题分析: 基于我们已知的,ipsec,strongswan的知识.问题分解如下: ...

  4. 最近都会来学一点Python

    https://www.cnblogs.com/hellosecretgarden/p/9206648.html 打开电脑,发现Python都是之前的代码,将近一年之前的时间. 最近都会重新掌握起来, ...

  5. Luogu P1114 “非常男女”计划/Luogu P2697 宝石串

    Luogu P1114 "非常男女"计划/Luogu P2697 宝石串 (感觉我最近很爱做双倍经验的题啊) 使$d$等于第$i$个位置男生数(绿宝石数)减女生数(红宝石数)的差值 ...

  6. Python下载图片并保存本地的两种方式

    一:使用Python中的urllib类中的urlretrieve()函数,直接从网上下载资源到本地,具体代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...

  7. HTML5 Web SQL 数据库

    呼和浩特seo:Web SQL 数据库 API 并不是 HTML5 规范的一部分,但是它是一个独立的规范,引入了一组使用 SQL 操作客户端数据库的 APIs. 如果你是一个 Web 后端程序员,应该 ...

  8. python - Flask 基础(1)

    这两天稍微接触了一点 Flask 框架,所以分享点基础 1. 配置文件 from flask import Flask app = Flask(__name__) # 使用自定义的配置文件 app.c ...

  9. SQL:SQL Broker

    -- ============================================= --启用 Broker USE master; GO ALTER DATABASE DEV_AMS S ...

  10. HashMap与HashTable的理解与区别

    Hashtable是java一开始发布时就提供的键值映射的数据结构,而HashMap产生于JDK1.2.虽然Hashtable比HashMap出现的早一些,但是现在Hashtable基本上已经被弃用了 ...