Using C # to implement NotifyIcon classes on smart devices

Source: Internet
Author: User
Tags implement reference resource valid

A few days ago, a netizen asked. NET CF how to implement NotifyIcon, I just know the original. NET CF does not provide a NotifyIcon control.
So I think the PC can use Shell_NotifyIcon and MessageWindow to implement the tray icon, just do not know. NET CF Support does not support these two things. Take a closer look at the suspicious namespaces in the. NET CF, and unexpectedly there is a MessageWindow class inside the Microsoft.WindowsCE.Forms namespace, so good that there is only one Shell_NotifyIcon function left. Then in the Windows CE SDK Help file, also found that the window CE Platform API already contains the Shell_NotifyIcon function. The two major "ingredients" are all together, only the pot left.
First look at the MessageWindow class, which provides a WndProc method for processing window messages and exposes valid window handles that may be passed to the native window function. To use it, derive a new class and rewrite the WndProc method so that you can intercept specific window messages. This is primarily used to handle click events.
The use of Shell_NotifyIcon is as follows:

[DllImport ("Coredll.dll")]
internal static extern int Shell_NotifyIcon (int dwmessage, ref notifyicondata Pnid);

Among them, the NOTIFYICONDATA structure is as follows:

struct NOTIFYICONDATA
{
int cbsize;
IntPtr hWnd;
UINT UID;
UINT Uflags;
UINT Ucallbackmessage;
IntPtr Hicon;
}
The life of the Pnid parameter needs to be noted and passed by reference because Shell_NotifyIcon requires a pointer to the NOTIFYICONDATA structure.
An HWND is a handle to a window that receives an icon in the taskbar to click a message.
When running the example, because the form is maximized, the taskbar is blocked, and the tray icon is visible after the form is minimized. (The effect picture unexpectedly pastes not to come up, another day pastes bar)
Download address for this class and example: Http://www.cnblogs.com/Files/ttinfo/NotifyIconCf.rar

Here is the implementation of the NotifyIcon class, and don't forget to reference Microsoft.WindowsCE.Forms. Note The Add method provides a different form of overloading, as detailed in the comments:


Using System;
Using System.Runtime.InteropServices;
Using System.Windows.Forms;

Namespace Notifyclient
{
/**////<summary>
Smart Device tray icon class
</summary>
public class NotifyIcon
{
Click event
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>
Add tray icon
</summary>
<param name= "Hicon" >icon file's valid handle </param>
public void Add (IntPtr hicon)
{
Notifymessage (Messagewindow.hwnd, Nim_add, (UINT) UID, hicon);
}
/**////<summary>
Add tray icon
</summary>
<param name= "iconres" > Icon resource name in the resource file after compilation, such as "#201547" </param>
public void Add (string iconres)
{
IntPtr Hicon = LoadIcon (GetModuleHandle (null), iconres);
Notifymessage (Messagewindow.hwnd, Nim_add, (UINT) UID, hicon);
}
/**////<summary>
Add tray icon
</summary>
<param name= "icon" >icon file </param>
public void Add (System.Drawing.Icon Icon)
{
Notifymessage (Messagewindow.hwnd, Nim_add, (UINT) UID, icon. Handle);
}
/**////<summary>
Add tray icon; icon in attribute
</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
Defining Message Constants
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;

Custom messages
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

}
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.