【问题标题】:BalloonTipClicked (Closed/Shown) Sender/EventArgsBalloonTipClicked(关闭/显示)发件人/EventArgs
【发布时间】:2013-07-03 13:07:29
【问题描述】:

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

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

【问题讨论】:

  • 标题中的标签是多余的,请勿添加。
  • 不知道我在标题中添加了标签...抱歉

标签: c# balloon-tip


【解决方案1】:

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

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;
            //...
        }
    }
}

【讨论】:

  • 为代码干杯!这与我使用的有点不同(我将在此评论之后将其作为答案发布)
【解决方案2】:

这看起来确实很难破解。感谢@keyboardP 的建议,因为它是一个可行的替代方案,但我最终创建了一个特殊的方法和枚举(因为当我得到答案或 cmets 时 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 之间,我可以根据自己的喜好使其具有可扩展性。 我知道这并不是最漂亮的解决方案,但它确实有效;出奇的好!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    相关资源
    最近更新 更多