在这个文章的(1)中,我本来打算完全自己实现一个支持Balloon Tip的NotifyIcon控件。后来发现实现NotifyIcon控件的大量代码都纠缠在事件的处理和包装上面,太没有写头了,简直就像打劫一样没有技术含量了。于是干脆一不做二不休,就用NotifyIcon Reflect出来的代码做基类来实现支持Balloon Tip得了。
于是实现一个NotifyIconEx类继承至重新编译的NotifyIcon类,新的NotifyIcon只做了利于被继承的非常少量的修改。目前除了事件处理外,只添加了一个ShowBalloonTip方法,并重载了一下WndProc方法,至于BalloonTipTitle、BalloonTipMessage和BalloonTipIcon以及Timeout的属性支持都没有加,因为要加上也非常的容易了。
派生类NotifyIconEx的代码如下:
namespace Birdshome
{
/**//// <summary>
/// Summary description for NotifyIconEx.
/// </summary>
public class NotifyIconEx : NotifyIcon
{
private const int WM_BALLOONTIPSHOWN = 0x0402;
private const int WM_BALLOONTIPCLOSING = 0x0403;
private const int WM_BALLOONTIPCLOSED = 0x0404;
private const int WM_BALLOONTIPCLICKED = 0x0405;
private static readonly object EVENT_BALLOONTIPSHOWN;
private static readonly object EVENT_BALLOONTIPCLOSED;
private static readonly object EVENT_BALLOONTIPCLICKED;
static NotifyIconEx()
{
NotifyIconEx.EVENT_BALLOONTIPSHOWN = new object();
NotifyIconEx.EVENT_BALLOONTIPCLOSED = new object();
NotifyIconEx.EVENT_BALLOONTIPCLICKED = new object();
}
public NotifyIconEx()
{
//
// TODO: Add constructor logic here
//
}
public event EventHandler BalloonTipShown
{
add
{
this.Events.AddHandler(NotifyIconEx.EVENT_BALLOONTIPSHOWN, value);
}
remove
{
this.Events.RemoveHandler(NotifyIconEx.EVENT_BALLOONTIPSHOWN, value);
}
}
public event EventHandler BalloonTipClosed
{
add
{
this.Events.AddHandler(NotifyIconEx.EVENT_BALLOONTIPCLOSED, value);
}
remove
{
this.Events.RemoveHandler(NotifyIconEx.EVENT_BALLOONTIPCLOSED, value);
}
}
public event EventHandler BalloonTipClicked
{
add
{
this.Events.AddHandler(NotifyIconEx.EVENT_BALLOONTIPCLICKED, value);
}
remove
{
this.Events.RemoveHandler(NotifyIconEx.EVENT_BALLOONTIPCLICKED, value);
}
}
public void ShowBalloonTip(InfoIcon infoIcon, string title, string message, uint timeout)
{
nid.uFlags = 0x0010;
nid.dwInfoFlags = (int)infoIcon;
nid.szInfo = message;
nid.szInfoTitle = title;
nid.uTimeoutOrVersion = timeout;
base.UpdateIcon(true);
}
private void OnBalloonTipShown()
{
EventHandler handler = (EventHandler) base.Events[NotifyIconEx.EVENT_BALLOONTIPSHOWN];
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
private void OnBalloonTipClosed()
{
EventHandler handler = (EventHandler) base.Events[NotifyIconEx.EVENT_BALLOONTIPCLOSED];
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
private void OnBalloonTipClicked()
{
EventHandler handler = (EventHandler) base.Events[NotifyIconEx.EVENT_BALLOONTIPCLICKED];
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
protected override void WndProc(ref System.Windows.Forms.Message msg)
{
int msgId = msg.Msg;
if ( msgId == 0x111 )
{
switch((int)msg.LParam)
{
case WM_BALLOONTIPSHOWN:
{
this.OnBalloonTipShown();
break;
}
case WM_BALLOONTIPCLOSING:
{
this.OnBalloonTipClosed();
break;
}
case WM_BALLOONTIPCLOSED:
{
this.OnBalloonTipClosed();
break;
}
case WM_BALLOONTIPCLICKED:
{
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控件,使用方便,价格公道,童叟无欺。