In a Windows operating system, some running Windows are not displayed. Only one icon is displayed on the taskbar, indicating that the program is running. You can use the mouse to interact with the application, such as Kingsoft Antivirus and other applications, sometimes we also need to compile some similar programs that only run in the background. In order not to interfere with the running interface of the foreground program and display unnecessary windows, the main window during program running should be invisible. At the same time, an icon is displayed in the static announcement area on the right side of the taskbar and responds to the user's mouse action. The following describes Visual
C ++ designs such programs.
1. Hide the main window of the program
First, to make the main window of the program invisible and the task button does not appear on the taskbar, you need to set the style and extended style of the main border window respectively:
Bool cmainframe: precreatewindow (createstruct & CS) { CS. Style = ws_popup; // make the main window invisible CS. dwexstyle | = ws_ex_toolwindow; // The task button is not displayed. Return cframewnd: precreatewindow (CS ); } |
2. Add the icon indicating that the program is running to the taskbar
By calling the above function in the cmainframe: oncreate () function of the main framework window, you can display the icon on the job bar. This step uses the system API function shell_policyicon () display an icon in the notification area of the taskbar. The prototype of this function is: Before calling this function, you need to determine the value of its parameter. The first parameter of the shell_policyicon () function is a predefined message. One of the following values can be taken: nim_add, nim_delete, or nim_modify, indicating adding, deleting, or modifying icons respectively. Another parameter is a pointer to the policyicondata type. Its prototype is:
Typedef struct _ policyicondata { DWORD cbsize; Hwnd; Uint uid; Uint uflags; Uint ucallbackmessage; Hicon; Charsztip [64];} Notifyicondata |
Among the members of this structure, cbsize is the number of bytes occupied by this structure, hwnd is the handle of the window that receives the message sent by the icon (when you move the cursor over the program icon on the taskbar, the icon will send a message, which you need to define by yourself ), UID is the ID of the displayed icon. uflags indicates whether the values of several other members (hicon, ucallbackmessage, and sztip) are valid. ucallbackmessage is a user-defined message, when a user uses some mouse actions on the icon, the icon sends the message to the main frame window (the window specified in the hwnd member) of the application ,. Hicon is the handle of the icon displayed on the taskbar, and the string displayed when the sztip mouse stays on the icon.
Int cmainframe: oncreate (maid) { Notifyicondata TND; TND. cbsize = sizeof (policyicondata ); TND. hwnd = This-> m_hwnd; TND. uid = idr_mainframe; TND. uflags = nif_message | nif_icon | nif_tip; TND. ucallbackmessage = wm_mymessage; File: // user-defined message, that is, the message sent by the icon when you move the cursor over the program icon on the taskbar TND. hicon = loadicon (AfxGetInstanceHandle (), makeintresource (idr_mainframe )); Strcpy (TND. sztip, "Test Program"); // The icon prompts "test program" Shell_policyicon (nim_add, & TND); // Add an icon to the taskbar} |
3. Implementation of user-program interaction
The user interacts, that is, when the user clicks the icon or double-click the left mouse button or right-click the icon to perform the corresponding operation, at least to respond to the user's willingness to terminate the program. As mentioned above, when you move the mouse over the icon, a custom message will be sent to the window specified in the hwnd member. The message is the wm_myessage specified by the ucallbackmessage member, the value is wm_user + 101 (I will not talk about how to customize the message ). To implement the task, we need to customize the message in the hwnd window:
Void cmainframe: onmymessage (wparam, lparam) { Uint uid; // the ID of the icon that sent the message Uint umousemsg; // Mouse Action Point pt; Uid = (uint) wparam; Umousemsg = (uint) lparam; If (umousemsg = wm_rbuttondown) // right-click { Switch (UID) { Case idr_mainframe: // if it is our icon Getcursorpos (& pt); // get the cursor position Afxgetapp ()-> m_pmainwnd-> showwindow (sw_shownormal); // display the program window Break; Default: } } Return; } |
4. Delete the program icon when the program ends
When the program ends, you need to delete the icon in the announcement area. At this time, you should call the shell_policyicon function, except that the first parameter is nim_delete:
Void cmainframe ::~ Cmainframe () { Notifyicondata tnid; Tnid. cbsize = sizeof (policyicondata ); Tnid. hwnd = This-> m_hwnd; Tnid. uid = idr_mainframe; // ensure that our icon is deleted. Shell_policyicon (nim_delete, & tnid ); } |