VC ++ System Tray

Source: Internet
Author: User

(1) Principles

1. Principle of minimization: First hide the window, and then draw the icon in the lower right corner.

2. Restoration principle: display the window and delete the images in the tray.


(2) Program Implementation

1. Custom message wm_showtask: # define wm_showtask (wm_user + 1)

2. Add a command response in the: onsyscommand (uint NID, lparam) function body of MFC.

If (nid = SC _minimize)
Totray (); // the function that minimizes data to the tray.

3. Add on_message (wm_showtask, onshowtask) to message ing. Where wm_showtask is the message name, And onshowtask is the message response function defined by myself.


(3) specific function content

1. minimize to the tray Function

Void cmydlg: totray ()
{
Notifyicondata NID;
Nid. cbsize = (DWORD) sizeof (policyicondata );
Nid. hwnd = This-> m_hwnd;
Nid. uid = idr_mainframe;
Nid. uflags = nif_icon | nif_message | nif_tip;
Nid. ucallbackmessage = wm_showtask; // custom message name
Nid. hicon = loadicon (AfxGetInstanceHandle (), makeintresource (idr_mainframe ));
Strcpy (NID. sztip, "program name"); // message tip
Shell_policyicon (nim_add, & nid); // Add an icon in the tray Area
Showwindow (sw_hide); // hide the Main Window
}

2. The restore interface function defines the message Response Function in the header file.

Afx_msg lresult onshowtask (wparam, lparam); // wparam receives the icon ID, while lparam receives the mouse action.
Lresult cmydlg: onshowtask (wparam, lparam)
{
If (wparam! = Idr_mainframe)
Return 1;
Switch (lparam)
{
Case wm_rbuttonup: // The shortcut menu is displayed when you right-click it. There is only one "off"
{

Lppoint lpoint = new tagpoint;
: Getcursorpos (lpoint); // get the cursor position
Cmenu menu;
Menu. createpopupmenu (); // declare a pop-up menu
// Add the menu item "close" and click it to send the message wm_destroy to the main window (already
// Hide) to end the program.
Menu. appendmenu (mf_string, wm_destroy, "close ");
// Determine the position of the pop-up menu
Menu. trackpopupmenu (tpm_leftalign, lpoint-> X, lpoint-> Y, this );
// Reclaim Resources
Hmenu = menu. Detach ();
Menu. destroymenu ();
Delete lpoint;
}
Break;
Case wm_lbuttondblclk: // double-click the left button.
{
This-> showwindow (sw_show); // a simple display of the main window is complete.
Deletetray ();
}
Break;
Default:
Break;
}
Return 0;
}

3. Delete the tray icon function void cmydlg: deletetray ()
{
Notifyicondata NID;
Nid. cbsize = (DWORD) sizeof (policyicondata );
Nid. hwnd = This-> m_hwnd;
Nid. uid = idr_mainframe;
Nid. uflags = nif_icon | nif_message | nif_tip;
Nid. ucallbackmessage = wm_showtask; // custom message name
Nid. hicon = loadicon (AfxGetInstanceHandle (), makeintresource (idr_mainframe ));
Strcpy (NID. sztip, "program name"); // The message is scheduled task reminder"
Shell_policyicon (nim_delete, & nid); // Delete the icon in the tray Area
}

  1. /*** Message of the taskbar status area to be processed by the system ***/

  2. Typedef struct _ policyicondata {

  3. DWORD cbsize; // the size of the struct, in bytes

  4. Hwnd; // window handle

  5. Uint uid; // The identifier of the taskbar icon defined by the application

  6. Uint uflags; // indicates which other Members are valid data.

  7. Uint ucallbackmessage; // Message ID defined by the application

  8. Hicon; // the handle of the added, modified, or deleted icon.

  9. Tchar sztip [64]; // pointer to a string ending with \ 0

  10. DWORD dwstate; // version 5.0, icon status

  11. DWORD dwstatemask; // version 5.0. Specify the bits of the dwstate member that can be set or accessed.

  12. Tchar szinfo [256]; // pointer to a string ending with \ 0. The content of the string is the content of the balloon prompt.

  13. Union {

  14. Uint utimeout; // indicates the timeout time of the balloon prompt, in milliseconds. After this time, the balloon prompt will disappear.

  15. Uint uversion; // used to set the icon message interface in the Windows 95 or Windows 2000 Style

  16. };

  17. Tchar szinfotitle [64]; // pointer to a string ended with \ 0. The content of the string is the title of the balloon prompt

  18. DWORD dwinfoflags; // set this member to add an icon to the balloon prompt box. The added icon appears on the left side of the balloon prompt title.

  19. Guid guiditem; // Reserved

  20. Hicon hballoonicon; // a custom balloon icon for Windows Vista or later

  21. } Notifyicondata, * ppolicyicondata;


