2

我正在尝试确定哪个 BalloonTip(NotifyIcon)发送了 BalloonTipClicked(和 Closed 和 Shown)事件,因为我有几个不同的场景,其中可能会显示气球,它们并不完全相同,也不会有相同的预期动作。

有谁知道您是否可以识别有关发送 Clicked/Closed/Shown 事件的 BalloonTip 的任何信息?

4

2 回答 2

3

这是一种解决方法,但您可以创建一个方法(或扩展方法)

public static void ShowBalloonAndUpdate(this NotifyIcon ni, int timeout, string title, string text, ToolTipIcon icon )
{
    ni.BalloonTipTitle = title;
    ni.BalloonTipText = text;
    ni.BalloonTipIcon = icon;
    ni.ShowBalloonTip(timeout);
}

当您使用该方法调用 BalloonTip 时,它将更新 NotifyIcon 的属性。

myNotifyIcon.ShowBalloonAndUpdate(1000, "Hello" "My Message", ToolTipIcon.Info);

然后可以在任何 BalloonTip 事件中读取这些属性。您可以根据其中一个属性决定要做什么(例如BalloonTitle

private void myNotifyIcon_BalloonTipShown(Object sender, EventArgs e) 
{
    NotifyIcon ni = sender as NotifyIcon;
    if(ni != null)   
    {
        switch(ni.BalloonTitle)
        {
            case "Hello":
                  //Hello tooltip was shown
                  break;
            //...
        }
    }
}
于 2013-07-03T13:19:28.797 回答
1

看起来这确实是一块很难破解的饼干。感谢@keyboardP 的建议,因为它是一个可行的替代方案,但我最终创建了一个特殊的方法和枚举(因为当我得到答案或评论时,SO 没有给我发电子邮件......我需要检查我的偏好...... ):

internal static void ShowTip(Int32 timeout, String tipTitle, String tipText, ToolTipIcon tipIcon = ToolTipIcon.None, ShowTipAction tipAction = ShowTipAction.STA_Shown_WriteReg, String linkToOpen = "")
{
    if ((tipAction & ShowTipAction.STA_Nothing) == 0) // if STA_Nothing has not been passed
    {
        if ((tipAction & ShowTipAction.STA_Clicked_Nothing) == 0 && ((tipAction & ShowTipAction.STA_Clicked_OpenLink) > 0 || (tipAction & ShowTipAction.STA_Clicked_WriteReg) > 0))
            trayIcon.BalloonTipClicked += (s, e) => // if STA_Clicked_Nothing has not been passed and either STA_Clicked_OpenLink or STA_Clicked_WriteReg has been passed
            { // when this balloon tip is clicked
                if ((tipAction & ShowTipAction.STA_Clicked_OpenLink) > 0) // open passed link
                    MethodorProcessToLaunchSite(linktoOpen);
                if ((tipAction & ShowTipAction.STA_Clicked_WriteReg) > 0) // write notification indicator to registry
                    RegWriteMethod;
            };
        if ((tipAction & ShowTipAction.STA_Closed_Nothing) == 0 && (tipAction & ShowTipAction.STA_Closed_WriteReg) > 0) // if STA_Closed_Nothing has not been passed and STA_Closed_WriteReg has been passed
            trayIcon.BalloonTipClosed += (s, e) => { RegWriteMethod; }; // when this balloon tip is closed, write notification indicator to registry
        if ((tipAction & ShowTipAction.STA_Shown_Nothing) == 0 && (tipAction & ShowTipAction.STA_Shown_WriteReg) > 0) // if STA_Shown_Nothing has not been passed and STA_Shown_WriteReg has been passed
            trayIcon.BalloonTipShown += (s, e) => { RegWriteMethod; }; // when this balloon tip is shown, write notification indicator to registry
    }

    // Show the balloon tip
    trayIcon.ShowBalloonTip(timeout, tipTitle, tipText, tipIcon);
}

...和枚举:

[Flags]
internal enum ShowTipAction
{
    STA_Nothing = 1,
    STA_Clicked_Nothing = 2,
    STA_Clicked_OpenLink = 4,
    STA_Clicked_WriteReg = 8,
    STA_Closed_Nothing = 16,
    STA_Closed_WriteReg = 32,
    STA_Shown_Nothing = 64,
    STA_Shown_WriteReg = 128
}

在 lambdas 和 enum 之间,我可以让它随心所欲地扩展。我知道这并不是最漂亮的解决方案,但它确实有效;出奇的好!

于 2013-07-08T09:38:09.060 回答