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);
posted @ 2017-02-23 13:35  蜗牛小传  阅读(597)  评论(0编辑  收藏  举报

青春是什么?青春就像下面这段文字!

因为我处于页面最下端,所以很少有人看到我,因为我的字体小,所以即使别人看到了,也可能一扫而过,可能不会有人愿意停下来读一下我,对别人来讲,我就像一段无聊的广告一样放在这里,对我来讲,这就是我的青春。

我曾无数次问设计师,为什么不能把我放在首页最上方,为什么不能让所有人看到我?为什么我的青春不能像别人一样绽放?设计师从来没有回答过我的问题,实际上他从来都没听到过我的声音,你是否也和我一样?拥有梦想,

却不得不在人群中彷徨,拥有青春,却没有人愿意停下来为你鼓掌?