显示气球通知

时间:2012-11-14 04:30:26

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

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

我应该怎么做才能调试它,因为它不会抛出错误而不显示气球?

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

6 个答案:

答案 0 :(得分:44)

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

答案 1 :(得分:23)

马修确定了这个问题,但我仍然努力将所有部分放在一起。所以我认为一个在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();

答案 2 :(得分: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....");
        }
    }
}

答案 3 :(得分:1)

ShowBalloonnTip取毫秒数。 3毫秒可能太快,你甚至看不到。尝试更像3000的东西

您可能需要将组件模型传递给构造函数。这是我在所有例子中看到的。对不起已经很久没用过了。请参阅第一个答案:

NotifyIcon not showing

答案 4 :(得分:0)

看一下http://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.aspx

中的示例

我发现你的代码之间存在一些明显的差异,你会遗漏许多内容,例如创建ComponentModelContainer并将其传递到NotifyIcon的构造函数中

答案 5 :(得分:0)

为了将来的编码员:

  

从Windows Vista开始,[timeout]参数已弃用

请参阅:C# NotifyIcon Show Balloon Parameter Deprecated

因此,您最好将> Windows Vista的参数输入0。更糟糕的是,对链接的答案的评论表明,仅在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。
  • 我故意在函数中定义了处置方式,以便我可以调试并验证断点确实被击中。