天天看点

windows mobile添加任务栏图标

本文讲的是在net cf下给windows mobile添加任务栏图标的两种方式:

1、P/Invoke

using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace MobileExamine { /// <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资源名称,如“#10301” </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); } } /// <summary> /// 删除托盘图标;icon为属性中的icon /// </summary> public void Remove() { NotifyMessage(messageWindow.Hwnd, NIM_DELETE, (uint)uID, IntPtr.Zero); } /// <summary> /// 修改托盘图标;icon为属性中的icon /// </summary> 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; Shell_NotifyIcon(dwMessage, ref notdata); } #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 #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 } }

需要导入Microsoft.WindowsCE.Forms类库。

在Form中调用:

public partial class MainForm : Form { // 注意,定义的是全局变量。 NotifyIcon notifyIcon; public MainForm() { InitializeComponent(); InitNotifyIcon(); } private void notifyIcon_Click(object sender, EventArgs e) { MessageBox.Show("Icon Clicked!"); } /// <summary> /// 设置任务栏图标。 /// </summary> private void InitNotifyIcon() { notifyIcon= new NotifyIcon(); // 添加图标对象 notifyIcon.Add(this.Icon); notifyIcon.Click += new EventHandler(notifyIcon_Click); } private void menuItemClose_Click(object sender, EventArgs e) { this.Close(); // 删除图标对象 notifyIcon.Remove(); } }

参考自:http://msdn.microsoft.com/en-us/library/aa446525.aspx

2、使用OpenNetCF 推出的 Smart Device Framework ( SDF )类库中的 NotifyIcon 类来完成。

OpenNetCF 的官方网址是 http://www.opennetcf.com/ ,你可以点击这个地址

http://www.opennetcf.com/Products/SmartDeviceFramework/tabid/65/Default.aspx 下载 Smart Device Framework

( SDF )最新版本 2.3 ,它分为社区版(免费),标准版( $50 ),专业版( $500 ),后两个版本只支持

Visual Studio 2008 ,而免费的社区版同时还支持 Visual Studio 2005 。社区版与收费版本最大的区别我认为是没有设计时支持,

而 SDF 中只有少量的控件类,所以设计时支持也显得不是那么必要了。下载安装完成后,在visual studio中添加对

OpenNETCF.Windows.Forms.dll的引用,在安装目录下的Bin目录下。

使用:

using OpenNETCF.Windows.Forms; public partial class MainForm : Form { // 注意,定义的是全局变量。 NotifyIcon notifyIcon; public MainForm() { InitializeComponent(); InitNotifyIcon(); } private void notifyIcon_Click(object sender, EventArgs e) { MessageBox.Show("Icon Clicked!"); } /// <summary> /// 设置任务栏图标。 /// </summary> private void InitNotifyIcon() { notifyIcon = new NotifyIcon(); // 单击事件。 notifyIcon.Click += new EventHandler(notifyIcon_Click); notifyIcon.Icon = this.Icon; // 这里通过引用窗体的图标对Icon属性赋值。 notifyIcon.Text = "Mobile"; notifyIcon.Visible = true; } }

继续阅读