用C#实现智能设备上的NotifyIcon类

    前几天有网友问.NET CF中怎么实现NotifyIcon,我这才知道原来.NET CF并没有提供NotifyIcon控件。
    于是偶想PC上可以用Shell_NotifyIcon和MessageWindow来实现托盘图标,只是不知道.NET CF支持不支持这两个东东了。仔细看了一下.NET CF中可疑的命名空间,没想到在Microsoft.WindowsCE.Forms命名空间里面竟然有一个MessageWindow 类,太好了,只剩下一个Shell_NotifyIcon 函数了。接着   在Window CE的SDK的帮助文件里,又发现Window CE Platform API已经包含了Shell_NotifyIcon函数。两大“主料”都齐了,只剩下锅了。
    先看一下MessageWindow类,这个类提供了 WndProc 方法,用于处理窗口消息,并公开了可能传递给本机窗口函数的有效窗口句柄。要使用它,派生一个新类,并重写的 WndProc 方法,这样才能截获特定的窗口消息。这里主要用来处理click事件。
        Shell_NotifyIcon的用法如下:

None.gif [DllImport( " coredll.dll " )]
None.gif
internal   static   extern   int  Shell_NotifyIcon( int  dwMessage,  ref   NOTIFYICONDATA pnid);
None.gif

其中,NOTIFYICONDATA结构如下:

None.gif struct  NOTIFYICONDATA
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
int cbSize;
InBlock.gif    IntPtr hWnd;
InBlock.gif    
uint uID;
InBlock.gif    
uint uFlags;
InBlock.gif    
uint uCallbackMessage;
InBlock.gif    IntPtr hIcon;
ExpandedBlockEnd.gif}

     Pnid参数的生命需要注意,是按引用传递的,因为Shell_NotifyIcon 需要一个指向 NOTIFYICONDATA 结构的指针。
    hWnd是用来接收任务栏中图标单击消息的窗口的句柄。
运行示例的时候由于窗体最大化,挡住了任务栏,把窗体最小化之后就能看到托盘图标了。(效果图片竟然贴不上来,改天再贴吧)
该类和示例的下载地址:http://files.cnblogs.com/ttinfo/NotifyIconCf.rar

下面是NotifyIcon类的实现,别忘了引用Microsoft.WindowsCE.Forms。注意Add方法提供了不同的重载形式,具体请参看注释:

None.gif using  System;
None.gif
using  System.Runtime.InteropServices;
None.gif
using  System.Windows.Forms;
None.gif
None.gif
namespace  NotifyClient
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 智能设备托盘图标类
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class NotifyIcon
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//单击事件
InBlock.gif
        public event System.EventHandler Click;
InBlock.gif
InBlock.gif        
private MyMessageWindow messageWindow;
InBlock.gif        
private int uID = 5000;
InBlock.gif        
private System.Drawing.Icon _Icon;
InBlock.gif        
InBlock.gif        
public NotifyIcon()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            messageWindow 
= new MyMessageWindow(this);
InBlock.gif            messageWindow.uID 
= uID;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public System.Drawing.Icon Icon
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _Icon 
= value;
InBlock.gif
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif      
InBlock.gif        
~NotifyIcon()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Remove();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 添加托盘图标
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="hIcon">icon文件的有效句柄</param>

InBlock.gif        public void Add(IntPtr hIcon)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            NotifyMessage(messageWindow.Hwnd, NIM_ADD, (
uint)uID, hIcon);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 添加托盘图标
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="IconRes">编译之后的资源文件中的icon资源名称,如“#201547”</param>

InBlock.gif        public void Add(string IconRes)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IntPtr hIcon 
= LoadIcon(GetModuleHandle(null), IconRes);
InBlock.gif            NotifyMessage(messageWindow.Hwnd, NIM_ADD, (
uint)uID, hIcon);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 添加托盘图标
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="icon">icon文件</param>

InBlock.gif        public void Add(System.Drawing.Icon icon)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            NotifyMessage(messageWindow.Hwnd, NIM_ADD, (
uint)uID, icon.Handle);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 添加托盘图标;icon为属性中的icon
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void Add()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (_Icon != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                NotifyMessage(messageWindow.Hwnd, NIM_ADD, (
uint)uID, _Icon.Handle);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void Remove()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            NotifyMessage(messageWindow.Hwnd, NIM_DELETE, (
uint)uID, IntPtr.Zero);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Modify(IntPtr hIcon)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            NotifyMessage(messageWindow.Hwnd, NIM_MODIFY, (
uint)uID, hIcon);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif        
private void NotifyMessage(IntPtr hwnd, int dwMessage, uint uID, IntPtr hIcon)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            NOTIFYICONDATA notdata 
= new NOTIFYICONDATA();
InBlock.gif
InBlock.gif            notdata.cbSize 
= 152;
InBlock.gif            notdata.hIcon 
= hIcon;
InBlock.gif            notdata.hWnd 
= hwnd;
InBlock.gif            notdata.uCallbackMessage 
= WM_NOTIFY_TRAY;
InBlock.gif            notdata.uFlags 
= NIF_MESSAGE | NIF_ICON;
InBlock.gif            notdata.uID 
= uID;
InBlock.gif
InBlock.gif            
int ret = Shell_NotifyIcon(dwMessage, ref notdata);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
API#region API 
InBlock.gif        
//定义消息常量
InBlock.gif
        const int NIF_MESSAGE = 0x00000001;
InBlock.gif        
const int NIF_ICON = 0x00000002;
InBlock.gif        
internal const int WM_LBUTTONDOWN = 0x0201;    
InBlock.gif
InBlock.gif        
internal const int NIM_ADD = 0x00000000;
InBlock.gif        
internal const int NIM_MODIFY = 0x00000001;
InBlock.gif        
internal const int NIM_DELETE = 0x00000002;
InBlock.gif
InBlock.gif        
//自定义消息
InBlock.gif
        internal const int WM_NOTIFY_TRAY = 0x0400 + 2001;
InBlock.gif
InBlock.gif        
InBlock.gif
InBlock.gif
InBlock.gif        
internal struct NOTIFYICONDATA
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
internal int cbSize;
InBlock.gif            
internal IntPtr hWnd;
InBlock.gif            
internal uint uID;
InBlock.gif            
internal uint uFlags;
InBlock.gif            
internal uint uCallbackMessage;
InBlock.gif            
internal IntPtr hIcon;            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [DllImport(
"coredll.dll")]
InBlock.gif        
internal static extern int Shell_NotifyIcon(
InBlock.gif            
int dwMessage, ref NOTIFYICONDATA pnid);
InBlock.gif
InBlock.gif        [DllImport(
"coredll.dll")]
InBlock.gif        
internal static extern int SetForegroundWindow(IntPtr hWnd);
InBlock.gif
InBlock.gif        [DllImport(
"coredll.dll")]
InBlock.gif        
internal static extern int ShowWindow(
InBlock.gif            IntPtr hWnd,
InBlock.gif            
int nCmdShow);
InBlock.gif
InBlock.gif        [DllImport(
"coredll.dll")]
InBlock.gif        
internal static extern IntPtr GetFocus();
InBlock.gif
InBlock.gif        [DllImport(
"coredll.dll")]
InBlock.gif        
internal static extern IntPtr LoadIcon(IntPtr hInst, string IconName);
InBlock.gif
InBlock.gif        [DllImport(
"coredll.dll")]
InBlock.gif        
internal static extern IntPtr GetModuleHandle(String lpModuleName);
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
MessageWindow#region MessageWindow
InBlock.gif
InBlock.gif        
internal class MyMessageWindow : Microsoft.WindowsCE.Forms.MessageWindow
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif            
private int _uID = 0;
InBlock.gif            
private NotifyIcon notifyIcon;
InBlock.gif
InBlock.gif           
InBlock.gif            
public MyMessageWindow(NotifyIcon notIcon)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                notifyIcon 
= notIcon;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
public int uID
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
set
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    _uID 
= value;
InBlock.gif
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
protected override void WndProc(ref Microsoft.WindowsCE.Forms.Message msg)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                
if (msg.Msg == WM_NOTIFY_TRAY)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if ((int)msg.LParam == WM_LBUTTONDOWN)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if ((int)msg.WParam == _uID)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
InBlock.gif                            
if (notifyIcon.Click != null)
InBlock.gif                                notifyIcon.Click(notifyIcon, 
null);
InBlock.gif
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


 

转载于:https://www.cnblogs.com/ttinfo/archive/2006/10/31/545741.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值