在C#中创建气球提示

11

我可以知道如何在我的使用C#编写的应用程序中制作一个弹出气泡消息吗?

例如,当我启动我的应用程序时,它会弹出一个窗口,显示“欢迎来到UbuntuSE App”。

是的,这个弹出窗口不是消息框弹出窗口,而是托盘菜单中的弹出窗口。

类似于这样:

enter image description here

附注: 如果我没错的话,这被称为气球工具提示。但是,我该如何在我的代码中使用它呢?


在Windows 10上,它不再是一个“气球”提示:/ - Brackets
7个回答

22

如果您正在使用Winforms,您可以使用NotifyIcon类。此对象具有ShowBalloonTip方法,可显示气球提示:

var icon = new NotifyIcon();
icon.ShowBalloonTip(1000, "Balloon title", "Balloon text", ToolTipIcon.None)

4
你一定在寻找通知图标控件。
这里有一个CodeProject示例,另外还有一个完整的MSDN示例在这里。同时请查看以下图片:enter image description here

4
你可以使用.NET 2.0 System.Windows.Forms中的NotifyIcon控件。请参考:使用NotifyIcon控件。根据MSDN,NotifyIcon是指创建通知区域图标的组件。该类不能被继承。


3

如果不设置"icon"属性,弹出窗口将无法显示。

    NotifyIcon ballon = new NotifyIcon();
    ballon.Icon = SystemIcons.Application;//or any icon you like
    ballon.ShowBalloonTip(1000, "Balloon title", "Balloon text", ToolTipIcon.None)

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

1
感谢提供的信息!我做了类似的东西,确实有效!
    private void NotifyBaloon(string text, string tooltip, string title, bool show)
    {
        notifyIconMain.Text = text;
        notifyIconMain.BalloonTipText = tooltip;
        notifyIconMain.BalloonTipTitle = title;
        if (show)
            notifyIconMain.ShowBalloonTip(1000);
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接