2010-03-11 100 views
12

我一直在使用NotifyIcon来显示任务栏中的图标。 此程序没有Windows窗体。我也许可以创造一个,让它看不见,但我希望避免它。 NotifyIcon附带的ToolTip函数有些缺乏,这里的一位专家建议我看一下ToolTip功能。可以将ToolTip附加到表单上。可以将它附加到NotifyIcon上吗?我试着这样做:Combine 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"); 

但容器显然不是一个有效的控制或者 我为我的完全缺乏的是如何这可能会或可能无法正常工作懂得道歉

预先感谢

+1

NotifyIcons和系统托盘似乎是在有出路在Windows 7中,按钮和菜单工具栏预览上有路相反。您可能想要考虑新软件的这一点。 – 2011-03-25 23:46:37

回答

1

NotifyIcon用于您在屏幕右下角看到的系统托盘图标,ToolTip的用途仅用于控件,如文本框,复选框等......例如,假设有一个TextBox实例所谓的“textBox1的”,形式大于该上会的工作:

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

现在,当你将鼠标悬停在文本框,显示工具提示...

1

我不知道,你可以设置一个工具提示直接通知图标。这与在通知图标本身上设置文本属性是一回事。通知图标文本有一些限制。它仅限于128个字符,只能保持很短的时间。如果您想要显示更长时间的更多信息,则应查看通知图标的气球文本属性。我强烈建议阅读MSDN页面,这很有帮助。

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

+0

*注意:根据上面提供的链接下的文本文档,Text限制为63个字符(请参阅“例外”部分):http://msdn.microsoft.com/zh-cn/library/system.windows .forms.notifyicon.text.aspx – 2012-02-24 00:59:29

3

托盘图标不支持多工具提示,只气球。有点有道理,图标通常非常接近,所以很难看到什么图标产生的提示没有“干”气球。使用NotifyIcon.BalloonTipText属性。

1

你不应该。

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

检查用户界面指导原则:

  1. Notifications
  2. Balloons
+1

这就是理论。过去十年左右的做法是,通知图标实际上不是用于通知,而是用于低调用户界面。 – 2011-05-29 23:06:07

26

迟来的答案,但对别人也许有用。

NotifyIcon.Text = "ToolTipText"; 
1

我的电脑上的所有托盘图标都有工具提示。您需要使用接受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(); 
    } 
} 
+0

一般而言,您希望将链接中的相关代码放在您的答案中,而不仅仅是包含链接。 – 2014-06-10 21:55:18