Pokaż powiadomienie z balonem

Próbuję użyć poniższego kodu, aby pokazać powiadomienie balonem. Zweryfikowałem, że jest on wykonywany za pomocą punktów przerwania. Nie wykazuje też żadnych błędów.

Co powinienem zrobić, aby to debugować, ponieważ nie wyrzuca błędów i nie pokazuje balonu?

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);
}
Author: abatishchev, 2012-11-14

5 answers

Nie podałeś ikony do wyświetlenia na pasku zadań. Uruchamiając kod w LINQPad, po prostu dodając notifyIcon.Icon = SystemIcons.Application przed wywołaniem do ShowBalloonTip udało mi się wyświetlić końcówkę. Zauważ również, że powinieneś wywołać Dispose, Gdy skończysz ze swoją instancją NotifyIcon.

 42
Author: Matthew Ferreira,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-14 05:04:13

Matthew zidentyfikował problem, ale nadal starałem się złożyć wszystkie kawałki razem. Pomyślałem więc, że zwięzły przykład, który działa w LINQPad as-is, będzie pomocny (i prawdopodobnie gdzie indziej). Wystarczy odwołać się do Zgromadzenia System.Windows.Forms i wkleić ten kod.

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();
 17
Author: Jeremy Cook,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-20 10:12:59

Patrz poniższy kod źródłowy.

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....");
        }
    }
}
 2
Author: kombsh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-11 16:38:20

ShowBalloonnTip pobiera liczbę milisekund. 3 milisekundy mogą być zbyt szybkie, aby nawet zobaczyć. Spróbuj czegoś bardziej jak 3000

Może być konieczne przekazanie modelu komponentu do konstruktora. To jest to, co widzę we wszystkich przykładach. Dawno go nie używałem. Zobacz pierwszą odpowiedź tutaj:

NotifyIcon not showing

 1
Author: AaronLS,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:02:53

Spójrz na przykład tutaj http://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.aspx

Widzę pewne wyraźne różnice między nim a Twoim kodem, jest wiele elementów, które pomijasz, takich jak tworzenie ComponentModelContainer i przekazywanie go do konstruktora NotifyIcon.

 0
Author: evanmcdonnal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-14 06:02:47