如何在我的C#应用​​程序中创建自己的事件,例如可用的默认事件?

时间:2020-11-27 00:07:09

I needed to update the system tray icon's text value, of my application, whenever a user hovers over it. I noticed that no such event exists for system tray icons. Is it possible to create a hover event for a system tray icon and if so how can I go about accomplishing it?

每当用户将鼠标悬停在应用程序上时,我都需要更新系统托盘图标的文本值。我注意到系统托盘图标不存在此类事件。是否可以为系统托盘图标创建悬停事件,如果是,我该如何完成它?

2 个解决方案

#1


How about hooking into NotifyIcon.MouseMove?

挂钩到NotifyIcon.MouseMove怎么样?

As a basic example, this seems to work (with a NotifyIcon on a Form):

作为一个基本的例子,这似乎有效(在Form上使用NotifyIcon):

    public Form1() {
        InitializeComponent();
        notifyIcon1.MouseMove += delegate
        {
            notifyIcon1.Text = DateTime.Now.TimeOfDay.ToString();
        };
        notifyIcon1.Icon = SystemIcons.Hand;
        notifyIcon1.Visible = true;            
    }

#2


In WPF, UI Elements have a ToolTipOpening/ToolTipClosing event. You should update the tooltip text in the opening. I don't know if system tray icons have such behavior, but i guess there is something similar.

在WPF中,UI Elements具有ToolTipOpening / ToolTipClosing事件。您应该在开头更新工具提示文本。我不知道系统托盘图标是否有这样的行为,但我猜有类似的东西。

#1


How about hooking into NotifyIcon.MouseMove?

挂钩到NotifyIcon.MouseMove怎么样?

As a basic example, this seems to work (with a NotifyIcon on a Form):

作为一个基本的例子,这似乎有效(在Form上使用NotifyIcon):

    public Form1() {
        InitializeComponent();
        notifyIcon1.MouseMove += delegate
        {
            notifyIcon1.Text = DateTime.Now.TimeOfDay.ToString();
        };
        notifyIcon1.Icon = SystemIcons.Hand;
        notifyIcon1.Visible = true;            
    }

#2


In WPF, UI Elements have a ToolTipOpening/ToolTipClosing event. You should update the tooltip text in the opening. I don't know if system tray icons have such behavior, but i guess there is something similar.

在WPF中,UI Elements具有ToolTipOpening / ToolTipClosing事件。您应该在开头更新工具提示文本。我不知道系统托盘图标是否有这样的行为,但我猜有类似的东西。