我正在尝试识别哪个BalloonTip(NotifyIcon)发送了BalloonTipClicked(以及已关闭和已显示)事件,因为我有几个不同的场景,可以显示气球,它们不是全部相同,也不会有相同的预期动作。
是否有人知道您是否可以识别发送Clicked / Closed / Shown事件的BalloonTip的任何?
答案 0 :(得分:2)
这是一种解决方法,但您可以创建方法(或扩展方法)
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;
//...
}
}
}
答案 1 :(得分: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之间,我可以根据自己的喜好进行扩展。 我知道这不是最漂亮的解决方案,但它有效;非常好!