Frage

I am trying to identify which BalloonTip (NotifyIcon) sent the BalloonTipClicked (and Closed, and Shown) events as I have a few different scenarios where balloons may be shown and they aren't all the same nor will have the same expected actions.

Does anyone know if you can identify anything about the BalloonTip that send the Clicked/Closed/Shown events?

War es hilfreich?

Lösung 2

It really does seem like this is a tough cookie to crack. Thanks to @keyboardP for his suggestion as it is a viable alternative, but I ended up creating a special method and enum (as SO didn't email me when I got answers or comments ... I need to check my prefs ...):

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);
}

... and the enum:

[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
}

Between the lambdas and the enum, I can make this as extensible as I like. I know it's not really the prettiest solution, but it works; surprisingly well!

Andere Tipps

It's a bit of a workaround but you could create a method (or an extension method)

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);
}

When you call the BalloonTip with that method, it will update the properties of the NotifyIcon.

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

These properties can then be read in any of the BalloonTip events. You can decide what to do based on one of the properties (e.g. 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;
            //...
        }
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top