バルーンが表示される可能性があり、それらがすべて同じではないか、同じ期待されるアクションを持たないいくつかの異なるシナリオがあるため、どの BalloonTip (NotifyIcon) が BalloonTipClicked (および Closed、および Shown) イベントを送信したかを特定しようとしています。
Clicked/Closed/Shown イベントを送信する BalloonTip について何かを特定できるかどうかは誰にもわかりませんか?
バルーンが表示される可能性があり、それらがすべて同じではないか、同じ期待されるアクションを持たないいくつかの異なるシナリオがあるため、どの BalloonTip (NotifyIcon) が BalloonTipClicked (および Closed、および Shown) イベントを送信したかを特定しようとしています。
Clicked/Closed/Shown イベントを送信する BalloonTip について何かを特定できるかどうかは誰にもわかりませんか?
これはちょっとした回避策ですが、メソッド (または拡張メソッド) を作成できます。
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 イベントで読み取ることができます。プロパティの 1 つに基づいて何を行うかを決定できます (例: 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;
//...
}
}
}
これはクラックするのが難しいクッキーのようです。実行可能な代替手段であるため、@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
}
ラムダと列挙型の間で、これを好きなだけ拡張可能にすることができます。私はそれが本当に最も美しい解決策ではないことを知っていますが、うまくいきます。意外と元気!