关于WinCe托盘做法

前段时间做一个 WinCe的小程序,用来检测ZigBee网络信号。有显示系统托盘(NotifyIcon)的需求、

 

开始了。

 

首页准备一个托盘的类  NotifyIcon.cs 

  1 using System;
  2 using System.Runtime.InteropServices;
  3 using System.Windows.Forms;
  4 
  5 namespace WinCeSerial.NotifyClient
  6 {
  7     /// <summary>
  8     /// 智能设备托盘图标类
  9     /// </summary>
 10     public class NotifyIcon
 11     {
 12         //单击事件
 13         public event System.EventHandler Click;
 14 
 15         private MyMessageWindow messageWindow;
 16         private int uID = 5000;
 17         private System.Drawing.Icon _Icon;
 18         
 19         public NotifyIcon()
 20         {
 21             messageWindow = new MyMessageWindow(this);
 22             messageWindow.uID = uID;
 23         }
 24         public System.Drawing.Icon Icon
 25         {
 26             set
 27             {
 28                 _Icon = value;
 29 
 30             }
 31         }
 32       
 33         ~NotifyIcon()
 34         {
 35             Remove();
 36         }
 37 
 38         /// <summary>
 39         /// 添加托盘图标
 40         /// </summary>
 41         /// <param name="hIcon">icon文件的有效句柄</param>
 42         public void Add(IntPtr hIcon)
 43         {
 44             NotifyMessage(messageWindow.Hwnd, NIM_ADD, (uint)uID, hIcon);
 45         }
 46         /// <summary>
 47         /// 添加托盘图标
 48         /// </summary>
 49         /// <param name="IconRes">编译之后的资源文件中的icon资源名称,如“#201547”</param>
 50         public void Add(string IconRes)
 51         {
 52             IntPtr hIcon = LoadIcon(GetModuleHandle(null), IconRes);
 53             NotifyMessage(messageWindow.Hwnd, NIM_ADD, (uint)uID, hIcon);
 54         }
 55         /// <summary>
 56         /// 添加托盘图标
 57         /// </summary>
 58         /// <param name="icon">icon文件</param>
 59         public void Add(System.Drawing.Icon icon)
 60         {
 61             NotifyMessage(messageWindow.Hwnd, NIM_ADD, (uint)uID, icon.Handle);
 62         }
 63         /// <summary>
 64         /// 添加托盘图标;icon为属性中的icon
 65         /// </summary>
 66         public void Add()
 67         {
 68             if (_Icon != null)
 69             {
 70                 NotifyMessage(messageWindow.Hwnd, NIM_ADD, (uint)uID, _Icon.Handle);
 71             }
 72         }
 73         public void Remove()
 74         {
 75 
 76             NotifyMessage(messageWindow.Hwnd, NIM_DELETE, (uint)uID, IntPtr.Zero);
 77         }
 78 
 79         public void Modify(IntPtr hIcon)
 80         {
 81 
 82             NotifyMessage(messageWindow.Hwnd, NIM_MODIFY, (uint)uID, hIcon);
 83 
 84         }
 85 
 86 
 87 
 88         private void NotifyMessage(IntPtr hwnd, int dwMessage, uint uID, IntPtr hIcon)
 89         {
 90             NOTIFYICONDATA notdata = new NOTIFYICONDATA();
 91 
 92             notdata.cbSize = 152;
 93             notdata.hIcon = hIcon;
 94             notdata.hWnd = hwnd;
 95             notdata.uCallbackMessage = WM_NOTIFY_TRAY;
 96             notdata.uFlags = NIF_MESSAGE | NIF_ICON;
 97             notdata.uID = uID;
 98 
 99             int ret = Shell_NotifyIcon(dwMessage, ref notdata);
100         }
101 
102         #region API 
103         //定义消息常量
104         const int NIF_MESSAGE = 0x00000001;
105         const int NIF_ICON = 0x00000002;
106         internal const int WM_LBUTTONDOWN = 0x0201;    
107 
108         internal const int NIM_ADD = 0x00000000;
109         internal const int NIM_MODIFY = 0x00000001;
110         internal const int NIM_DELETE = 0x00000002;
111 
112         //自定义消息
113         internal const int WM_NOTIFY_TRAY = 0x0400 + 2001;
114 
115         
116 
117 
118         internal struct NOTIFYICONDATA
119         {
120             internal int cbSize;
121             internal IntPtr hWnd;
122             internal uint uID;
123             internal uint uFlags;
124             internal uint uCallbackMessage;
125             internal IntPtr hIcon;            
126         }
127 
128         [DllImport("coredll.dll")]
129         internal static extern int Shell_NotifyIcon(
130             int dwMessage, ref NOTIFYICONDATA pnid);
131 
132         [DllImport("coredll.dll")]
133         internal static extern int SetForegroundWindow(IntPtr hWnd);
134 
135         [DllImport("coredll.dll")]
136         internal static extern int ShowWindow(
137             IntPtr hWnd,
138             int nCmdShow);
139 
140         [DllImport("coredll.dll")]
141         internal static extern IntPtr GetFocus();
142 
143         [DllImport("coredll.dll")]
144         internal static extern IntPtr LoadIcon(IntPtr hInst, string IconName);
145 
146         [DllImport("coredll.dll")]
147         internal static extern IntPtr GetModuleHandle(String lpModuleName);
148 
149 
150         #endregion
151 
152 
153         #region MessageWindow
154 
155         internal class MyMessageWindow : Microsoft.WindowsCE.Forms.MessageWindow
156         {
157             
158             private int _uID = 0;
159             private NotifyIcon notifyIcon;
160 
161            
162             public MyMessageWindow(NotifyIcon notIcon)
163             {
164                 notifyIcon = notIcon;
165             }
166 
167             public int uID
168             {
169                 set
170                 {
171                     _uID = value;
172 
173                 }
174             }
175 
176             protected override void WndProc(ref Microsoft.WindowsCE.Forms.Message msg)
177             {
178                
179 
180                 if (msg.Msg == WM_NOTIFY_TRAY)
181                 {
182                     if ((int)msg.LParam == WM_LBUTTONDOWN)
183                     {
184                         if ((int)msg.WParam == _uID)
185                         {
186                             
187                             if (notifyIcon.Click != null)
188                                 notifyIcon.Click(notifyIcon, null);
189                         }
190                     }
191                 }
192 
193             }
194         }
195         #endregion
196 
197     }
198 }

 

接下来就是 使用这个托盘类了。

 1  SortedList table = new SortedList();
 2         //int iCount = 0;
 3         //Assembly asm;
 4         string strRes;//接受的内容
 5         int netCount = 0;
 6         int caseValue = 0; //0 已经关闭网络; 1已经开启网络
 7         NotifyIcon notifyicon;
 8         System.Drawing.Icon icon;
 9         public frmSignMain()
10         {
11             InitializeComponent();
12 
13             //notifyicon = new NotifyIcon();
14             //notifyicon.Click += new EventHandler(notifyicon_Click);
15             //Assembly asm = Assembly.GetExecutingAssembly();
16             //icon = new Icon(asm.GetManifestResourceStream("WinCeSerial.Properties.00.ico"));
17 
18             //notifyicon.Icon = icon;
19 
20             //notifyicon.Add();
21         }
22 
23         void notifyicon_Click(object sender, EventArgs e)
24         {
25             //if (caseValue == 1)
26             //{
27             //    MessageBox.Show("ZigBee信号:" + cbLevel.Text);
28             //}
29             //else
30             //{
31             //    MessageBox.Show("未检测到信号.");
32             //}
33 
34 
35 
36  //切换托盘
37             notifyicon.Remove();
38             caseValue = 0;
39             notifyicon = new NotifyIcon();
40             notifyicon.Click += new EventHandler(notifyicon_Click);
41             Assembly asm = Assembly.GetExecutingAssembly();
42             icon = new Icon(asm.GetManifestResourceStream("WinCeSerial.Properties.gray.ico"));
43             notifyicon.Icon = icon;
44             notifyicon.Add();
45         }

好了,就这些,代码有些没用的变量自己去掉即可。

 

 

posted @ 2013-05-21 17:50  prettyjun  阅读(728)  评论(0编辑  收藏  举报