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

下面是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
}
}

转载于:https://www.cnblogs.com/jasmine_xm/archive/2010/11/01/1866054.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值