国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

用C#實(shí)現(xiàn)智能設(shè)備上的NotifyIcon類

2019-11-18 16:54:01
字體:
供稿:網(wǎng)友

    前幾天有網(wǎng)友問.NET CF中怎么實(shí)現(xiàn)NotifyIcon,我這才知道原來.NET CF并沒有提供NotifyIcon控件。
    于是偶想PC上可以用Shell_NotifyIcon和MessageWindow來實(shí)現(xiàn)托盤圖標(biāo),只是不知道.NET CF支持不支持這兩個(gè)東東了。仔細(xì)看了一下.NET CF中可疑的命名空間,沒想到在Microsoft.WindowsCE.Forms命名空間里面竟然有一個(gè)MessageWindow 類,太好了,只剩下一個(gè)Shell_NotifyIcon 函數(shù)了。接著   在Window CE的SDK的幫助文件里,又發(fā)現(xiàn)Window CE Platform API已經(jīng)包含了Shell_NotifyIcon函數(shù)。兩大“主料”都齊了,只剩下鍋了。
    先看一下MessageWindow類,這個(gè)類提供了 WndPRoc 方法,用于處理窗口消息,并公開了可能傳遞給本機(jī)窗口函數(shù)的有效窗口句柄。要使用它,派生一個(gè)新類,并重寫的 WndProc 方法,這樣才能截獲特定的窗口消息。這里主要用來處理click事件。
        Shell_NotifyIcon的用法如下:

[DllImport("coredll.dll")]
internal static extern int Shell_NotifyIcon(int dwMessage, ref  NOTIFYICONDATA pnid);

其中,NOTIFYICONDATA結(jié)構(gòu)如下:

struct NOTIFYICONDATA
{
    int cbSize;
    IntPtr hWnd;
    uint uID;
    uint uFlags;
    uint uCallbackMessage;
    IntPtr hIcon;
}
     Pnid參數(shù)的生命需要注意,是按引用傳遞的,因?yàn)镾hell_NotifyIcon 需要一個(gè)指向 NOTIFYICONDATA 結(jié)構(gòu)的指針。
    hWnd是用來接收任務(wù)欄中圖標(biāo)單擊消息的窗口的句柄。
運(yùn)行示例的時(shí)候由于窗體最大化,擋住了任務(wù)欄,把窗體最小化之后就能看到托盤圖標(biāo)了。(效果圖片竟然貼不上來,改天再貼吧)
該類和示例的下載地址:http://m.survivalescaperooms.com/Files/ttinfo/NotifyIconCf.rar

下面是NotifyIcon類的實(shí)現(xiàn),別忘了引用Microsoft.WindowsCE.Forms。注意Add方法提供了不同的重載形式,具體請(qǐng)參看注釋:


using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace NotifyClient
{
    /**//// <summary>
    /// 智能設(shè)備托盤圖標(biāo)類
    /// </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>
        /// 添加托盤圖標(biāo)
        /// </summary>
        /// <param name="hIcon">icon文件的有效句柄</param>
        public void Add(IntPtr hIcon)
        {
            NotifyMessage(messageWindow.Hwnd, NIM_ADD, (uint)uID, hIcon);
        }
        /**//// <summary>
        /// 添加托盤圖標(biāo)
        /// </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>
        /// 添加托盤圖標(biāo)
        /// </summary>
        /// <param name="icon">icon文件</param>
        public void Add(System.Drawing.Icon icon)
        {
            NotifyMessage(messageWindow.Hwnd, NIM_ADD, (uint)uID, icon.Handle);
        }
        /**//// <summary>
        /// 添加托盤圖標(biāo);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

    }
}
http://m.survivalescaperooms.com/ttinfo/archive/2006/10/31/545741.html


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 平乐县| 临泽县| 永兴县| 苗栗县| 江门市| 山阴县| 光泽县| 崇左市| 蕲春县| 石门县| 姚安县| 双牌县| 承德县| 瑞丽市| 明水县| 克拉玛依市| 莱西市| 龙井市| 岚皋县| 镇平县| 屏东县| 颍上县| 隆子县| 海城市| 巴中市| 于田县| 太原市| 泰和县| 咸丰县| 榕江县| 会泽县| 芒康县| 儋州市| 上饶县| 阜阳市| 沙湾县| 会昌县| 望谟县| 长岛县| 石棉县| 斗六市|