程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 用C#實現智能設備上的NotifyIcon類

用C#實現智能設備上的NotifyIcon類

編輯:關於C語言

前幾天有網友問.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方法提供了不同的重載形式,具體請參看注釋:

http://www.mscto.com

using System;

using System.Runtime.InteropServices;

using System.Windows.Forms;

namespace NotifyClIEnt

{

/**////

/// 智能設備托盤圖標類

///

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();

} http://www.mscto.com

/**////

/// 添加托盤圖標

///

/// icon文件的有效句柄

public void Add(IntPtr hIcon)

{

NotifyMessage(messageWindow.Hwnd, NIM_ADD, (uint)uID, hIcon);

}

/**////

/// 添加托盤圖標

///

/// 編譯之後的資源文件中的icon資源名稱,如“#201547”

public void Add(string IconRes)

{

IntPtr hIcon = LoadIcon(GetModuleHandle(null), IconRes);

NotifyMessage(messageWindow.Hwnd, NIM_ADD, (uint)uID, hIcon);

}

/**////

/// 添加托盤圖標

///

/// icon文件

public void Add(System.Drawing.Icon icon)

{

NotifyMessage(messageWindow.Hwnd, NIM_ADD, (uint)uID, icon.Handle);

}

/**////

/// 添加托盤圖標;icon為屬性中的icon

///

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;

}

http://www.mscto.com

[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);

http://www.mscto.com

[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

}

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved