前几天有网友问.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的用法如下:
[DllImport("coredll.dll")]
internalstaticexternintShell_NotifyIcon(intdwMessage,refNOTIFYICONDATApnid);
其中,NOTIFYICONDATA结构如下:
structNOTIFYICONDATA
{
intcbSize;
IntPtrhWnd;
uintuID;
uintuFlags;
uintuCallbackMessage;
IntPtrhIcon;
}
Pnid参数的生命需要注意,是按引用传递的,因为Shell_NotifyIcon 需要一个指向 NOTIFYICONDATA 结构的指针。
hWnd是用来接收任务栏中图标单击消息的窗口的句柄。
运行示例的时候由于窗体最大化,挡住了任务栏,把窗体最小化之后就能看到托盘图标了。(效果图片竟然贴不上来,改天再贴吧)
该类和示例的下载地址:http://www.cnblogs.com/Files/ttinfo/NotifyIconCf.rar
下面是NotifyIcon类的实现,别忘了引用Microsoft.WindowsCE.Forms。注意Add方法提供了不同的重载形式,具体请参看注释:
usingSystem;
usingSystem.Runtime.InteropServices;
usingSystem.Windows.Forms;
namespaceNotifyClient
{
///<summary>
///智能设备托盘图标类
///</summary>
publicclassNotifyIcon
{
//单击事件
publiceventSystem.EventHandlerClick;
privateMyMessageWindowmessageWindow;
privateintuID=5000;
privateSystem.Drawing.Icon_Icon;
publicNotifyIcon()
{
messageWindow=newMyMessageWindow(this);
messageWindow.uID=uID;
}
publicSystem.Drawing.IconIcon
{
set
{
_Icon=value;
}
}
~NotifyIcon()
{
Remove();
}
///<summary>
///添加托盘图标
///</summary>
///<paramname="hIcon">icon文件的有效句柄</param>
publicvoidAdd(IntPtrhIcon)
{
NotifyMessage(messageWindow.Hwnd,NIM_ADD,(uint)uID,hIcon);
}
///<summary>
///添加托盘图标
///</summary>
///<paramname="IconRes">编译之后的资源文件中的icon资源名称,如“#201547”</param>
publicvoidAdd(stringIconRes)
{
IntPtrhIcon=LoadIcon(GetModuleHandle(null),IconRes);
NotifyMessage(messageWindow.Hwnd,NIM_ADD,(uint)uID,hIcon);
}
///<summary>
///添加托盘图标
///</summary>
///<paramname="icon">icon文件</param>
publicvoidAdd(System.Drawing.Iconicon)
{
NotifyMessage(messageWindow.Hwnd,NIM_ADD,(uint)uID,icon.Handle);
}
///<summary>
///添加托盘图标;icon为属性中的icon
///</summary>
publicvoidAdd()
{
if(_Icon!=null)
{
NotifyMessage(messageWindow.Hwnd,NIM_ADD,(uint)uID,_Icon.Handle);
}
}
publicvoidRemove()
{
NotifyMessage(messageWindow.Hwnd,NIM_DELETE,(uint)uID,IntPtr.Zero);
}
publicvoidModify(IntPtrhIcon)
{
NotifyMessage(messageWindow.Hwnd,NIM_MODIFY,(uint)uID,hIcon);
}
privatevoidNotifyMessage(IntPtrhwnd,intdwMessage,uintuID,IntPtrhIcon)
{
NOTIFYICONDATAnotdata=newNOTIFYICONDATA();
notdata.cbSize=152;
notdata.hIcon=hIcon;
notdata.hWnd=hwnd;
notdata.uCallbackMessage=WM_NOTIFY_TRAY;
notdata.uFlags=NIF_MESSAGE|NIF_ICON;
notdata.uID=uID;
intret=Shell_NotifyIcon(dwMessage,refnotdata);
}
#regionAPI
//定义消息常量
constintNIF_MESSAGE=0x00000001;
constintNIF_ICON=0x00000002;
internalconstintWM_LBUTTONDOWN=0x0201;
internalconstintNIM_ADD=0x00000000;
internalconstintNIM_MODIFY=0x00000001;
internalconstintNIM_DELETE=0x00000002;
//自定义消息
internalconstintWM_NOTIFY_TRAY=0x0400+2001;
internalstructNOTIFYICONDATA
{
internalintcbSize;
internalIntPtrhWnd;
internaluintuID;
internaluintuFlags;
internaluintuCallbackMessage;
internalIntPtrhIcon;
}
[DllImport("coredll.dll")]
internalstaticexternintShell_NotifyIcon(
intdwMessage,refNOTIFYICONDATApnid);
[DllImport("coredll.dll")]
internalstaticexternintSetForegroundWindow(IntPtrhWnd);
[DllImport("coredll.dll")]
internalstaticexternintShowWindow(
IntPtrhWnd,
intnCmdShow);
[DllImport("coredll.dll")]
internalstaticexternIntPtrGetFocus();
[DllImport("coredll.dll")]
internalstaticexternIntPtrLoadIcon(IntPtrhInst,stringIconName);
[DllImport("coredll.dll")]
internalstaticexternIntPtrGetModuleHandle(StringlpModuleName);
#endregion
#regionMessageWindow
internalclassMyMessageWindow:Microsoft.WindowsCE.Forms.MessageWindow
{
privateint_uID=0;
privateNotifyIconnotifyIcon;
publicMyMessageWindow(NotifyIconnotIcon)
{
notifyIcon=notIcon;
}
publicintuID
{
set
{
_uID=value;
}
}
protectedoverridevoidWndProc(refMicrosoft.WindowsCE.Forms.Messagemsg)
{
if(msg.Msg==WM_NOTIFY_TRAY)
{
if((int)msg.LParam==WM_LBUTTONDOWN)
{
if((int)msg.WParam==_uID)
{
if(notifyIcon.Click!=null)
notifyIcon.Click(notifyIcon,null);
}
}
}
}
}
#endregion
}
}
分享到:
相关推荐
C# WPF NotifyIcon类的使用
c# WPF 下使用NotifyIcon 可以下载调试一下 有什么问题相互交流
模仿QQ闪动,用到C#中的NotifyIcon组件,还有Timer组件
开发环境:vs2013; 此方法实现了鼠标移动到notifyIcon图标上显示窗体,鼠标离开隐藏窗体。
C#实现窗体最小化至系统托盘,利用NotifyIcon控件实现
C# 2008 NotifyIcon控件的实验示例
外国人写的 操作NotifyIcon 包括创建 删除 获取 NotifyIcon的位置信息等等 很值得参考 虽然是用的WPF 但是核心代码都一样。
VS2017 .Net Framework 2.5 开发, yong 在WindowsForm下使用NotifyIcon、ContextMenuStrip,Timer 实现任务栏托盘菜单隐藏,显示及闪烁效果。
托盘程序具有直观、占用屏幕空间较小并且可以为它定义多个功能菜单,这就...微软公司极力推荐的下一代程序开发语言--Visual C#来说,却可以十分方便设计出一个托盘程序。本文就是介绍Visual C#设计托盘程序的具体过程。
NotifyIcon控件,实现最小化到系统托盘
c# winform程序右下角通知实现实例NotifyIcondemo 实现winform程序打开后,在屏幕右下角有响应的图标,可以进行退出等各种操作。
WPF实现的窗口最小化到任务栏 系统托盘 NotifyIcon 任务栏
一个简单的NotifyIcon演示程序,适合初学者,语言是C#了
NotifyIcon 控件实现托盘提示消息框
托盘程序及实现思路:主要是实现图标的闪烁及报警提示
任务栏托盘菜单(NotifyIcon) 属性:Icon--系统栏中显示的图标 ContextMenuStrip--和contextMenuStrip连用 其他的可以看看,还有三个提示信息的设置...
WinCE notifyIcon 托盘图标显示,在Windows CE 5.0 环境下测试通过.
NotifyIcon控件和ContentMenu控件的使用 本例是一托盘隐藏程序!