Note: in a single document, add the onsyscommd message response to the framework class.
Single-document pallet Program


1. notifyicondata Structure

---- The notifyicondata structure contains the information used by the system to process the tray icons, including the selected icons, callback messages, prompt messages, and corresponding windows of the icons. It is defined:

Typedef struct _ policyicondata {
DWORD cbsize;
// The size of the structure in bytes
Hwnd;
// Handle of the window for receiving Tray Icon notification messages
Uint uid; // ID of the icon defined by the application
Uint uflags;
// Set attributes of the icon
Uint ucallbackmessage;
// Message ID defined by the application. The message is sent to hwnd.
Hicon;
// Handle of the icon
Char sztip [64];
// The prompt message displayed when you move the cursor over the icon
} Notifyicondata, * ppolicyicondata;
In this structure, the uflags member can make one or more of the following combinations:
The specified hicon member is valid.
The value of the nif_message setting Member ucallbackmessage is valid.
The error message returned when the member sztip is set to valid.

---- 2. shell_policyicon Function
---- The global function shell_policyicon () is used to add, delete, or modify icons on the tray. Its prototype is:

Winshellapi bool winapi
Shell_policyicon (DWORD dwmessage,
Pnotifyicondata pnid );

---- Pnid is the pointer to the above notifyicondata structure; dwmessage is the passed message, which can be one of the following messages:
Nim_add add icon
Nim_delete Delete icon
Nim_modify

---- 3. Tray Icon Program Design Example
---- First, we use Appwizard to create an application tray that is not based on the document/view structure. We do not want to display the main window when the application starts. Therefore, delete the following two sentences in the initinstance () function of the application class ctrayapp to display the code in the main window:

Pframe-> activateframe ();
Pframe-> showwindow (sw_show );
Protection member variable for adding the policyicondata structure to the cmainframe class
M_tnid, and return in its oncreate Function
Code for generating the tray icon before the statement:

M_tnid.cbsize = sizeof (policyicondata );
M_tnid.hwnd = This-> m_hwnd;
M_tnid.uflags = nif_message | nif_icon | nif_tip;
M_tnid.ucallbackmessage = mywm_policyicon;
// User-defined callback message
Cstring sztooltip;
Sztooltip = _ T ("tray icon instance ");
_ Tcscpy (m_tnid.sztip, sztooltip );
M_tnid.uid = idr_mainframe;
Hicon;
Hicon = afxgetapp ()-> loadicon (idr_mainframe );
M_tnid.hicon = hicon;
: Shell_policyicon (nim_add, & m_tnid );
If (hicon): destroyicon (hicon );

---- The ID of the callback message should be defined in the header function of the main framework class:
# Define mywm_policyicon wm_user + 1

---- To process the icon callback message, such as double-clicking with the left mouse button or right-clicking the message, we reload the windowproc () function. In addition, we also hope that the icon will not appear in the blank area of the taskbar when the main frame window is minimized, and the corresponding processing will be done in this function at the same time.
Lresult cmainframe: windowproc
(Uint message, wparam, lparam
Lparam)
{
Switch (Message ){

Case mywm_policyicon:
// For user-defined messages
If (lparam = wm_lbuttondblclk)
{// The main window appears when you double-click the mouse
Afxgetapp ()-> m_pmainwnd->
Showwindow (sw_show );
}
Else if (lparam = wm_rbuttondown ){
// Right-click the pop-up menu
Cmenu menu;

Menu. loadmenu (idr_right_menu );
// Load the predefined menu
Cmenu * pmenu = menu. getsubmenu (0 );
Cpoint Pos;
Getcursorpos (& Pos );
Pmenu-> trackpopupmenu
(Tpm_leftalign | tpm_rightbutton,
POs. X, POS. Y, afxgetmainwnd ());
}
Break;
Case wm_syscommand:
// If it is a system message
If (wparam = SC _minimize ){
// The main window is hidden when the minimum message is received
Afxgetapp ()-> m_pmainwnd->
Showwindow (sw_hide );
Return 0;
}
Break;
}
Return cframewnd: windowproc
(Message, wparam, lparam );
}

---- To cause the icon to disappear when the application exits, map the wm_destroy message and add it to the ondestroy () function:
: Shell_policyicon (nim_delete, & m_tnid );

---- Now, we have implemented common functions of the tray icon program. We can also use the shell_policyicon () function to call different status indicators, just as Kingsoft's icon changes when the main window is opened and when the word is paused.
---- This program is debugged in VC ++ 6.0, Windows98/2000 Professional.

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.