在这个文章的(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;

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控件,使用方便,价格公道,童叟无欺
。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器