。于是幹脆一不做二不休,就用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
public event EventHandler BalloonTipClosed
public event EventHandler BalloonTipClicked
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()
private void OnBalloonTipClosed()
private void OnBalloonTipClicked()
protected override void WndProc(ref System.Windows.Forms.Message 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控件,使用友善,價格公道,童叟無欺
。
本文轉自部落格園鳥食軒的部落格,原文連結:http://www.cnblogs.com/birdshome/,如需轉載請自行聯系原部落客。