VC Programming System Tray Program-taskbar

Source: Internet
Author: User

There are some errors here and you need to correct them yourself.
1. You should not initialize the precreatewindow. Use showwindow () to hide it ()
2. policyicondata TND; is defined as a global variable. Otherwise, the Destructor will be invalid.
3. Change sw_shownormal to sw_show.
..
================================
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 the design methods for developing such programs in Visual C ++.

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 );
}

 

 

 

 

========================================

 

 

Taskbar-hide the main interface. Date:

Share/Add to favorites. h file:

Notifyicondata NID; // the taskbar tray.
Afx_msg lresult onshowtask (wparam, lparam );//

================
. Cpp file:

Bool crememberdlg: oninitdialog ()
{
Totray (); // Add it to the taskbar tray.
Setwindowpos (& wndnotopmost, swp_hidewindow); // set the display mode to the top, and do not display it.
Modifystyleex (ws_ex_appwindow, ws_ex_toolwindow); // The settings are not displayed in the taskbar.
This-> setwindowtext (L ""); // The task name in the task manager is not displayed.
}

Void crememberdlg: totray (void)
{
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 ));
Wcscpy (NID. sztip, l "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

}

Lresult crememberdlg: 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, l "quit ");
// 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_shownormal); // a simple display of the main window is complete.
// This-> setwindowpos (& wndtopmost, 0, 0, 0, swp_nomove | swp_nosize );
}
Break;
}
Return 0;
}

Void crememberdlg: ondestroy ()
{
Cdialog: ondestroy ();

// Todo: add the message processing program code here
Shell_policyicon (nim_delete, & nid); // when it is disabled, delete the tray

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.