System Environment: Windows 7
Software environment: Visual Studio 2008 SP1
Objective: To achieve the effect of system tray and bubble prompts
We should have seen the effects of system pallets. For example, QQ, anti-virus software, and other products all reside in the taskbar icon. This is very useful for some software, easy to operate software, and can hide software. As shown in:
To implement the system tray, you must understand the notifyicondata struct, as shown below:
/*** Message of the taskbar status area to be processed by the system ***/
Typedef struct _ policyicondata {
DWORD cbsize; // the size of the struct, in bytes
Hwnd; // window handle
Uint uid; // Application Program Identifier of the defined taskbar icon
Uint uflags; // indicates which other Members are valid data.
Uint ucallbackmessage; // Message ID defined by the application
Hicon; // the handle of the added, modified, or deleted icon.
Tchar sztip [64]; // pointer to a string ending with \ 0
DWORD dwstate; // version 5.0, icon status
DWORD dwstatemask; // version 5.0. Specify the bits of the dwstate member that can be set or accessed.
Tchar szinfo [256]; // pointer to a string ending with \ 0. The content of the string is the content of the balloon prompt.
Union {
Uint utimeout; // indicates the timeout time of the balloon prompt, in milliseconds. After this time, the balloon prompt will disappear.
Uint uversion; // used to set the icon message interface in the Windows 95 or Windows 2000 Style
};
Tchar szinfotitle [64]; // pointer to a string ended with \ 0. The content of the string is the title of the balloon prompt
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.
Guid guiditem; // Reserved
Hicon hballoonicon; // a custom balloon icon for Windows Vista or later
} Notifyicondata, * ppolicyicondata;
/*** Message of the taskbar status area to be processed by the system ***/
Typedef struct _ policyicondata {
DWORD cbsize; // the size of the struct, in bytes
Hwnd; // window handle
Uint uid; // The identifier of the taskbar icon defined by the application
Uint uflags; // indicates which other Members are valid data.
Uint ucallbackmessage; // Message ID defined by the application
Hicon; // the handle of the added, modified, or deleted icon.
Tchar sztip [64]; // pointer to a string ending with \ 0
DWORD dwstate; // version 5.0, icon status
DWORD dwstatemask; // version 5.0. Specify the bits of the dwstate member that can be set or accessed.
Tchar szinfo [256]; // pointer to a string ending with \ 0. The content of the string is the content of the balloon prompt.
Union {
Uint utimeout; // indicates the timeout time of the balloon prompt, in milliseconds. After this time, the balloon prompt will disappear.
Uint uversion; // used to set the icon message interface in the Windows 95 or Windows 2000 Style
};
Tchar szinfotitle [64]; // pointer to a string ended with \ 0. The content of the string is the title of the balloon prompt
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.
Guid guiditem; // Reserved
Hicon hballoonicon; // a custom balloon icon for Windows Vista or later
} Notifyicondata, * ppolicyicondata;
Implementation is started as follows:
Step 1: Create a dialog box-based project with any name;
Step 2: Add two variables in the header file of the dialog box, one policyicondata variable NID and one bool variable isnotify. The logical variable is used to determine whether the tray has appeared. In the dialog box, initialize the oninitdialog function to add
Isnotify = false;
Then add # define wm_showtask (wm_user + n) to the header file. Note that N> 100 or more is required. Then add a declaration message in the message ing function:
Afx_msg lresult onshowtask (wparam, lparam );
Step 3: Add begin_message_map and end_message_map to the CPP file in the dialog box.
On_message (wm_showtask, onshowtask)
Step 4: implement the onshowtask () function, that is, the tray menu
View plaincopy to clipboardprint?
Lresult ccd_rom_opendlg: onshowtask (wparam, lparam) // wparam receives the icon ID, while lparam receives the mouse behavior.
{
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 "close"
{
Lppoint lpoint = new tagpoint;
: Getcursorpos (lpoint); // get the cursor position
Cmenu menu;
Menu. createpopupmenu (); // declare a pop-up menu
Menu. appendmenu (mf_string, wm_destroy, _ T ("close"); // Add the menu item "close". Click it to send the message wm_destroy to the main window (hidden) and end the program.
Menu. trackpopupmenu (tpm_leftalign, lpoint-> X, lpoint-> Y, this); // determine the position of the pop-up menu
Setforegroundwindow ();
Hmenu = menu. Detach (); // reclaim Resources
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;
}
Lresult ccd_rom_opendlg: onshowtask (wparam, lparam) // wparam receives the icon ID, while lparam receives the mouse behavior.
{
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 "close"
{
Lppoint lpoint = new tagpoint;
: Getcursorpos (lpoint); // get the cursor position
Cmenu menu;
Menu. createpopupmenu (); // declare a pop-up menu
Menu. appendmenu (mf_string, wm_destroy, _ T ("close"); // Add the menu item "close". Click it to send the message wm_destroy to the main window (hidden) and end the program.
Menu. trackpopupmenu (tpm_leftalign, lpoint-> X, lpoint-> Y, this); // determine the position of the pop-up menu
Setforegroundwindow ();
Hmenu = menu. Detach (); // reclaim Resources
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;
}
Step 5: Add a function to implement the tray
View plaincopy to clipboardprint?
Void ccd_rom_opendlg: onbnclickedbutton1 ()
{
// Todo: add the control notification handler here Code
If (! Isnotify)
{
Nid. cbsize = (DWORD) sizeof (policyicondata );
Nid. hwnd = This-> m_hwnd;
Nid. uid = idr_mainframe;
Nid. uflags = nif_icon | nif_message | nif_tip | nif_info;
Nid. ucallbackmessage = wm_showtask; // custom message name. Note: The message here is a custom message.
Nid. hicon = loadicon (AfxGetInstanceHandle (), makeintresource (idr_mainframe ));
Wcscpy_s (NID. sztip, _ T ("VC System Tray program"); // The message is scheduled task reminder"
Wcscpy_s (NID. szinfo, _ T ("title "));
Wcscpy_s (NID. szinfotitle, _ T ("content "));
Nid. dwinfoflags = niif_info;
Nid. utimeout = 5000;
Shell_policyicon (nim_add, & nid); // Add an icon in the tray Area
}
// Animatewindow (1000, aw_hide | aw_blend); // You can slowly disappear the window.
// Killtimer (0 );
Showwindow (sw_hide); // hide the Main Window
}
Void ccd_rom_opendlg: onbnclickedbutton1 ()
{
// Todo: add the control notification handler code here
If (! Isnotify)
{
Nid. cbsize = (DWORD) sizeof (policyicondata );
Nid. hwnd = This-> m_hwnd;
Nid. uid = idr_mainframe;
Nid. uflags = nif_icon | nif_message | nif_tip | nif_info;
Nid. ucallbackmessage = wm_showtask; // custom message name. Note: The message here is a custom message.
Nid. hicon = loadicon (AfxGetInstanceHandle (), makeintresource (idr_mainframe ));
Wcscpy_s (NID. sztip, _ T ("VC System Tray program"); // The message is scheduled task reminder"
Wcscpy_s (NID. szinfo, _ T ("title "));
Wcscpy_s (NID. szinfotitle, _ T ("content "));
Nid. dwinfoflags = niif_info;
Nid. utimeout = 5000;
Shell_policyicon (nim_add, & nid); // Add an icon in the tray Area
}
// Animatewindow (1000, aw_hide | aw_blend); // You can slowly disappear the window.
// Killtimer (0 );
Showwindow (sw_hide); // hide the Main Window
}
Step 6: Turn the close button of the dialog box into a hidden interface, that is, the button to close will not close the program, in the onsyscommand () function
View plaincopy to clipboardprint?
Void ccd_rom_opendlg: onsyscommand (uint NID, lparam)
{
If (NID & 0xfff0) = idm_aboutbox)
{
Caboutdlg dlgabout;
Dlgabout. domodal ();
}
Else if (NID & 0xfff0) = SC _close) // trigger the close button
{
// Onclose (); // do not close
Onbnclickedbutton1 (); // enable the tray
}
Else
{
Cdialog: onsyscommand (NID, lparam );
}
}
Void ccd_rom_opendlg: onsyscommand (uint NID, lparam)
{
If (NID & 0xfff0) = idm_aboutbox)
{
Caboutdlg dlgabout;
Dlgabout. domodal ();
}
Else if (NID & 0xfff0) = SC _close) // trigger the close button
{
// Onclose (); // do not close
Onbnclickedbutton1 (); // enable the tray
}
Else
{
Cdialog: onsyscommand (NID, lparam );
}
}
Step 7: destroy. When the program is closed, remember to destroy the tray icon and reload the ondestroy () function.
View plaincopy to clipboardprint?
Void ccd_rom_opendlg: ondestroy ()
{
Cdialog: ondestroy ();
Shell_policyicon (nim_delete, & nid );
// Todo: add the message processing program code here
}
Void ccd_rom_opendlg: ondestroy ()
{
Cdialog: ondestroy ();
Shell_policyicon (nim_delete, & nid );
// Todo: add the message processing program code here
}
Okay, it's done. The implementation result is as follows:
More information about policyicondata: http://baike.baidu.com/view/1873525.html