• C# 设定时间内自动关闭提示框


    通过程序来自动关闭这个消息对话框而不是由用户点击确认按钮来关闭。然而.Net framework 没有为我们提供自动关闭MessageBox 的方法,要实现这个功能,我们需要使用Window API 来完成。

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Threading;
    
    namespace LockScreenMsg
    {
        public class ShowMsg
        {
            //使用FindWindow API 来查找对应的窗体句柄
            /// <summary>
            /// 通过窗口的类名或者窗口标题的名字来查找窗口句柄。
            /// </summary>
            /// <param name="lpClassName">窗口类名</param>
            /// <param name="lpWindowName">窗口标题名</param>
            /// <returns></returns>
            [DllImport("user32.dll", SetLastError = true)]
            static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
            //使用 EndDialog来关闭对话框
            [DllImport("user32.dll")]
            static extern bool EndDialog(IntPtr hDlg, out IntPtr nResult);
            /*思路是在调用MessageBox.Show 前启动一个后台工作线程,这个工作线程等待一定时间后开始查找消息对话框的窗口句柄,
             * 找到后调用EndDialog API 函数关闭这个消息对话框。不过这个方法有个问题,
             * 就是如果同时又多个同名的消息对话框(可能不一定是这个应用的),
             * 这样做可能会关错窗口,如何解决这个问题,我还没有想出比较好的方法,
             * */
         
    
    
            /// <summary>
            /// 提示框,在设定的时间内自动关闭
            /// </summary>
            /// <param name="text">文本提示</param>
            /// <param name="caption">提示框标题</param>
            /// <param name="buttons">按钮类型</param>
            /// <param name="timeout">自动消失时间设置(单位毫秒)</param>
            /// <returns>返回MessageBox的DialogResult</returns>
            public static DialogResult ShowMessageBoxTimeout(string text, string caption,
                MessageBoxButtons buttons, int timeout)
            {
                DialogResult dr;
                ThreadPool.QueueUserWorkItem(new WaitCallback(CloseMessageBox),new CloseState(caption, timeout));
                return dr=MessageBox.Show(text, caption, buttons);
            }
            //这个函数中首先利用线程池调用一个工作线程 CloseMessageBox ,并将对话框的标题和延时时间通过CloseState这个类传递给CloseMessageBox函数。
    
    
            private static void CloseMessageBox(object state)
            {
                CloseState closeState = state as CloseState;
    
                Thread.Sleep(closeState.Timeout);
                IntPtr dlg = FindWindow(null, closeState.Caption);//查找
    
                if (dlg != IntPtr.Zero)//如果查找到的结果不等于0
                {
                    IntPtr result;
                    EndDialog(dlg, out result);//关闭窗口
                }
            }
        }
    }
    
    

    CloseState类如下:

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace LockScreenMsg
    {
        class CloseState
        {
            private int _Timeout;
    
            /// <summary>
            /// In millisecond
            /// </summary>
            public int Timeout
            {
                get
                {
                    return _Timeout;
                }
            }
    
            private string _Caption;
    
            /// <summary>
            /// Caption of dialog
            /// </summary>
            public string Caption
            {
                get
                {
                    return _Caption;
                }
            }
    
            public CloseState(string caption, int timeout)
            {
                _Timeout = timeout;
                _Caption = caption;
            }
        }
    }
    
    

    使用:

    DialogResult dr= ShowMsg.ShowMessageBoxTimeout("在5s内若无任何动作程序将自动退出", "提示!", MessageBoxButtons.OK, 1000 * 5);
    
  • 相关阅读:
    日常巡检
    mysql 主从
    tomcat +apache 动静分离
    ELK安装
    LVS-NAT模式
    shell 三剑客
    shell $传参
    zabbix安装
    lvs-DR 负载均衡
    解决ubuntu中pycharm的图标没有问题
  • 原文地址:https://www.cnblogs.com/snail0404/p/6433080.html
Copyright © 2020-2023  润新知