在这个文章的(1)中,我本来打算完全自己实现一个支持Balloon Tip的NotifyIcon控件。后来发现实现NotifyIcon控件的大量代码都纠缠在事件的处理和包装上面,太没有写头了,简直就像打劫一样没有技术含量了。于是干脆一不做二不休,就用NotifyIcon Reflect出来的代码做基类来实现支持Balloon Tip得了。
在这个文章的(1)中,我本来打算完全自己实现一个支持Balloon Tip的NotifyIcon控件。后来发现实现NotifyIcon控件的大量代码都纠缠在事件的处理和包装上面,太没有写头了,简直就像打劫一样没有技术含量了
。于是干脆一不做二不休,就用NotifyIcon Reflect出来的代码做基类来实现支持Balloon Tip得了。
于是实现一个NotifyIconEx类继承至重新编译的NotifyIcon类,新的NotifyIcon只做了利于被继承的非常少量的修改。目前除了事件处理外,只添加了一个ShowBalloonTip方法,并重载了一下WndProc方法,至于BalloonTipTitle、BalloonTipMessage和BalloonTipIcon以及Timeout的属性支持都没有加,因为要加上也非常的容易了。
派生类NotifyIconEx的代码如下:
using System;
![](/Images/OutliningIndicators/None.gif)
namespace Birdshome
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Summary description for NotifyIconEx.
/// </summary>
public class NotifyIconEx : NotifyIcon
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private const int WM_BALLOONTIPSHOWN = 0x0402;
private const int WM_BALLOONTIPCLOSING = 0x0403;
private const int WM_BALLOONTIPCLOSED = 0x0404;
private const int WM_BALLOONTIPCLICKED = 0x0405;
![](/Images/OutliningIndicators/InBlock.gif)
private static readonly object EVENT_BALLOONTIPSHOWN;
private static readonly object EVENT_BALLOONTIPCLOSED;
private static readonly object EVENT_BALLOONTIPCLICKED;
![](/Images/OutliningIndicators/InBlock.gif)
static NotifyIconEx()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
NotifyIconEx.EVENT_BALLOONTIPSHOWN = new object();
NotifyIconEx.EVENT_BALLOONTIPCLOSED = new object();
NotifyIconEx.EVENT_BALLOONTIPCLICKED = new object();
}
![](/Images/OutliningIndicators/InBlock.gif)
public NotifyIconEx()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// TODO: Add constructor logic here
//
}
![](/Images/OutliningIndicators/InBlock.gif)
public event EventHandler BalloonTipShown
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
add
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Events.AddHandler(NotifyIconEx.EVENT_BALLOONTIPSHOWN, value);
}
remove
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Events.RemoveHandler(NotifyIconEx.EVENT_BALLOONTIPSHOWN, value);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public event EventHandler BalloonTipClosed
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
add
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Events.AddHandler(NotifyIconEx.EVENT_BALLOONTIPCLOSED, value);
}
remove
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Events.RemoveHandler(NotifyIconEx.EVENT_BALLOONTIPCLOSED, value);
}
}
public event EventHandler BalloonTipClicked
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
add
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Events.AddHandler(NotifyIconEx.EVENT_BALLOONTIPCLICKED, value);
}
remove
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Events.RemoveHandler(NotifyIconEx.EVENT_BALLOONTIPCLICKED, value);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public void ShowBalloonTip(InfoIcon infoIcon, string title, string message, uint timeout)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
nid.uFlags = 0x0010;
nid.dwInfoFlags = (int)infoIcon;
nid.szInfo = message;
nid.szInfoTitle = title;
nid.uTimeoutOrVersion = timeout;
base.UpdateIcon(true);
}
![](/Images/OutliningIndicators/InBlock.gif)
private void OnBalloonTipShown()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EventHandler handler = (EventHandler) base.Events[NotifyIconEx.EVENT_BALLOONTIPSHOWN];
if (handler != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
handler(this, EventArgs.Empty);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
private void OnBalloonTipClosed()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EventHandler handler = (EventHandler) base.Events[NotifyIconEx.EVENT_BALLOONTIPCLOSED];
if (handler != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
handler(this, EventArgs.Empty);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
private void OnBalloonTipClicked()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EventHandler handler = (EventHandler) base.Events[NotifyIconEx.EVENT_BALLOONTIPCLICKED];
if (handler != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
handler(this, EventArgs.Empty);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
protected override void WndProc(ref System.Windows.Forms.Message msg)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int msgId = msg.Msg;
if ( msgId == 0x111 )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch((int)msg.LParam)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case WM_BALLOONTIPSHOWN:
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.OnBalloonTipShown();
break;
}
case WM_BALLOONTIPCLOSING:
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.OnBalloonTipClosed();
break;
}
case WM_BALLOONTIPCLOSED:
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.OnBalloonTipClosed();
break;
}
case WM_BALLOONTIPCLICKED:
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.OnBalloonTipClicked();
break;
}
}
}
base.WndProc (ref msg);
}
}
}
为了尽可能的利用原来的NotifyIcon中的代码,不做太大的改动。新的NotifyIcon中修改了UpdateIcon方法中uFlags的管理。原来的代码是在调用UpdateIcon时给uFlags赋值为0x0001(即:NIF_MESSAGE),然后再通过一些判断通过|操作加入新的flag。现在把第一次赋值改为了:uFlags|=0x0001,目的是为了把ShowBalloonTip中对uFlags的赋值传递进取。但是如果在显示了Balloon Tip后,uFlags中仍然保持了0x0010(即:NIF_INFO)标志位,那么只要NotifyIcon中移执行UpdateIcon就会再次显示Balloon Tip
。所以在UpdateIcon方法的最后,我们清除uFlags中的0x0010标识位,让uFlag ^= 0x0010;,就这样简单NotifyIcon即改造完毕。
新鲜出炉的NotifyIcon控件,使用方便,价格公道,童叟无欺
。