设为首页 友情链接
在线留言 发表文章
加入收藏 广告联系

刺猬首页

| 专案技术 | 网络技术 | 图形图象 | 网络编程 | 网页设计 | 操作系统 | 服务器 | 技术白皮书 | 在线实验室 | 刺猬论坛 |
  | 数据库 | 设计赏析 | 存储频道 | 网络安全 | 私服架设 |  Solaris | 网站评估 | PC维护技巧 | 下载中心 | 博 客 |
专题: | Linux | java | cisco | 防病毒 | 刀片 | SOA | iscsi | ASP.NET | SQL | Oracle |
您现在的位置: IT公社 IT community >> 网络编程 >> ASP.NET >> 教程正文 用户登录 新用户注册
专 题 栏 目
最 新 热 门
最 新 推 荐
相 关 文 章
变脸:用CSS+JS打造的网…
用CSS实现动态显示的五角…
用css实现文字的自动隐藏
PHP中使用crypt()实现用…
使用C#开发SmartPhone程…
用CSS样式表控制鼠标显示…
制作网页学习之常用CSS实…
VS2005中使用C#的新特性…
C#获取WAVE文件文件头信…
C#中势将窗体拖拽进行到…
  用C#实现智能设备上的NotifyIcon类           
用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")]
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

[1] [2] 下一页  

频道声明:本频道的文章除部分特别声明禁止转载的专稿外,可以自由转载.但请务必注明出出处和原始作者 文章版权归本频道与文章作者所有.对于被频道转载文章的个人和网站,我们表示深深的谢意。

原始作者:佚名 录入时间:2006-11-22
信息来源:不详 投稿信箱:itqoo@126.com
教程录入:admin    责任编辑:admin 
  • 上一个教程:

  • 下一个教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    - 关于我们 - 合作伙伴 - 友情链接 - 广告刊登 - 投稿热线 - 在线留言版权声明联系方式 -
    IT公社版权所有 粤ICP备05127012号
    Copyrigh@2005-2006 itqoo.com.Inc All Rights Reserved  推荐分辨率 1024*768
    联系站长:E-Mail:itqoo@126.com     MSN:urchincc@hotmail.com    QQ:点击这里给我发消息
    特别感谢:亿太网络提供空间支持