`
hududanyzd
  • 浏览: 815679 次
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多
前几天有网友问.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#NotifyIcon闪烁示例

    在本文中,我们将深入探讨如何使用C#编程语言中的`NotifyIcon`组件和`Timer`组件来实现类似于QQ消息提示的闪烁效果。`NotifyIcon`是Windows Forms应用程序中一个非常实用的工具,它允许程序在任务栏托盘区域显示图标...

    WPF NotifyIcon类

    在C#的Windows Presentation Foundation(WPF)框架中,`NotifyIcon`类是一个非常重要的组件,它允许开发者在系统托盘区域显示图标和通知。这个类通常用于在应用程序最小化时提供用户界面反馈,或者在后台运行时显示...

    c# WPF 下使用NotifyIcon

    c# WPF 下使用NotifyIcon 可以下载调试一下 有什么问题相互交流

    C# WPF 实现鼠标移动到NotifyIcon上显示QQ未读消息,移开关闭未读消息

    综上所述,这个项目展示了如何使用C# WPF的`NotifyIcon`组件结合鼠标事件来实现在鼠标悬停时显示未读QQ消息,以及鼠标移开时自动关闭消息的交互效果。这个设计可以用于提供非侵入式的用户通知,提高用户体验。通过...

    C# 2008 NotifyIcon

    这个控件主要用在那些需要在后台运行且不希望在任务栏上显示窗口的应用程序中。下面我们将深入探讨`NotifyIcon`控件的使用方法、功能以及一些常见的应用场景。 1. **`NotifyIcon`的基本使用** - 创建`NotifyIcon`...

    C# 利用NotifyIcon控件实现窗体最小化至系统托盘

    在C#中,`NotifyIcon`控件属于`System.Windows.Forms`命名空间,通常用于创建在系统托盘区显示的小图标,以及与该图标相关的右键菜单和提示信息。 要实现窗体最小化到系统托盘,我们需要以下几个步骤: 1. **引入...

    C#做托盘程序(NotifyIcon控件)

    - `NotifyIcon`的`Text`属性用于设置鼠标悬停在图标上时显示的提示文字,例如`TrayIcon.Text = "用 Visual C#做托盘程序" + "\n" + "作者:马金虎于 2001.12.08"`。 4. **添加菜单**: - 为了在托盘图标上提供...

    c# winform程序右下角通知实现实例NotifyIcondemo

    在C# WinForm应用程序开发中,`NotifyIcon`组件是一个非常重要的工具,它允许程序员在系统托盘(屏幕右下角)显示一个图标,并通过该图标提供用户通知和交互功能。`NotifyIcon`实例通常用于那些不需要在任务栏中显示...

    C# 操作 NotifyIcon

    外国人写的 操作NotifyIcon 包括创建 删除 获取 NotifyIcon的位置信息等等 很值得参考 虽然是用的WPF 但是核心代码都一样。

    WPF实现系统托盘 NotifyIcon

    这种功能通过使用`NotifyIcon`组件可以轻松达成。`NotifyIcon`是Windows Forms库中的一个组件,但在WPF中,我们需要通过Windows Forms互操作性(WindowsFormsHost)来使用它。下面我们将详细讲解如何在WPF中实现这一...

    C#实现的简单文件备份工具

    本项目是一个用C#实现的简单文件备份工具,旨在提供一种便捷的方式对用户指定的源路径文件进行备份。以下是对该工具的功能、实现原理及C#编程相关知识点的详细解释。 1. 文件备份功能: - 非压缩备份:工具直接...

    C# 仿SQL--通知图标控件NotifyIcon最小化托盘

    【标题】"C# 仿SQL--通知图标控件NotifyIcon最小化托盘"涉及到的知识点主要集中在C#编程语言的GUI(图形用户界面)设计和系统托盘交互上。在Windows应用程序开发中,有时我们需要将应用窗口最小化后隐藏在系统托盘...

    NotifyIcon

    本项目是一个针对初学者的简单 `NotifyIcon` 演示程序,使用了C#编程语言。 1. **NotifyIcon组件**: - `NotifyIcon` 是.NET Framework提供的System.Windows.Forms命名空间中的控件,用于在任务栏的通知区域显示...

    C# WPF 状态栏图标特效 类似QQ/MSN的状态栏图标显示 NotifyIcon

    下面我们将深入探讨如何使用C#和WPF来实现这种状态栏图标特效。 首先,我们需要了解`NotifyIcon`组件。`NotifyIcon`是.NET Framework中的一个类,位于`System.Windows.Forms`命名空间下,即使在WPF应用中,我们仍然...

    C# NotifyIcon实现任务栏托盘菜单

    VS2017 .Net Framework 2.5 开发, yong 在WindowsForm下使用NotifyIcon、ContextMenuStrip,Timer 实现任务栏托盘菜单隐藏,显示及闪烁效果。

    c#系统托盘图标刷新类

    1. **初始化托盘图标**:通常会使用`System.Windows.Forms.NotifyIcon`类来创建和管理托盘图标。通过设置`Icon`属性来指定显示的图标,`Visible`属性来控制图标的可见性,以及`ContextMenu`来添加右键菜单。 ```...

    WPF NotifyIcon 实现例子

    本示例将介绍如何在WPF应用中使用`NotifyIcon`来实现类似的功能,且全程无需依赖任何第三方控件,完全基于.NET原生代码。 首先,我们需要在WPF的XAML文件中引入`System.Windows.Forms.Integration`命名空间,因为`...

    notifyIcon托盘程序及实现思路

    在Windows操作系统中,"notifyIcon"是指系统任务栏通知区域的小图标,通常用于应用程序的状态指示或后台运行。这种托盘程序的设计与实现是许多开发者在创建桌面应用时需要掌握的关键技能之一。本篇文章将深入探讨...

Global site tag (gtag.js) - Google Analytics