可设置指定时间自己主动消失的 MessageBox实现

本文主要是讲怎样实现可设置指定时间自己主动消失的 MessageBox提示框ShowMessageBoxTimeout实现;

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

ShowMessageBoxTimeout实现

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

        //三个參数:1、文本提示-text。2、提示框标题-caption。3、按钮类型-MessageBoxButtons ,4、自己主动消失时间设置-timeout
        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);
            }
        }
    }
}

ShowMessageBoxTimeout调用

//三个參数:1、文本提示。2、提示框标题。3、按钮类型,4、自己主动消失时间设置
ShowMsg.ShowMessageBoxTimeout("欢迎使用数据导出服务程序,本程序默认最小化到电脑托盘,1分钟后正式启动。", 
"程序启动温馨提示-窗体1分钟内无操作会自己主动关闭", MessageBoxButtons.OK, 1000 * 60 * 1);

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值