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自动定时关闭和自定义提示框总按钮上文本的问题的整理

    如果要改变Messagebox上按钮的文本和自动关闭Messagebox提示框,一种方法是自定义一个winform窗口模仿替代Messagebox,变通的实现效果,另一种方法是通过调用系统的API来实 ...

  2. this.$modal.confirm 自定义按钮关闭_Excel迫使人类基因重命名?用VBA给科学家们支一招!—— 怎样快速定制工具栏按钮...

    点击上方蓝色文字关注我们吧~ 大家好!前几天有一个不大不小的科技新闻引起了很多朋友的共鸣: -NO.01- 为什么Excel会让科学家犯愁? - -  - - 之所以说它能引起共鸣,是因为科学家们遇到 ...

  3. this.$modal.confirm 自定义按钮关闭_自定义函数,让你玩转Excel得心应手

    让"自动更正"输入统一的文本,你是不是经常为输入某些固定的文本,如<电脑报>而烦恼呢?那就往下看吧. 1.执行"工具→自动更正"命令,打开" ...

  4. this.$modal.confirm 自定义按钮关闭_【勤哲资料】7.6 自定义打印

    自定义打印 通常情况下,我们填报了一份表单,若想打印的话,直接利用Excel本身的打印功能就可以了,但是在某些情况下,Excel的打印功能就不能完全满足我们的需要了,例如: 套打 把合计行作为页尾,每 ...

  5. Element-UI 组件MessageBox 弹框 关闭遮罩层

    奇怪需求,Dialog不做body遮罩导致多一级的遮罩会显示异常,就把Dialog里的MessageBox的关闭,又不影响其他的使用,故关闭后需要打开,终于在官网看到并尝试成功了,分享出来 this. ...

  6. 自定义WPF关闭窗口事件触发的函数

    背景: 例如我在点击窗口关闭按钮时候要执行的函数有一个,名为 Window_Closing 那么我需要在界面控件初始化后添加到Closing中 public SerialOption(){Initia ...

  7. C#WinFormMessageBox(C#),XtraMessageBox(devexpress),自定义MessageBox

    1.自定义MessageBox C#自定义控件之-自定义MessageBox_微尘物联-联通无限-CSDN博客_c# 自定义messagebox var dr= new MessageBoxEx.Me ...

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

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

  9. python 关闭窗口事件_关于python:如何在Tkinter中处理窗口关闭事件?

    如何在Python Tkinter程序中处理窗口关闭事件(用户单击" X"按钮)? Tkinter支持一种称为协议处理程序的机制.在这里,术语协议是指应用程序和窗口管理器之间的交互 ...

最新文章

  1. CVPR 2021 Oral | Transformer再发力!华南理工和微信提出UP-DETR
  2. 人脸识别如何在大型银行中大规模商用?
  3. wireshark 如何修改抓包时间日期显示格式?
  4. Win10修改防火墙入站规则
  5. VTK:PolyData之PointNormals
  6. eclipse3.4 SVN插件安装
  7. Linux系统语言教程,Linux操作系统基础及语言基础教程-麦可网张凌华
  8. 数据库行转列在现实需求中的用法
  9. python如何使用多线程_Python多线程与多线程中join()的用法
  10. java正则表达式校验车牌号_车牌号校验正则表达式
  11. python自动翻译pdf_python pdf一键翻译脚本
  12. 【折腾】GitHub Actions 反代 RSSHub + 多实例轮询
  13. 机器学习入门好文,强烈推荐
  14. 周易正易 p1-100
  15. 基于人脸识别的课堂考勤系统 tkinter+openpyxl+face_recognition
  16. 基于百万级别的站内信设计
  17. 关于js的回调函数,同步回调与异步回调
  18. Linux时间函数time()、ctime()、ctime_r()、localtime()、localtime_r()、asctime()、strftime()的转换关系
  19. 争议中的云算力市场 |链捕手
  20. python编程的就业方向_2021年python编程就业方向是怎样的?

热门文章

  1. 前端学习日记(八):css中字体、列表、鼠标形状、显示与隐藏、溢出处理、盒子模型的简单应用
  2. 携程Java面试题,疯狂java讲义第三版
  3. linux关闭established状态,有关ESTABLISHED,CLOSE_WAIT,TIME_WAIT等连接状态
  4. layui框架----下拉框的二级联动
  5. QT使用windeployqt部署发布及其精简
  6. 计蒜客习题:新年礼物
  7. 计算机游戏的作文,游戏迷作文
  8. Node.js:pm2管理进程启动npm run dev和开机自启
  9. 自考计算机辅助设计真题07075,07075计算机辅助设计.doc
  10. oracle声明一个表变量赋值,Oracle Declare 变量声明