C ++ Dialog Box System Tray implementation

Source: Internet
Author: User

Background requirements:

1. When the MFC dialog box is minimized, only the tray icon in the lower right corner of the system is displayed.

2. Double-click the tray and restore it in the dialog box.

3. Right-click the tray and the "close" menu will pop up to close the dialog box.

Steps:

1. Create an MFC dialog box Program (in this example, the program name is TaskbarIcon) and set "MinimizeBox" to true in the property.

2. Define a custom message macro.

[Cpp]
# Define policy_show WM_USER + 2500

3. The OnSysCommand (system message response function) Addition Dialog Box minimizes code execution.
[Cpp] view plaincopy
Void CTaskbarIconDlg: OnSysCommand (UINT nID, LPARAM lParam)
{
If (SC _MINIMIZE = nID)
{
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 = policy_show; // custom message name
Nid. hIcon = LoadIcon (AfxGetInstanceHandle (), MAKEINTRESOURCE (IDR_MAINFRAME ));
Wcscpy_s (nid. szTip, _ T ("icon test"); // message tip
 
Shell_policyicon (NIM_ADD, & nid); // Add an icon in the tray Area
ShowWindow (SW_HIDE); // hide the Main Window
Return;
}
 
//... Built-in system code
}

4. Custom message response for pallets

[Cpp]
BEGIN_MESSAGE_MAP (CTaskbarIconDlg, CDialog)
//... Omitting other message ing
 
ON_MESSAGE (policy_show, OnTrayIcon)
//} AFX_MSG_MAP
END_MESSAGE_MAP ()
 
Afx_msg LRESULT OnTrayIcon (WPARAM wParam, LPARAM lParam );
 
LRESULT CTaskbarIconDlg: OnTrayIcon (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"
 
// Declare a pop-up menu
// Add the menu item "close" and click it to send the message WM_CLOSE to the main window (already
// Hide) to end the program.
CMenu menu;
Menu. CreatePopupMenu ();
Menu. AppendMenu (MF_STRING, ID_APP_EXIT, _ T ("close "));
 
// Get the mouse position
LPPOINT lpoint = new tagPOINT;
: GetCursorPos (lpoint );
 
// Determine the position of the pop-up menu
Menu. TrackPopupMenu (TPM_LEFTALIGN, lpoint-> x, lpoint-> y, this );
 
// Reclaim Resources
HMENU hmenu = menu. Detach ();
Menu. DestroyMenu ();
 
Delete lpoint;
Lpoint = NULL;
 
Break;
}
Case WM_LBUTTONDBLCLK:
{
// Double-click the left button.
 
// Display page www.2cto.com
ShowWindow (SW_SHOW );
// Top the interface
SetForegroundWindow ();
 
Break;
}
}
 
Return 0;
}

5. Delete the tray icon when you close it.

Override OnCancel function.

[Cpp]
Virtual void OnCancel ();
 
Void CTaskbarIconDlg: OnCancel ()
{
// Delete the taskbar icon
NOTIFYICONDATA nid;
Nid. hWnd = this-> m_hWnd;
Nid. uID = IDR_MAINFRAME;
Shell_policyicon (NIM_DELETE, & nid );
 
CDialog: OnCancel ();
}

Author: segen_jaa

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.