首页 > 其他 > 详细

可设置指定时间自动消失的 MessageBox

时间:2015-06-10 22:32:05      阅读:305      评论:0      收藏:0      [点我收藏+]

本文主要是讲如何实现可设置指定时间自动消失的 MessageBox提示框

在开发客户端应用程序的时候,经常用得WinForm中MessageBox提示框。但是有时候还是满足不了一些用户要求,客户要求千奇百怪,例如客户需要做某些提示的时候,不去点击确定或取消的时候,等待一段时间自动消失,为此我们可以使用下面类来实现,采用 Thread.Sleep来关掉当前提示框,具体代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;

namespace Tools.App
{
    public class ShowMsg
    {

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        static extern bool EndDialog(IntPtr hDlg, out IntPtr nResult);


        public static void ShowMessageBoxTimeout(string text, string caption,
            MessageBoxButtons buttons, int timeout)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(CloseMessageBox),
                new CloseState(caption, timeout));
            MessageBox.Show(text, caption, buttons);
        }


        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)
            {
                IntPtr result;
                EndDialog(dlg, out result);
            }
        }
    }
}

希望以上分享对初学朋友有些帮助,谢谢!
更多关注付义方技术博客:http://blog.csdn.net/fuyifang
或者直接用手机扫描二维码查看更多博文:
技术分享

可设置指定时间自动消失的 MessageBox

原文:http://blog.csdn.net/fuyifang/article/details/46447547

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!