none
如何加快状态栏消息的显示?

    问题

  • 系统 Win10 + .net framework 4.6.1

    使用系统自带 notifyIcon 组件用户显示消息,查看 MSDN 得知显示消息的停留时间是系统控制的,我想加快显示内容的切换速度,即使停留时间缩短,缩短到显示消息时,上一次的显示效果就自动消失,不知如何实现?谢谢。


    不重要的其实最重要

    2016年8月17日 9:02

全部回复

  • 你好,

    请问你的项目是 winform, WPF, 还是 web 的,能提供一下相关的代码吗?

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年8月18日 6:24
    版主
  • 你好

    目前项目是 winform,但以后我也打算用 wpf 重写。以下我截取部分代码并配以说明

    1. 自定义一个 NotifyProxy 类,用来创建一个状态栏托盘实例,并包装一些功能

    /// <summary>
            /// 初始化托盘代理
            /// </summary>
            public static void Initialize()
            {
                InitContextMenu();
                if (mainNotify != null) { mainNotify.Dispose(); }
    
                mainNotify = new NotifyIcon();
                mainNotify.ContextMenuStrip = mainNotifyMenu;
                mainNotify.Icon = ImageResource.Eye;
                mainNotify.Visible = true;
    
                ProgressReporter = new Progress<Web.Workflow.ProgressResult>();
                ProgressReporter.ProgressChanged += ProgressReporter_ProgressChanged;
            }

    2. NotifyProxy 类里边,定义了一些有关“显示提示消息”的方法,比如

    public static void Hello()
    {
        mainNotify.ShowBalloonTip(DefaultShownTimeout, Application.ProductName, "Hello, I'm here!", ToolTipIcon.Info);
    }
    
    public static void GoodBye()
    {
        mainNotify.ShowBalloonTip(DefaultShownTimeout, Application.ProductName, "See u again, GoodBye!", ToolTipIcon.Info);
    }
    
    public static void SayMessage(string title,string message)
    {
        mainNotify.ShowBalloonTip(DefaultShownTimeout, title, message, ToolTipIcon.None);
    }
    
    public static void SayMessage(string title, string message,int timeout)
    {
        mainNotify.ShowBalloonTip(timeout < DefaultShownTimeout ? DefaultShownTimeout : timeout, title, message, ToolTipIcon.None);
    }
    
    public static void SayError(string title,string message)
    {
        mainNotify.ShowBalloonTip(DefaultShownTimeout, title, message, ToolTipIcon.Error);
    }

    3. NotifyProxy 类里边,给托盘对象定义了几个功能菜单,分别是

    /// <summary>
    /// 初始化菜单
    /// </summary>
    private static void InitContextMenu()
    {
        if (mainNotifyMenu != null)
        {
            foreach (ToolStripMenuItem item in mainNotifyMenu.Items)
            { item.Dispose(); }
            mainNotifyMenu.Dispose();
        }
    
        mainNotifyMenu = new ContextMenuStrip();
    
        var settingMenu = new ToolStripMenuItem("Settings");
        settingMenu.Click += SettingMenu_Click;
    
        var browseMenu = new ToolStripMenuItem("Browser");
        browseMenu.Click += BrowseMenu_Click;
    
        var exploreMenu = new ToolStripMenuItem("Explore");
        exploreMenu.Click += ExploreMenu_Click;
    
        var spliter1Menu = new ToolStripMenuItem("-");
    
        var exitMenu = new ToolStripMenuItem("Exit");
        exitMenu.Click += ExitMenu_Click;
    
        mainNotifyMenu.Items.AddRange(new[] { settingMenu, browseMenu, exploreMenu, spliter1Menu, exitMenu });
    }

    4. 我显示消息的部分代码也很简单,就是调用了类中 SayMessage() 或是 SayError() 这样的方法来显示消息,比如

    private async static void ExploreMenu_Click(object sender, EventArgs e)
    {
        if (ExploreRun)
        {
            var title = "Explore Say";
            var message = "Explore already running, no news is good news";
            SayMessage(title, message, 0);
    
            return;
        }
        else
        {
            ExploreRun = true; 
        }
    
        try
        {
            BLL.ExploreHelper.CheckConfigure();
        }
        catch(ArgumentException ex) {
    
            var message = string.Format("Missing parameter {0}", ex.ParamName);
            message += Environment.NewLine;
            message += "Please check parameters again in setting menu";
            var title = "Missing Parameter";
    
            SayError(title, message, 0);
            return;
        }
                
        Web.Workflow.Entry.SetDbParameters(
            RuntimeConfigure.DbDataSource,
            RuntimeConfigure.DbInitialCatalog,
            RuntimeConfigure.DbIntegratedSecurity,
            RuntimeConfigure.DbUserName ?? string.Empty,
            RuntimeConfigure.DbPassword ?? string.Empty);
    
        Web.Workflow.Entry.SetWebParameters(
            RuntimeConfigure.HomePage, RuntimeConfigure.WebUserName, RuntimeConfigure.WebPassword);
    
        // 1. 登录网站
        bool logged = false;
        try
        {
            logged = await Web.Workflow.Entry.LoginWeb();
        }
        catch(Exception ex)
        {
            var message = string.Format("Error occurred in explore operation, the infomation is {0}", ex.Message);
            var title = "Explore Error";
    
            SayError(title, message, 0);
            return;
        }
    
        if (logged)
        {
            // detect all section from home page
            var taskDetectSetion =Web.Workflow.Entry.DetectAllSection();
            var sections = await taskDetectSetion;
            var form = new SelectTargetSection();
            form.SectionSource = sections;
            var dr = form.ShowDialog();
            {
                if (dr == DialogResult.OK)
                {
                    MessageBox.Show("暂未实现后续开始工作流程的代码");
                    return;
                }
                else {
                    var message = string.Format("You made Explore canceled, see u later");
                    var title = "Explore Canceled";
    
                    SayWarning(title, message, 0);
                    return;
                }
            }
        }
        else
        {
            var message = string.Format("Web Login failed, check your user account and try again laster");
            var title = "Explore Failed";
    
            SayWarning(title, message, 0);
            return;
        }
    }
    5. 可以看到,对托盘对象的调用,都是获得结果之后,只是作为显示“工作进度”的一个手段。我的问题集中在调用 notifyIcon 对象——也就是 NotifyProxy 中的唯一实例【mainNotify】,来显示消息时,(Win10)系统下消息停留时间较长,下一条想显示的消息必须等到上一条消失掉才能出现。我不知道如何才能做到有新消息要显示时,上一条如果还没自动消息,就立刻被新消息替换掉。


    不重要的其实最重要

    2016年8月27日 17:24