簡體   English   中英

顯示氣球通知

[英]Show a Balloon notification

我正在嘗試使用以下代碼顯示氣球通知。 我已經驗證了它是通過使用斷點執行的。 它也沒有顯示錯誤。

我應該做些什么來調試它,因為它不會引發錯誤並且不顯示氣球?

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

您實際上尚未指定要在任務欄中顯示的圖標。 通過在ShowBalloonTip調用之前簡單地添加notifyIcon.Icon = SystemIcons.Application在LINQPad中運行代碼,我就可以顯示提示。 另請注意,在完成NotifyIcon實例后,應調用Dispose

馬修(Matthew)指出了問題所在,但我仍在努力將所有內容放在一起。 因此,我認為一個可以直接在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();

請參見下面的源代碼。

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

ShowBalloonnTip需要毫秒數。 3毫秒可能太快了,您甚至看不到。 嘗試更多類似3000

您可能需要將組件模型傳遞給構造器。 這是我在所有示例中看到的。 對不起,我已經使用了很長時間。 在這里看到第一個答案:

NotifyIcon未顯示

在這里查看示例http://msdn.microsoft.com/zh-cn/library/system.windows.forms.notifyicon.aspx

我看到它與您的代碼之間存在一些明顯的區別,您遺漏了很多內容,例如創建ComponentModelContainer並將其傳遞給NotifyIcon的構造函數。

為了將來的編碼員:

從Windows Vista開始不推薦使用[timeout]參數

請參見: C#NotifyIcon顯示不建議使用氣球參數

因此,最好將> 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。
  • 我特意在函數中定義了處置方式,以便我可以調試和驗證斷點確實被擊中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM