组合 NotifyIcon 和 ToolTip

发布于 2024-08-24 23:29:33 字数 550 浏览 6 评论 0原文

我一直在使用 NotifyIcon 以便在任务栏中显示图标。 该程序没有 Windows 窗体。我也许可以创建一个并使其不可见,但我希望避免它。 NotifyIcon 附加的 ToolTip 功能有些缺乏,这里的一位专家建议我看看 ToolTip 功能。可以将工具提示附加到表单。是否可以将其附加到 NotifyIcon?我正在尝试这样做:

NotifyIcon CTicon = new NotifyIcon();
ToolTip toolTip = new ToolTip();
toolTip.SetToolTip(CTicon, "Test");

我收到错误“无法从‘System.Windows.Forms.NotifyIcon’转换为‘System.Windows.Forms.Control’。有没有办法转换?我也尝试过:

toolTip.SetToolTip(CTicon.Container, "Test");

但是一个容器显然也不是有效的控制。 对于我完全不了解这可能或可能不起作用的情况,我深表歉意。

提前致谢。

I have been working with NotifyIcon in order to show an icon in the taskbar.
This program has no Windows Form. I perhaps could create one and make it invisible but I was hoping to avoid it. The ToolTip functions attached to NotifyIcon are somewhat lacking, and one of the gurus here suggested I look at the ToolTip functionality. It is possible to attach ToolTip to a form. Is is possible to attach it to just the NotifyIcon? I'm trying do this:

NotifyIcon CTicon = new NotifyIcon();
ToolTip toolTip = new ToolTip();
toolTip.SetToolTip(CTicon, "Test");

And I get the error "cannot convert from 'System.Windows.Forms.NotifyIcon' to 'System.Windows.Forms.Control'. Is there a way to convert? I also tried:

toolTip.SetToolTip(CTicon.Container, "Test");

but a container is apparently not a valid control either.
I apologize for my total lack of understanding of how this may or may not work.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

尘世孤行 2024-08-31 23:29:33

一个迟来的答案,但可能对其他人有用。

NotifyIcon.Text = "ToolTipText";

A belated answer, but maybe useful for others .

NotifyIcon.Text = "ToolTipText";
帝王念 2024-08-31 23:29:33

托盘图标不支持方形工具提示,仅支持气球。有点道理,图标通常非常靠近,因此很难看出哪个图标产生了气球上没有“茎”的尖端。使用 NotifyIcon.BalloonTipText 属性。

Tray icons don't support square tool tips, only balloons. Kinda makes sense, the icons are usually quite close together so it would be hard to see what icon produced the tip without the "stem" on the balloon. Use the NotifyIcon.BalloonTipText property.

前事休说 2024-08-31 23:29:33

NotifyIcon 用于您在屏幕右下角看到的系统托盘图标,ToolTip 的使用仅适用于文本框、复选框等控件...例如,假设有一个名为 ' 的 TextBox 实例textBox1',在表单上比这有效:

toolTip1.SetToolTip(textBox1, "Hello World");

现在,当您将鼠标悬停在文本框上时,会显示一个工具提示...

NotifyIcon is used for the system tray icon that you see on the bottom right hand corner of the screen, usage of ToolTip is only for controls such as textboxes, checkboxes and so on...for example, let's assume there's a TextBox instance called 'textBox1', on the form than this would work:

toolTip1.SetToolTip(textBox1, "Hello World");

Now, when you mouse-over the textbox, a tooltip is shown...

走过海棠暮 2024-08-31 23:29:33

我不确定您是否可以直接在通知图标上设置工具提示。这与在通知图标本身上设置文本属性是一样的。通知图标文本有一些限制。它的长度限制为 128 个字符,并且只会保留很短的时间。如果您想在更长的时间内显示更多信息,您应该查看通知图标的气球文本属性。我强烈建议您阅读 MSDN 页面,它非常有帮助。

http://msdn.microsoft.com/en-我们/library/system.windows.forms.notifyicon.aspx

I'm not sure you can set a tooltip directly on a notify icon. It's the same thing as setting the text property on the notify icon itself. There are some limitations to the notify icon text. It's limited to 128 chars and will only stay up for a short amount of time. If you want to display more info for a longer amount of time you should look at the balloon text property of the notify icon. I highly suggest reading the MSDN page it's quite helpful.

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

咋地 2024-08-31 23:29:33

你不应该。

NotifyIcon 用于显示通知,而 ToolTip 用于显示有关用户当前活动的信息,它应该“就地”使用。

检查用户界面指南:

  1. 通知
  2. 气球

You shouldn't.

NotifyIcon is used to show notifications, whereas ToolTip is used to show the information about the user's current activity, it should be used "in place".

Check the user interface guidelines:

  1. Notifications
  2. Balloons
稚然 2024-08-31 23:29:33

我计算机上的所有托盘图标都有工具提示。您需要使用接受 Component 作为参数的构造函数来创建 NotifyIcon。它显示 NotifyIcon.Text 属性。

我能够使用此处的示例代码创建一个:
http://msdn.microsoft.com/en-us/library/1by05f8d.aspx

using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : System.Windows.Forms.Form
{
    private System.Windows.Forms.NotifyIcon notifyIcon1;
    private System.Windows.Forms.ContextMenu contextMenu1;
    private System.Windows.Forms.MenuItem menuItem1;
    private System.ComponentModel.IContainer components;

    [STAThread]
    static void Main() 
    {
        Application.Run(new Form1());
    }

    public Form1()
    {
        this.components = new System.ComponentModel.Container();
        this.contextMenu1 = new System.Windows.Forms.ContextMenu();
        this.menuItem1 = new System.Windows.Forms.MenuItem();

        // Initialize contextMenu1 
        this.contextMenu1.MenuItems.AddRange(
                    new System.Windows.Forms.MenuItem[] {this.menuItem1});

        // Initialize menuItem1 
        this.menuItem1.Index = 0;
        this.menuItem1.Text = "E&xit";
        this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);

        // Set up how the form should be displayed. 
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Text = "Notify Icon Example";

        // Create the NotifyIcon. 
        this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);

        // The Icon property sets the icon that will appear 
        // in the systray for this application.
        notifyIcon1.Icon = new Icon("appicon.ico");

        // The ContextMenu property sets the menu that will 
        // appear when the systray icon is right clicked.
        notifyIcon1.ContextMenu = this.contextMenu1;

        // The Text property sets the text that will be displayed, 
        // in a tooltip, when the mouse hovers over the systray icon.
        notifyIcon1.Text = "Form1 (NotifyIcon example)";
        notifyIcon1.Visible = true;

        // Handle the DoubleClick event to activate the form.
        notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);

    }

    protected override void Dispose( bool disposing )
    {
        // Clean up any components being used. 
        if( disposing )
            if (components != null)
                components.Dispose();            

        base.Dispose( disposing );
    }

    private void notifyIcon1_DoubleClick(object Sender, EventArgs e) 
    {
        // Show the form when the user double clicks on the notify icon. 

        // Set the WindowState to normal if the form is minimized. 
        if (this.WindowState == FormWindowState.Minimized)
            this.WindowState = FormWindowState.Normal;

        // Activate the form. 
        this.Activate();
    }

    private void menuItem1_Click(object Sender, EventArgs e) {
        // Close the form, which closes the application. 
        this.Close();
    }
}

All the tray icons on my computer have tooltips. You need to create your NotifyIcon using the constructor that accepts a Component as an argument. It displays the NotifyIcon.Text property.

I was able to create one using the example code here:
http://msdn.microsoft.com/en-us/library/1by05f8d.aspx

using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : System.Windows.Forms.Form
{
    private System.Windows.Forms.NotifyIcon notifyIcon1;
    private System.Windows.Forms.ContextMenu contextMenu1;
    private System.Windows.Forms.MenuItem menuItem1;
    private System.ComponentModel.IContainer components;

    [STAThread]
    static void Main() 
    {
        Application.Run(new Form1());
    }

    public Form1()
    {
        this.components = new System.ComponentModel.Container();
        this.contextMenu1 = new System.Windows.Forms.ContextMenu();
        this.menuItem1 = new System.Windows.Forms.MenuItem();

        // Initialize contextMenu1 
        this.contextMenu1.MenuItems.AddRange(
                    new System.Windows.Forms.MenuItem[] {this.menuItem1});

        // Initialize menuItem1 
        this.menuItem1.Index = 0;
        this.menuItem1.Text = "E&xit";
        this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);

        // Set up how the form should be displayed. 
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Text = "Notify Icon Example";

        // Create the NotifyIcon. 
        this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);

        // The Icon property sets the icon that will appear 
        // in the systray for this application.
        notifyIcon1.Icon = new Icon("appicon.ico");

        // The ContextMenu property sets the menu that will 
        // appear when the systray icon is right clicked.
        notifyIcon1.ContextMenu = this.contextMenu1;

        // The Text property sets the text that will be displayed, 
        // in a tooltip, when the mouse hovers over the systray icon.
        notifyIcon1.Text = "Form1 (NotifyIcon example)";
        notifyIcon1.Visible = true;

        // Handle the DoubleClick event to activate the form.
        notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);

    }

    protected override void Dispose( bool disposing )
    {
        // Clean up any components being used. 
        if( disposing )
            if (components != null)
                components.Dispose();            

        base.Dispose( disposing );
    }

    private void notifyIcon1_DoubleClick(object Sender, EventArgs e) 
    {
        // Show the form when the user double clicks on the notify icon. 

        // Set the WindowState to normal if the form is minimized. 
        if (this.WindowState == FormWindowState.Minimized)
            this.WindowState = FormWindowState.Normal;

        // Activate the form. 
        this.Activate();
    }

    private void menuItem1_Click(object Sender, EventArgs e) {
        // Close the form, which closes the application. 
        this.Close();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文