【问题标题】:Show a Balloon notification显示气球通知
【发布时间】:2012-11-14 04:30:25
【问题描述】:

我正在尝试使用以下代码来显示气球通知。我已经验证它是通过使用断点执行的。它也没有显示任何错误。

我应该如何调试它,因为它没有抛出错误并且没有显示气球?

private void showBalloon(string title, string body)
{
    NotifyIcon notifyIcon = new NotifyIcon();
    notifyIcon.Visible = true;

    if (title != null)
    {
        notifyIcon.BalloonTipTitle = title;
    }

    if (body != null)
    {
        notifyIcon.BalloonTipText = body;
    }

    notifyIcon.ShowBalloonTip(30000);
}

【问题讨论】:

    标签: c# .net winforms notifyicon notification-area


    【解决方案1】:

    您实际上并未指定要在任务栏中显示的图标。在 LINQPad 中运行您的代码,只需在调用 ShowBalloonTip 之前添加 notifyIcon.Icon = SystemIcons.Application,我就可以显示提示。另请注意,当您完成 NotifyIcon 实例后,您应该调用 Dispose

    【讨论】:

    • 我在窗口关闭/关闭时使用Dispose,否则它会一直徘徊,直到您将鼠标移到它上面。
    • @AndrewGrinder 微软打算在用户不在时继续显示信息,并且仅在他使用计算机时达到超时
    【解决方案2】:

    马修发现了这个问题,但我仍然努力将所有部分放在一起。所以我认为一个在 LINQPad 中按原样工作的简洁示例会有所帮助(并且可能在其他地方)。只需引用System.Windows.Forms 程序集,然后将这段代码粘贴进去。

    var notification = new System.Windows.Forms.NotifyIcon()
    {
        Visible = true,
        Icon = System.Drawing.SystemIcons.Information,
        // optional - BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info,
        // optional - BalloonTipTitle = "My Title",
        BalloonTipText = "My long description...",
    };
    
    // Display for 5 seconds.
    notification.ShowBalloonTip(5000);
    
    // This will let the balloon close after it's 5 second timeout
    // for demonstration purposes. Comment this out to see what happens
    // when dispose is called while a balloon is still visible.
    Thread.Sleep(10000);
    
    // The notification should be disposed when you don't need it anymore,
    // but doing so will immediately close the balloon if it's visible.
    notification.Dispose();
    

    【讨论】:

    • 这很好用,但它只能工作 5 秒,即使我已经将 ShowBaloonTip 值设为 1 分钟,虽然我已经重建了项目......我找不到原因......
    • 我为此找到了这个问题:stackoverflow.com/questions/3906531/…
    • @HQtunes.com:您的链接是ToolTip,而不是NotifyIcon。这可能与This [the timeout] parameter is deprecated as of Windows Vista. Notification display times are now based on system accessibility settings.
    • 感谢这个例子,只是注意到一种处理通知的方法 - 在 ShowBalloonTip() 之前添加这些事件处理程序似乎对我来说工作得很好,不需要线程睡眠:notification.BalloonTipClosed += (sender, args) => notification.Dispose(); 和 @987654329 @(我发现两者都是必需的,具体取决于用户是点击关闭还是让它超时)。
    【解决方案3】:

    请参阅下面的源代码。

    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.IO;
    using System.Reflection;
    using System.Windows.Forms;
    
    namespace ShowToolTip
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void btBallonToolTip_Click(object sender, EventArgs e)
            {
                ShowBalloonTip();
                this.Hide();
            }
    
            private void ShowBalloonTip()
            {
                Container bpcomponents = new Container();
                ContextMenu contextMenu1 = new ContextMenu();
    
                MenuItem runMenu = new MenuItem();
                runMenu.Index = 1;
                runMenu.Text = "Run...";
                runMenu.Click += new EventHandler(runMenu_Click);
    
                MenuItem breakMenu = new MenuItem();
                breakMenu.Index = 2;
                breakMenu.Text = "-------------";
    
                MenuItem exitMenu = new MenuItem();
                exitMenu.Index = 3;
                exitMenu.Text = "E&xit";
    
                exitMenu.Click += new EventHandler(exitMenu_Click);
    
                // Initialize contextMenu1
                contextMenu1.MenuItems.AddRange(
                            new System.Windows.Forms.MenuItem[] { runMenu, breakMenu, exitMenu });
    
                // Initialize menuItem1
    
                this.ClientSize = new System.Drawing.Size(0, 0);
                this.Text = "Ballon Tootip Example";
    
                // Create the NotifyIcon.
                NotifyIcon notifyIcon = new NotifyIcon(bpcomponents);
    
                // The Icon property sets the icon that will appear
                // in the systray for this application.
                string iconPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\setup-icon.ico";
                notifyIcon.Icon = new Icon(iconPath);
    
                // The ContextMenu property sets the menu that will
                // appear when the systray icon is right clicked.
                notifyIcon.ContextMenu = contextMenu1;
    
                notifyIcon.Visible = true;
    
                // The Text property sets the text that will be displayed,
                // in a tooltip, when the mouse hovers over the systray icon.
                notifyIcon.Text = "Morgan Tech Space BallonTip Running...";
                notifyIcon.BalloonTipText = "Morgan Tech Space BallonTip Running...";
                notifyIcon.BalloonTipTitle = "Morgan Tech Space";
                notifyIcon.ShowBalloonTip(1000);
            }
    
            void exitMenu_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            void runMenu_Click(object sender, EventArgs e)
            {
                MessageBox.Show("BallonTip is Running....");
            }
        }
    }
    

    【讨论】:

    • 实现的解释在哪里?我试图按原样运行代码,但没有任何反应。请解释如何使这段代码真正显示气球通知。
    【解决方案4】:

    为了未来的程序员:

    [timeout] 参数自 windows vista 起已弃用

    见:C# NotifyIcon Show Balloon Parameter Deprecated

    因此,您不妨将 0 放入 > Windows Vista 的参数中。更糟糕的是,链接答案中的 cmets 表明,这些气球的替代品,吐司通知,仅在 Windows 8 中引入。因此,对于落在两个凳子之间的可怜的旧 Windows 7,Vista

    因此,在上述答案的基础上,特别是采用 @jlmt 在 cmets 中建议的 lambda 函数,这是一个适用于 Windows 7 的解决方案:

    //Todo: use abstract factory pattern to detect Windows 8 and in that case use a toastnotification instead
            private void DisplayNotificationBalloon(string header, string message)
            {
                NotifyIcon notifyIcon = new NotifyIcon
                {
                    Visible = true,
                    Icon = SystemIcons.Application
                };
                if (header != null)
                {
                    notifyIcon.BalloonTipTitle = header;
                }
                if (message != null)
                {
                    notifyIcon.BalloonTipText = message;
                }
                notifyIcon.BalloonTipClosed += (sender, args) => dispose(notifyIcon);
                notifyIcon.BalloonTipClicked += (sender, args) => dispose(notifyIcon);
                notifyIcon.ShowBalloonTip(0);
            }
    
            private void dispose(NotifyIcon notifyIcon)
            {
                notifyIcon.Dispose();
            }
    

    注意事项

    • 我在里面放了一个 TODO 来为 Windows 编写另一个实现 8,因为人们现在在 Windows 7/8 上是 50/50,所以最好支持 更新的功能。我猜还有其他人为此编写多个代码 理想情况下,Windows 版本可能也应该这样做。要不就 停止支持 7 并改用 ToastNotification。
    • 我特意在函数中定义了处理,以便我可以调试并验证断点确实被命中。

    【讨论】:

      【解决方案5】:

      ShowBalloonnTip 需要毫秒数。 3 毫秒可能太快了,你甚至看不到。尝试更像 3000

      您可能需要将组件模型传递给构造函数。这是我在所有示例中看到的。抱歉很久没用了。在此处查看第一个答案:

      NotifyIcon not showing

      【讨论】:

      • 不,没有解决任何问题...是否有一些要求,例如我必须让应用程序在系统托盘中运行才能使用它?
      • @Ben 如果您没有注意到我链接了一个类似的问题并添加了另一个建议。除此之外,没有其他想法。我猜它与您的应用程序没有任何关系。换句话说,必须将控件添加到表单中的某种控件容器/集合或引用中。我怀疑这是传递给构造函数的组件模型的目的,将其连接到您的应用程序。
      【解决方案6】:

      看看这里的例子http://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.aspx

      我发现它与您的代码之间存在一些明显差异,您遗漏了许多部分,例如创建 ComponentModelContainer 并将其传递给 NotifyIcon 的构造函数。

      【讨论】:

      • 完成...它没有解决任何问题:-\
      猜你喜欢
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多