Original article address: VC tray Programming
In VC programming, Tray operations are often involved. For example, adding a tray icon for our program, adding a left-click response to the tray, or right-clicking the menu, we often use QQ as a tray program.
Now let's take a look at the notes for pallet programming in VC. First, we need to figure out a struct-notifyicondata, which is the key to pallet programming.
Typedef struct _ policyicondata {
DWORD cbsize;
Hwnd;
Uint uid;
Uint uflags;
Uint ucallbackmessage;
Hicon;
Tchar sztip [64];
DWORD dwstate;
DWORD dwstatemask;
Tchar szinfo [256];
Union {
Uint utimeout;
Uint uversion;
};
Tchar szinfotitle [64];
DWORD dwinfoflags;
Guid guiditem;
} Notifyicondata, * ppolicyicondata;
Let's take a look at what each member variable represents. In fact, in general, you can pay attention to the first 10 members in the application. The following members are rarely used.
The size of the cbsize struct, in bytes.
The handle of the hwnd window. The marked window is used to receive messages related to the tray icon. When the shell_policyicon function is called, The hwnd and uid members are used to indicate the icons to be operated.
The identifier of the taskbar icon defined by the UID application. When the shell_policyicon function is called, The hwnd and uid members are used to indicate the icons to be operated. By calling multiple times, you can use different UID to associate multiple icons to a window hwnd.
Uflags indicates which other Members are valid (that is, which members are valid ). This member can be a combination of the following values:
The nif_icon hicon member takes effect.
The nif_message ucallbackmessage member takes effect.
The member of the sz_tip sztip function.
The nif_state dwstate and dwstatemask members work.
Nif_info uses the balloon prompt instead of the normal tool prompt box. The szinfo, utimeout, szinfotitle, and dwinfoflags members work.
Nif_guid is retained.
Ucallbackmessage
The message identifier defined by the application. When a mouse event occurs in the tray icon area or the icon is selected or activated on the keyboard, the system uses this identifier to send a message to the window marked by the hwnd member. The wparam parameter of the message response function indicates the taskbar icon of a message event. The lparam parameter contains the mouse or keyboard-specific message based on the event, for example, when the mouse pointer moves over the tray icon, lparam is wm_mousemove.
The handle of the icon added, modified, or deleted by hicon. Note that different versions of Windows have different icon requirements. Windows XP supports 32 bits.
SzTip points to a pointer to a string ending with \ 0. The content of the string is the standard tooltip information. Contains the last \ 0 characters, and the szTip can contain up to 64 characters. For Version 5.0 and later versions, szTip can contain a maximum of 128 characters (including the last \ 0 characters ).
DwState Version 5.0, icon status, which has two optional values:
NIS_HIDDEN icon hidden
Nis_nvidicon icon sharing
DwStateMask Version 5.0. Specify the bits of the dwState member that can be set or accessed. For example, if this member is set to NIS_HIDDEN, only the hidden status can be obtained.
SzInfo Version 5.0. pointer to a string ending with \ 0. The content of the string is the prompt content of the balloon. It can contain up to 255 characters. If you want to remove an existing balloon prompt, set the uFlags member to NIF_INFO and set szInfo to null.
UTimeout and uVersion members are consortium. UTimeout indicates the timeout time of the balloon prompt, in milliseconds. After this time, the balloon prompt will disappear. By default, the system prompts a minimum timeout value of 10 seconds and a maximum value of 30 seconds. If the uTimeout value is less than 10, the minimum value is set. If the value is greater than 30, the maximum value is set. The timeout time can be divided into two types: maximum and minimum, because the balloon prompts of different icons are displayed at the same time. For details, refer to the remarks in the NOTIFYICONDATA struct description in MSDN.
UVersion Version 5.0. And uTimeout are federated. It is used to set the icon message interface in the Windows 95 or Windows 2000 style. For more information, see the description of the shell_policyicon function. This member is valid only when the shell_policyicon function is used to send the NIM_SETVERSION message. Optional values:
0 adopts the Windows 95 style. Use this value for software designed for Windows earlier than windows 2000.
Policyicon_version uses the Windows 2000 style. Use this value for software designed for Windows 2000 and later versions.
Szinfotitle version 5.0. pointer to a string ending with \ 0. The content of the string is the title of the balloon prompt. This title appears in the upper part of the balloon prompt box, containing a maximum of 63 characters.
Dwinfoflags version 5.0. Set this member to add an icon to the balloon prompt box. The added icon appears on the left of the title of the balloon prompt. Note that if the szinfotitle member is set to an empty string, the icon is not displayed. Optional values:
Niif_error icon.
Niif_info icon.
Niif_none has no icon.
Niif_user uses the icon specified by the hicon member and requires Windows XP Service Pack 2 (SP2) or a later system.
Niif_warning warning icon.
Niif_icon_mask version 6.0. retained.
Niif_nosound version 6.0. The corresponding sound cannot be played.
Guiditem version 6.0. Reserved.
There are many Members, so the description is quite long. As I mentioned above, just clarify the meaning of the representatives of the first 10 members, which is not commonly used. Now, let's take a look at the general steps of pallet programming. First, declare a global policyicondata struct variable in the dialog box in our test project (MFC Application:
Notifyicondata m_skynid;
Then initialize in the oninitdialog function:
M_skynid.cbsize = (DWORD) sizeof (policyicondata );
M_skynid.hwnd = This-> m_hwnd;
M_skynid.uid = idr_skynid_menu;
M_skynid.uflags = nif_icon | nif_message | nif_tip;
M_skynid.dwstate = nis_1_dicon;
M_skynid.ucallbackmessage = wm_taskmanage;
M_skynid.hicon = loadicon (AfxGetInstanceHandle (), makeintresource (idr_mainframe ));
_ Tcscpy (m_skynid.sztip, _ T ("system status-not logged on "));
Shell_policyicon (nim_add, & m_skynid );
* ************************** The above Code is compiled incorrectly under vc6.0, you can refer to the solution section ********************************* in vc6.0 tray programming ****************************
The initialization code above is easy to understand. m_skynid.uid can be customized as long as it is unique. In the last sentence, use Windows API shell_policyicon to add the tray icon represented by the initialized struct variable to the system tray. In this way, your application Tray Icon will appear in the lower right corner of your desktop. Of course, this icon will not be automatically deleted when the program exits. You need to add the processing code when the program exits. In this way, add the code to the ondestroy function:
Shell_policyicon (nim_delete, & m_skynid );
At this point, your program can automatically create and delete icons in the system tray when it starts and exits. Next we will add a right-click menu for this tray. Add a menu to the resource. Here, the resource ID is IDR_SKYNID_MENU.
In the initialization code, m_SkyNID.uCallbackMessage = WM_TASKMANAGE sets a mouse event in the tray icon area or uses the keyboard to select or activate the icon, the windows marked by hWnd will receive the WM_TASKMANAGE message (this is a custom message # define WM_TASKMANAGE WM_USER + 100). Next we will respond to this message in the corresponding window and add message ing in the message ing macro.
ON_MESSAGE (WM_TASKMANAGE, OnTaskManage)
And add the message response function OnTaskManage.
LRESULT CSkyTestDlg: OnTaskManage (WPARAM wParam, LPARAM lParam)
{
If (wParam! = IDR_SKYNID_MENU)
Return 1;
Switch (lParam)
{
Case WM_RBUTTONUP:
{
CMenu * pTaskMenu, taskMenu;
CPoint pMousePosition;
TaskMenu. LoadMenu (IDR_SKYNID_MENU );
PTaskMenu = taskMenu. GetSubMenu (0 );
/***** For simple testing, you can also load the pTaskMenu into the system menu pTaskMenu = GetSystemMenu (false )**********/
GetCursorPos (& pMousePosition );
SetForegroundWindow ();
PTaskMenu-> TrackPopupMenu (TPM_RIGHTALIGN, pMousePosition. x, pMousePosition. y, this );
Break;
}
Case WM_LBUTTONDOWN:
{
Break;
}
}
Return 0;
}
/************************
The IDR_SKYNID_MENU menu involved in the above Code can be edited as follows:
Open the resource file **. rc in notepad, and find the place that represents the menu resource. The edit menu is as follows:
IDR_SKYNID_MENU MENU DISCARDABLE
BEGIN
POPUP "MyMenu"
BEGIN
Menuitem "about", id_about
Menuitem "Restore", id_reset
Menuitem "exit", id_exit
End
End
It's just a pop-up menu.
*******************************/
To briefly explain the code above, first define the cmenu variable taskmenu and loadmenu resource ID as idr_skynid_menu, and then use the cmenu pointer variable to obtain the loaded menu pointer, the trackpopupmenu function is used to display the menu to the specified coordinates. Setforegroundwindow () uses this function to set the menu as a front-end window. This solves the problem that the menu will not automatically disappear when you move the mouse away from the menu and click or double-click the menu. Finally, add the Event Response Processing code for each right-click menu item. Right-click the menu item and choose add event handler to automatically add three codes, take the exit program sub-item in the preceding menu as an example to check the code.
Afx_msg void onskytestexit (); // The response function declaration in the first header file
On_command (id_skytest_exit, & cskytestdlg: onskytestexit) // The second CPP file message ing macro
Void cskytestdlg: onskytestexit () // message response function of the third CPP File
{
If (iswindow (m_skylogindlg.m_hwnd ))
{
M_skylogindlg.sendmessage (wm_close );
}
Enddialog (true );
}
Now, the right-click menu of our pallets can work normally. The article is relatively simple. You are welcome to share with us.
**************************************** **************************************** **************************************** ***************************
The following describes the functions used!
**************************************** **************************************** **************************************** ***************************
1. Description of the shell_policyicon Function Format
Bool shell_policyicon (DWORD dwmessage, pnotifyicondata lpdata );
Parameter description: dwMessage is the input parameter. The sent message is transmitted, indicating the operation to be performed. Optional values:
NIM_ADD
Add an icon to the tray area. In this case, the hWnd and uID members in the NOTIFYICONDATA struct to which the second parameter lpdata points are used to mark this icon so that shell_policyicon can be used again later.
NIM_DELETE
Delete an icon in the tray area. In this case, the hWnd and uID members in the NOTIFYICONDATA struct to which the second parameter lpdata points are used to indicate the icon to be deleted.
NIM_MODIFY
Modify an icon in the tray area. In this case, the hWnd and uID members in the NOTIFYICONDATA struct to which the second parameter lpdata points are used to indicate the icon to be modified.
NIM_SETFOCUS
Version 5.0. Set focus. For example, when you press ESC to remove the menu from the pop-up menu of the Operation tray icon, the program should use this message to set the focus to the tray icon.
NIM_SETVERSION
Version 5.0. Set the taskbar to work according to the Version number specified by the uVersion member in the policyicondata struct to which the second parameter lpdata points. This message allows you to set whether to use the Windows2000 version 5.0-based style. The default value of uVersion is 0, indicating that the original Windows 95 icon message style is used by default. For specific differences between the two, see Remarks in the shell_policyicon Function Description in msdn.
Lpdata is the input parameter, which is a pointer to the NOTIFYICONDATA struct. The struct content is used to perform icon operations with the first parameter wMessage.
Returns TRUE if the icon operation is successful. Otherwise, FALSE is returned.
If the dwMessage parameter is set to NIM_SETVERSION, TRUE is returned if the version is set successfully. If the set version is not supported, FALSE is returned.
**************************************** **************************************** **************************************** ***************************
Let's take a brief introduction. The steps are the same!
**************************************** **************************************** **************************************** ***************************
Original article address: VC ++ MFC dialog box application program minimized to System Tray
Step 1: Define a policyicondata Structure Variable nid. The NOTIFYICONDATA struct definition can be found in MSDN without further introduction.
Step 2: Set policyicondata variable attributes:
Nid. cbSize = sizeof (policyicondata );
Nid. hWnd = m_hWnd;
Nid. uID = 0;
Nid. hIcon = m_hIcon;
Nid. uflags = nif_icon | nif_message | nif_tip | nif_info;
Nid. ucallbackmessage = wm_policyicon; // # define wm_policyicon (wm_user + 101) custom
Wcscpy (NID. sztip, l "this is a tag ");
Wcscpy (NID. szinfo, l "this is a bubble prompt message ");
Wcscpy (NID. szinfotitle, l "this is the title of the bubble prompt ");
Nid. dwinfoflags = niif_info;
Nid. utimeout = 1000;
Step 3: display the icon to the system tray. You only need to call an API.
Shell_policyicon (nim_add, & nid );
Step 4: After the icon is added, we usually need to make it respond to various user operations. For example, double-click the display form, right-click the context menu, and so on.
First, we define a callback function.
Lresult onpolicyicon (wparam, lparam)
{
Switch (lparam) // determine the corresponding event based on lparam
{
Case WM_LBUTTONDBLCLK: // if you left-click the tray icon, the form is displayed.
ShowWindow (SW_SHOWNORMAL );
SetForegroundWindow ();
Break;
Case WM_RBUTTONUP: // if the context menu pops up, the menu is displayed.
CPoint pos;
GetCursorPos (& pos );
If (pMenuContext! = NULL)
{
SetForegroundWindow (); // Add this sentence to make the pop-up menu disappear when the mouse clicks somewhere else
Pmenucontext-> trackpopupmenu (tpm_rightbutton | tpm_rightalign, POS. x + 1, POS. Y + 1, this );
}
Break;
}
Return 0;
}
Add
On_message (wm_policyicon, & onpolicyicon)
Message ing is complete.
We have completed adding a tray icon to the program and can perform certain operations on the application through the icon. However, when the program exits, the system tray icon will not be automatically deleted, therefore, we also need to call: shell_policyicon (nim_delete, & nid); In onclose () to delete the tray icon.