首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 於智慧裝置實作 NotifyIcon 元件:托盤圖示與事件處理 (第 2 部分)

     編輯:關於C語言

完整 NotifyIcon 類別實作

針對 .NET Compact Framework 缺乏 NotifyIcon 控制項,透過 P/Invoke 呼叫 coredll.dll 中的 Shell_NotifyIcon,並搭配 Microsoft.WindowsCE.Forms.MessageWindow 接收視窗訊息。

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

namespace NotifyClient
{
    public class NotifyIcon
    {
        public event EventHandler Click;
        private MyMessageWindow msgWin;
        private int uid = 5000;
        private Icon _icon;

        public NotifyIcon() { msgWin = new MyMessageWindow(this); msgWin.UID = uid; }

        public Icon Icon { set { _icon = value; } }

        ~NotifyIcon() { Remove(); }

        // 多載 Add 方法:支援 Handle、資源名稱、Icon 物件、屬性 Icon
        public void Add(IntPtr hIcon) { NotifyMsg(msgWin.Handle, NIM_ADD, (uint)uid, hIcon); }
        public void Add(string resName) { IntPtr h = LoadIcon(GetModuleHandle(null), resName); NotifyMsg(msgWin.Handle, NIM_ADD, (uint)uid, h); }
        public void Add(Icon icon) { NotifyMsg(msgWin.Handle, NIM_ADD, (uint)uid, icon.Handle); }
        public void Add() { if (_icon != null) NotifyMsg(msgWin.Handle, NIM_ADD, (uint)uid, _icon.Handle); }

        public void Remove() { NotifyMsg(msgWin.Handle, NIM_DELETE, (uint)uid, IntPtr.Zero); }
        public void Modify(IntPtr hIcon) { NotifyMsg(msgWin.Handle, NIM_MODIFY, (uint)uid, hIcon); }

        private void NotifyMsg(IntPtr hwnd, int msg, uint uid, IntPtr hIcon)
        {
            NOTIFYICONDATA nid = new NOTIFYICONDATA();
            nid.cbSize = 152; nid.hIcon = hIcon; nid.hWnd = hwnd;
            nid.uCallbackMessage = WM_NOTIFY_TRAY; nid.uFlags = NIF_MESSAGE | NIF_ICON; nid.uID = uid;
            Shell_NotifyIcon(msg, ref nid);
        }

        #region P/Invoke 定義
        const int NIF_MESSAGE = 0x1, NIF_ICON = 0x2, WM_LBUTTONDOWN = 0x201;
        const int NIM_ADD = 0, NIM_MODIFY = 1, NIM_DELETE = 2;
        const int WM_NOTIFY_TRAY = 0x400 + 2001;

        [StructLayout(LayoutKind.Sequential)]
        struct NOTIFYICONDATA
        {
            public int cbSize; public IntPtr hWnd; public uint uID; public uint uFlags;
            public uint uCallbackMessage; public IntPtr hIcon;
        }

        [DllImport("coredll.dll")] static extern int Shell_NotifyIcon(int msg, ref NOTIFYICONDATA nid);
        [DllImport("coredll.dll")] static extern IntPtr LoadIcon(IntPtr hInst, string name);
        [DllImport("coredll.dll")] static extern IntPtr GetModuleHandle(string mod);
        #endregion

        #region 內部訊息視窗
        class MyMessageWindow : Microsoft.WindowsCE.Forms.MessageWindow
        {
            private NotifyIcon owner; private int _uid;
            public MyMessageWindow(NotifyIcon n) { owner = n; }
            public int UID { set { _uid = value; } }
            protected override void WndProc(ref Microsoft.WindowsCE.Forms.Message m)
            {
                if (m.Msg == WM_NOTIFY_TRAY && (int)m.LParam == WM_LBUTTONDOWN && (int)m.WParam == _uid)
                    owner.Click?.Invoke(owner, EventArgs.Empty);
            }
        }
        #endregion
    }
}

使用方式

NotifyIcon ni = new NotifyIcon();
ni.Icon = new Icon("app.ico");
ni.Click += (s, e) => MessageBox.Show("托盤圖示被點擊");
ni.Add(); // 顯示於托盤

注意事項

  • 需參考 Microsoft.WindowsCE.Forms.dll。
  • 表單最大化會遮蔽任務列,需最小化才能看見托盤圖示。
  • 資源圖示名稱格式如 "#201547" (編譯後資源 ID)。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved