Programmatically display system tray icon on Windows Mobile

Source: Internet
Author: User
ArticleDirectory
    • 1. Declare some variables and function prototypes
    • 2. Implement a wrapper function to show and hide tray icon.
    • 3. Call showtrayicon function in wm_create Handler
    • 4. Add handler for wm_javasray_msg
    • 5. Getting the tap coordinates on System Tray Icon

As a Windows Mobile User you wocould see have some application specific icons near to the bottom right corner of the today screen. it give users ability to directly launch the application or show an menu with more choices.

Tray icons on today screen (Dell Axim)

This post discusses the steps and code to put your own icon in the system tray. I will also provide a solution to a common issue with Windows Mobile whereby its not easy to know the coordinates of the icon or the coordinates where the user might have tapped within the tray area.

Okay... I will take an example of a Win32 native application. The concepts will hold good even if you are programming with some other framework.

1. Declare some variables and function prototypes

// Some required defines
# Define wm_systray_msg wm_user + 1
# Define id_tray 1

// Some variables
Static policyicondata g_structpolicyicondata = {0 };
Static hwnd g_hwndmain = NULL;
Static hwnd g_hwnd = NULL;
Static wndproc g_fnproc = NULL;
Static DWORD g_dwtappos = 0;

Lresult implements topologerwindowproc (hwnd, uint MSG, wparam, lparam );

2. Implement a wrapper function to show and hide tray icon.

Bool showtrayicon (hwnd, bool bshowicon)
{
Bool Bret = false;

G_structpolicyicondata.cbsize = sizeof (policyicondata );
G_structpolicyicondata.hicon = loadicon (g_hinst, makeintresource (idi_showtrayicon ));
G_structpolicyicondata.hwnd = hwnd;
G_structpolicyicondata.ucallbackmessage = wm_policray_msg;
G_structpolicyicondata.uflags = nif_message | nif_icon;
G_structpolicyicondata.sztip [0] = ";
G_structpolicyicondata.uid = id_tray;

If (bshowicon)
Bret = shell_policyicon (nim_add, & g_structpolicyicondata );
Else
Bret = shell_policyicon (nim_delete, & g_structpolicyicondata );

Return Bret;
}

Above function fills up fields in policyicondata structure, setting the icon resource handle, parent window handle, callback message identifier, flags specifying that ucallbackmessage and Icon information is being set and an user defined id value.

3. Call showtrayicon function in wm_create Handler

In the wm_create handler portion of the main wndproc function, call showtrayicon (hwnd, true ).

Case wm_create:

Showtrayicon (hwnd, true );

...

...

4. Add handler for wm_javasray_msg

Wm_javasray_msg will be sent to the main window procedure (wndproc) when the user taps or clicks on the icon. we must add the following code to make some good use of the icon.

switch (Message)
{< br> case wm_1_ray_msg:
{< br> switch (lparam)
{< br> case wm_lbuttondown:
If (id_tray = wparam)
{< br> bool Bret = false;
point Pt = {0 };
hmenu htraymenu = loadmenu (g_hinst, makeintresource (idr_tray_menu);
If (htraymenu)
{< br> hmenu hsubmenu = getsubmenu (htraymenu, 0);
PT. X = loword (g_dwtappos);
PT. y = hiword (g_dwtappos);
Bret = trackpopupmenu (hsubmenu, tpm_centeralign | tpm_bottomalign, PT. x, PT. y, 0, hwnd, null);
dwerror = getlasterror ();
destroymenu (hsubmenu);
destroymenu (htraymenu );
}< BR >}< br> break;

Here we know that user has tapped on the icon area. as a result we can do anything that we might want. here I an showing up a popup menu using trackpopupmenu API. g_dwtappos variable contains the X & Y position where the user tapped on the system tray area. we will see later how we get the information in g_dwtappos. note that getcursorpos () does not get us this position value on Windows Mobile whereas it works on Windows desktop.

5. Getting the tap coordinates on System Tray Icon

One way I found to know the coordinates where the user tapped or clicked on the taskbar is to subclass the specified toprefererwindow and trap wm_cancelmode message. in the handler for wm_cancelmode, calling getmessagepos () will return the position into a g_dwtappos variable.

//
// Specify topologerwindow
//
Lresult implements topologerwindowproc (hwnd, uint MSG, wparam, lparam)
{
Switch (MSG)
{
// Trap wm_cancelmode message
// This will get us the point at which TAP occurred
Case wm_cancelmode:
G_dwtappos = getmessagepos ();
Break;
}

Return: callwindowproc (g_fnproc, hwnd, MSG, wparam, lparam );
}

Bool hook?top=erwindow ()
{
If (g_fnproc)
Return false;

G_hwnd =: findwindow (_ T ("desktopexplorerwindow"), null );
If (g_hwnd)
{
G_fnproc = (wndproc): setwindowlong (g_hwnd, gwl_wndproc, (long) reply topexplorerwindowproc );
}

Return g_hwnd! = NULL;
}

Bool freeze topologerwindow ()
{
If (! G_fnproc)
Return false;

: Setwindowlong (g_hwnd, gwl_wndproc, (long) g_fnproc );
G_fnproc = NULL;

Return true;
}

Hook?top=erwindow can be called from wm_create handler in main window procedure before calling showtrayicon.Freeze topologerwindow can be called from wm_destroy handler inMain Window procedure.

See below how the icon and popup menu shows up in my sample application.

Sample Application Showing System Tray Icon

 

 

Possibly related posts: (automatically generated)

    • Creating a system tray icon using Visual C ++ and Win32
    • Windows 7 beta help files are still a work in progress

 

From:

Http://knowledgepointer.wordpress.com/2008/12/13/programmatically-display-system-tray-icon-on-windows-mobile/

 

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.