STEP/Method
(1) Principles
1. Principle of minimization: First hide the window, and then draw the icon in the lower right corner.
2. Restoration principle: display the window and delete the images in the tray.
(2) Program Implementation
1. Custom message WM_SHOWTASK:
# Define WM_SHOWTASK (WM_USER + 1)
2.
: Added a command response in the OnSysCommand (UINT nID, LPARAM lParam) function body.
If (nID = SC _MINIMIZE)
ToTray (); // the function that minimizes data to the tray.
3. Add in message ing
ON_MESSAGE (WM_SHOWTASK, OnShowTask); // Where WM_SHOWTASK is the message name,
OnShowTask is a self-defined message response function, which is described later.
(3) specific function content
1. minimize to the tray FunctionCopy codeThe Code is as follows: void CMyDlg: 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, "program name"); // message tip
Shell_policyicon (NIM_ADD, & nid); // Add an icon in the tray Area
ShowWindow (SW_HIDE); // hide the Main Window
}
2. Restore interface functions
Define the message Response Function in the header fileCopy codeThe Code is as follows: afx_msg LRESULT OnShowTask (WPARAM wParam, LPARAM lParam );
// WParam receives the icon ID, while lParam receives the mouse action.
LRESULT CMyDlg: OnShowTask (WPARAM wParam, LPARAM lParam)
{
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
Menu. AppendMenu (MF_STRING, WM_DESTROY, "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
HMENU hmenu = menu. Detach ();
Menu. DestroyMenu (); // reclaim Resources
Delete lpoint;
} Break;
Case WM_LBUTTONDBLCLK: // double-click the left button.
{
This-> ShowWindow (SW_SHOW); // a simple display of the main window is complete.
DeleteTray ();
} Break;
Default: break;
}
Return 0;
}
Delete Tray Icon FunctionCopy codeThe Code is as follows: void CMyDlg: DeleteTray ()
{
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, "program name"); // The message is scheduled task reminder"
Shell_policyicon (NIM_DELETE, & nid); // Delete the icon in the tray Area
}