让FX1.1的NotifyIcon支持BalloonTip(2)

文章讲述了实现支持Balloon Tip的NotifyIcon控件的过程。因原实现代码在事件处理和包装上无技术含量,便用Reflect出来的代码做基类。实现了NotifyIconEx类,对原NotifyIcon少量修改,添加ShowBalloonTip方法等,还调整了UpdateIcon方法中uFlags管理。

    在这个文章的(1)中,我本来打算完全自己实现一个支持Balloon Tip的NotifyIcon控件。后来发现实现NotifyIcon控件的大量代码都纠缠在事件的处理和包装上面,太没有写头了,简直就像打劫一样没有技术含量了emembarrassed.gif。于是干脆一不做二不休,就用NotifyIcon Reflect出来的代码做基类来实现支持Balloon Tip得了。

    于是实现一个NotifyIconEx类继承至重新编译的NotifyIcon类,新的NotifyIcon只做了利于被继承的非常少量的修改。目前除了事件处理外,只添加了一个ShowBalloonTip方法,并重载了一下WndProc方法,至于BalloonTipTitle、BalloonTipMessage和BalloonTipIcon以及Timeout的属性支持都没有加,因为要加上也非常的容易了。

    派生类NotifyIconEx的代码如下:

None.gif using  System;
None.gif
None.gif
namespace  Birdshome
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for NotifyIconEx.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class NotifyIconEx : NotifyIcon
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private const int WM_BALLOONTIPSHOWN = 0x0402;
InBlock.gif        
private const int WM_BALLOONTIPCLOSING = 0x0403;
InBlock.gif        
private const int WM_BALLOONTIPCLOSED = 0x0404;
InBlock.gif        
private const int WM_BALLOONTIPCLICKED = 0x0405;
InBlock.gif
InBlock.gif        
private static readonly object EVENT_BALLOONTIPSHOWN;
InBlock.gif        
private static readonly object EVENT_BALLOONTIPCLOSED;
InBlock.gif        
private static readonly object EVENT_BALLOONTIPCLICKED;
InBlock.gif
InBlock.gif        
static NotifyIconEx()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            NotifyIconEx.EVENT_BALLOONTIPSHOWN 
= new object();
InBlock.gif            NotifyIconEx.EVENT_BALLOONTIPCLOSED 
= new object();
InBlock.gif            NotifyIconEx.EVENT_BALLOONTIPCLICKED 
= new object();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public NotifyIconEx()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: Add constructor logic here
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public event EventHandler BalloonTipShown
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            add
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Events.AddHandler(NotifyIconEx.EVENT_BALLOONTIPSHOWN, value);
ExpandedSubBlockEnd.gif            }

InBlock.gif            remove
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Events.RemoveHandler(NotifyIconEx.EVENT_BALLOONTIPSHOWN, value);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public event EventHandler BalloonTipClosed
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            add
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Events.AddHandler(NotifyIconEx.EVENT_BALLOONTIPCLOSED, value);
ExpandedSubBlockEnd.gif            }

InBlock.gif            remove
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Events.RemoveHandler(NotifyIconEx.EVENT_BALLOONTIPCLOSED, value);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif    
InBlock.gif        
public event EventHandler BalloonTipClicked
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            add
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Events.AddHandler(NotifyIconEx.EVENT_BALLOONTIPCLICKED, value);
ExpandedSubBlockEnd.gif            }

InBlock.gif            remove
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Events.RemoveHandler(NotifyIconEx.EVENT_BALLOONTIPCLICKED, value);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void ShowBalloonTip(InfoIcon infoIcon, string title, string message, uint timeout)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            nid.uFlags 
= 0x0010;
InBlock.gif            nid.dwInfoFlags 
= (int)infoIcon;
InBlock.gif            nid.szInfo 
= message;
InBlock.gif            nid.szInfoTitle 
= title;
InBlock.gif            nid.uTimeoutOrVersion 
= timeout;
InBlock.gif            
base.UpdateIcon(true);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void OnBalloonTipShown()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            EventHandler handler 
= (EventHandler) base.Events[NotifyIconEx.EVENT_BALLOONTIPSHOWN];
InBlock.gif            
if (handler != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                handler(
this, EventArgs.Empty);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void OnBalloonTipClosed()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            EventHandler handler 
= (EventHandler) base.Events[NotifyIconEx.EVENT_BALLOONTIPCLOSED];
InBlock.gif            
if (handler != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                handler(
this, EventArgs.Empty);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void OnBalloonTipClicked()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            EventHandler handler 
= (EventHandler) base.Events[NotifyIconEx.EVENT_BALLOONTIPCLICKED];
InBlock.gif            
if (handler != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                handler(
this, EventArgs.Empty);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void WndProc(ref System.Windows.Forms.Message msg)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int msgId = msg.Msg;
InBlock.gif            
if ( msgId == 0x111 )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
switch((int)msg.LParam)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
case WM_BALLOONTIPSHOWN:
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
this.OnBalloonTipShown();
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
case WM_BALLOONTIPCLOSING:
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
this.OnBalloonTipClosed();
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
case WM_BALLOONTIPCLOSED:
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
this.OnBalloonTipClosed();
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
case WM_BALLOONTIPCLICKED:
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
this.OnBalloonTipClicked();
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.WndProc (ref msg);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


    为了尽可能的利用原来的NotifyIcon中的代码,不做太大的改动。新的NotifyIcon中修改了UpdateIcon方法中uFlags的管理。原来的代码是在调用UpdateIcon时给uFlags赋值为0x0001(即:NIF_MESSAGE),然后再通过一些判断通过|操作加入新的flag。现在把第一次赋值改为了:uFlags|=0x0001,目的是为了把ShowBalloonTip中对uFlags的赋值传递进取。但是如果在显示了Balloon Tip后,uFlags中仍然保持了0x0010(即:NIF_INFO)标志位,那么只要NotifyIcon中移执行UpdateIcon就会再次显示Balloon Tipemdgust.gif。所以在UpdateIcon方法的最后,我们清除uFlags中的0x0010标识位,让uFlag ^= 0x0010;,就这样简单NotifyIcon即改造完毕。

    新鲜出炉的NotifyIcon控件,使用方便,价格公道,童叟无欺emsmilep.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值