42

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

我应该怎么做才能调试它,因为它没有抛出错误并且没有显示气球?

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

6 回答 6

48

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

于 2012-11-14T05:04:13.540 回答
35

马修发现了这个问题,但我仍然努力将所有部分放在一起。所以我认为一个在 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();
于 2016-01-22T21:40:32.013 回答
2

请参阅下面的源代码。

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....");
        }
    }
}
于 2013-09-11T08:47:39.313 回答
2

为了未来的编码员:

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

请参阅:C# NotifyIcon 显示气球参数已弃用

因此,您不妨将 0 放入 > Windows Vista 的参数中。更糟糕的是,对链接答案的评论表明,这些气球的替代品,吐司通知,仅在 Windows 8 中引入。因此,对于可怜的旧 Windows 7 落在两个凳子之间,Vista < 7 < 8,我们似乎处于不管 Windows 想把那个气球放在那里多长时间都行不通!我注意到它最终确实会消失,但是经过一些经验测试后,我很确定该参数确实被忽略了。

因此,在上述答案的基础上,特别是采用@jlmt 在评论中建议的 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。
  • 我特意在一个函数中定义了处理,这样我就可以调试并验证断点确实被命中了。
于 2019-04-05T18:56:05.463 回答
1

ShowBalloonnTip 采用毫秒数。3 毫秒可能太快了,你甚至看不到。尝试更多类似 3000 的东西

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

通知图标不显示

于 2012-11-14T04:33:01.743 回答
0

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

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

于 2012-11-14T04:34:03.043 回答