让FX1.1的NotifyIcon支持BalloonTip(2)
??? 在這個文章的(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控件,使用方便,價格公道,童叟無欺。
總結
以上是生活随笔為你收集整理的让FX1.1的NotifyIcon支持BalloonTip(2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 作幼儿教育软件的感受(2005-05-0
- 下一篇: Window 2000 网络操作命令全释