• 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);
    }
    }
    }
    }

  • 相关阅读:
    Asp.NetCore3.1 WebApi 获取配置json文件中的数据
    Dapper同时操作任意多张表的实现
    将视图批量新增到PowerDesigner中并以model图表的形式展示
    .NetCore3.1获取文件并重新命名以及大批量更新及写入数据
    .NetCore 简单的使用中间件
    比较复杂的SQL转Linq
    Asp.NetCore3.1版本的CodeFirst与经典的三层架构与AutoFac批量注入
    Git与GitLab的分支合并等简单的测试操作
    Winform的控件以及DataGridView的一般使用
    在Linux系统中运行并简单的测试RabbitMq容器
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/11165497.html
Copyright © 2020-2023  润新知