前几天有网友问.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")]
internal static extern int shell_notifyicon(int dwmessage, ref notifyicondata pnid);
其中,notifyicondata结构如下:
struct notifyicondata
{
int cbsize;
intptr hwnd;
uint uid;
uint uflags;
uint ucallbackmessage;
intptr hicon;
}
pnid参数的生命需要注意,是按引用传递的,因为shell_notifyicon 需要一个指向 notifyicondata 结构的指针。
hwnd是用来接收任务栏中图标单击消息的窗口的句柄。
运行示例的时候由于窗体最大化,挡住了任务栏,把窗体最小化之后就能看到托盘图标了。(效果图片竟然贴不上来,改天再贴吧)
该类和示例的下载地址:http://www.cnblogs.com/files/ttinfo/notifyiconcf.rar
下面是notifyicon类的实现,别忘了引用microsoft.windowsce.forms。注意add方法提供了不同的重载形式,具体请参看注释:
using system;
using system.runtime.interopservices;
using system.windows.forms;
namespace notifyclient
{
/**//// <summary>
/// 智能设备托盘图标类
/// </summary>
public class notifyicon
{
//单击事件
public event system.eventhandler click;
private mymessagewindow messagewindow;
private int uid = 5000;
private system.drawing.icon _icon;
public notifyicon()
{
messagewindow = new mymessagewindow(this);
messagewindow.uid = uid;
}
public system.drawing.icon icon
{
set
{
_icon = value;
}
}
~notifyicon()
{
remove();
}
/**//// <summary>
/// 添加托盘图标
/// </summary>
/// <param name="hicon">icon文件的有效句柄</param>
public void add(intptr hicon)
{
notifymessage(messagewindow.hwnd, nim_add, (uint)uid, hicon);
}
/**//// <summary>
/// 添加托盘图标
/// </summary>
/// <param name="iconres">编译之后的资源文件中的icon资源名称,如“#201547”</param>
public void add(string iconres)
{
intptr hicon = loadicon(getmodulehandle(null), iconres);
notifymessage(messagewindow.hwnd, nim_add, (uint)uid, hicon);
}
/**//// <summary>
/// 添加托盘图标
/// </summary>
/// <param name="icon">icon文件</param>
public void add(system.drawing.icon icon)
{
notifymessage(messagewindow.hwnd, nim_add, (uint)uid, icon.handle);
}
/**//// <summary>
/// 添加托盘图标;icon为属性中的icon
/// </summary>
public void add()
{
if (_icon != null)
{
notifymessage(messagewindow.hwnd, nim_add, (uint)uid, _icon.handle);
}
}
public void remove()
{
notifymessage(messagewindow.hwnd, nim_delete, (uint)uid, intptr.zero);
}
public void modify(intptr hicon)
{
notifymessage(messagewindow.hwnd, nim_modify, (uint)uid, hicon);
}
private void notifymessage(intptr hwnd, int dwmessage, uint uid, intptr hicon)
{
notifyicondata notdata = new notifyicondata();
notdata.cbsize = 152;
notdata.hicon = hicon;
notdata.hwnd = hwnd;
notdata.ucallbackmessage = wm_notify_tray;
notdata.uflags = nif_message | nif_icon;
notdata.uid = uid;
int ret = shell_notifyicon(dwmessage, ref notdata);
}
api#region api
//定义消息常量
const int nif_message = 0x00000001;
const int nif_icon = 0x00000002;
internal const int wm_lbuttondown = 0x0201;
internal const int nim_add = 0x00000000;
internal const int nim_modify = 0x00000001;
internal const int nim_delete = 0x00000002;
//自定义消息
internal const int wm_notify_tray = 0x0400 + 2001;
internal struct notifyicondata
{
internal int cbsize;
internal intptr hwnd;
internal uint uid;
internal uint uflags;
internal uint ucallbackmessage;
internal intptr hicon;
}
[dllimport("coredll.dll")]
internal static extern int shell_notifyicon(
int dwmessage, ref notifyicondata pnid);
[dllimport("coredll.dll")]
internal static extern int setforegroundwindow(intptr hwnd);
[dllimport("coredll.dll")]
internal static extern int showwindow(
intptr hwnd,
int ncmdshow);
[dllimport("coredll.dll")]
internal static extern intptr getfocus();
[dllimport("coredll.dll")]
internal static extern intptr loadicon(intptr hinst, string iconname);
[dllimport("coredll.dll")]
internal static extern intptr getmodulehandle(string lpmodulename);
#endregion
messagewindow#region messagewindow
internal class mymessagewindow : microsoft.windowsce.forms.messagewindow
{
private int _uid = 0;
private notifyicon notifyicon;
public mymessagewindow(notifyicon noticon)
{
notifyicon = noticon;
}
public int uid
{
set
{
_uid = value;
}
}
protected override void wndproc(ref microsoft.windowsce.forms.message msg)
{
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
}
}
新闻热点
疑难解答