The implementation and tray menu of the tray icon under MFC, And the mfc Tray Icon menu

Source: Internet
Author: User

The implementation and tray menu of the tray icon under MFC, And the mfc Tray Icon menu

Dialog Box header file XXXDlg. h:

1. Add the member variable policyicondata m_nid;

2. Add a tray message response function declaration

Afx_msg LRESULT OnTrayNotify (WPARAM wParam, LPARAM lParam );

The dialog box implements the file XXXDlg. cpp:

1. define tray message # define UM_TRAYNOTIFY WM_USER + 11

2. Add the constructor of CXXXDlg

Memset (& m_nid, 0, sizeof (m_nid); // Initialize policyicondata struct m_nid.cbSize = sizeof (m_nid); m_nid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;

3. Add the destructor of CXXXDlg

M_nid.hIcon = NULL; shell_policyicon (NIM_DELETE, & m_nid );

4. Add message ing:

BEGIN_MESSAGE_MAP (CMFC2Dlg, CDialog) //... ON_MESSAGE (UM_TRAYNOTIFY, & CMFC2Dlg: OnTrayNotify) //... END_MESSAGE_MAP ()

5. Add the OnInitDialog function:

M_nid.hWnd = GetSafeHwnd (); m_nid.uCallbackMessage = um_traypolicy;

// Set tray icon and tooltip m_nid.hIcon = m_hIcon;

// Set tray notification tip information CString strToolTip = _ T ("tray program"); _ tcsncpy_s (m_nid.szTip, strToolTip, strToolTip. getLength (); shell_policyicon (NIM_ADD, & m_nid );

6. Implementation of the OnTrayNotify function: LRESULT CXXXDlg: OnTrayNotify (WPARAM wParam, LPARAM lParam) {UINT uMsg = (UINT) lParam;

Switch (uMsg) {case WM_RBUTTONUP: {// right-click CMenu menuTray; CPoint point; int id; GetCursorPos (& point); menuTray. loadMenu (IDR_MENU_TRAY); id = menuTray. getSubMenu (0)-> TrackPopupMenu (TPM_RETURNCMD | TPM_LEFTALIGN | TPM_RIGHTBUTTON, point. x, point. y, this); # if 0 CString strInfo; strInfo. format (L "menuid % d", id); LPCTSTR strtmp; strtmp = strInfo. getBuffer (0); MessageBox (strtmp, L "test"); # endif switch (id) {case IDR_TRAY_EXIT: OnOK (); break; case IDR_TRAY_RESTORE:

// Display SetForegroundWindow (); ShowWindow (SW_SHOWNORMAL); break; default: break;} case WM_LBUTTONDBLCLK: SetForegroundWindow (); ShowWindow (sw_normal show); break; default: break;} return 0 ;}

7. Add WM_SIZE message processing:

Void CMFC2Dlg: OnSize (UINT nType, int cx, int cy) {CDialog: OnSize (nType, cx, cy );

If (nType = SIZE_MINIMIZED) {ShowWindow (SW_HIDE) ;}} 8. menu.

Add menu resources, such as IDR_MENU_TRAY.

Define a sub-menu Tray with several menu items, such as "Restore window". The ID is IDR_TRAY_RESTORE, "exit", and the ID is IDR_TRAY_EXIT.

Capture and right-click the message in the ontraypolicy function. For the pop-up menu, see step 1.

 

Now, the function of adding a tray is basically complete.

Related Article

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.