Windows tray programming !!!
I. Introduction to pallets
The so-called "Tray" refers to the part of the system time mark on the right of the following task bar on the Windows system interface. When the program is minimized or suspended, but you do not want to occupy the taskbar, you can put the program in the tray area. In fact, the programming of the tray area is very simple. The following briefly introduces ^_^
Ii. functions related to pallet Programming
In fact, the essence of putting a program on a tray is to first draw an icon in the tray area, then hide the program, and then process the message of the tray icon.
There is only one function for drawing the icon and determining the message sent by the icon, That Is ------
Winshellapi bool winapi shell_policyicon (
DWORD dwmessage,
Pnotifyicondata pnid
);
This function is used to send messages to the system to add, modify, or delete icons in the tray area.
Her return value is boolean. That is to say, if 0 is returned, it is Cheng Ren. If not 0, it is successful.
The dwmessage parameter indicates the application function of the function, whether to add, delete, or modify the icon. If it is added, its value is nim_add; if it is deleted, it is nim_delete; if it is deleted, it is changed to nim_modify.
The pnid parameter is a specific structure related to the icon of the program in the tray area. It is defined as follows:
Typedef struct _ policyicondata {
DWORD cbsize;
Hwnd;
Uint uid;
Uint uflags;
Uint ucallbackmessage;
Hicon;
Char sztip [64];
} Notifyicondata, * ppolicyicondata;
The following describes the parameters of the structure:
Cbsize: the length of the structure. The unit is bit. In the program, we use (DWORD) sizeof (policyicondata) to assign values to it.
Hwnd: a handle. If you operate the icon in the tray, the corresponding message is sent to the window represented by this handle. Naturally, in most cases it is this-> m_hwnd.
UID: the icon ID defined in the project.
Uflags: indicates which other Members have valid data, such as nif_icon, nif_message, and nif_tip. The valid data members are hicon, ucallbackmessage, and sztip. Of course, the three values can be associated with "|. The following describes the involved members.
Hicon: the icon handle to be added, deleted, or modified. If only uid is known, the loadicon function may be used to obtain the handle. For example, loadicon (AfxGetInstanceHandle (), makeintresource (idr_mainframe )).
Ucallbackmessage: this is an important data member in operations on the pallets. This is a message sign. When you use the mouse to operate the corresponding icon in the tray area, the message will be transmitted to the window represented by hwnd. Therefore, in uflags, it is generally valid. Generally, custom messages are used here.
Sztip: The text displayed when you move the mouse over the tray icon.
Iii. Tray programming example
The basic knowledge about pallet programming is the above. Next, we will enter the actual practice phase. Here are some examples of tray programming to help us better understand.
1. The totray () function that minimizes the program to the system tray area ().
Void ctimewakedlg: 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, "scheduled task reminder"); // The message is scheduled task reminder"
Shell_policyicon (nim_add, & nid); // Add an icon in the tray Area
Showwindow (sw_hide); // hide the Main Window
}
This is a very simple function. It first assigns values to policyicondata and then calls shell_policyicon. The first parameter is nim_add, which indicates adding. Then, use the showwindow function to hide the main window. In this way, the task of minimizing the program to the system tray area is realized.
2. The program has been minimized to the tray area. But how can I operate the tray icon? This reflects the role of ucallbackmessage, a member of policyicondata. The function provided by the tool is to send messages to the window represented by hwnd when you click the icon in the tray area with the mouse (whether left or right, the message name is wm_showtask. Based on the message mechanism of VC, add a message response function for custom messages.
Declare the message response function between // {afx_msg and //} afx_msg in the header file:
Afx_msg lresult onshowtask (wparam, lparam );
Then add message ing to the CPP file. Add the following between begin_message_map and end_message_map:
On_message (wm_showtask, onshowtask) maps messages and message response functions.
Then, the onshowtask function is added to the CPP file:
Lresult ctimewakedlg: onshowtask (wparam, lparam)
// Wparam receives the icon ID, while lparam receives the mouse action.
{
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.
}
Break;
}
Return 0;
}
It's over. There's nothing to say.