Dynamically change the method of the menu item when using CXTTRAYICON

xiaoxiao2021-03-06  104

When using CXTTTRAYICON, the method of dynamically changing the menu item uses CXTTRAYICON in the program, and finds that the menu item cannot be dynamically changed, such as the disable / allow menu item, modify the text of the menu. After reading the source code of CXTTrayicon, found two solutions: 1. If you use the CXTTRAYICON variable directly in the dialog, add message processing function AFX_MSG LRESULT ONTRAYNOTIFICATION (WPARAM WPARAM, LPARAM LPARAM);

BEGIN_MESSAGE_MAP (CTrayIconTestDlg, CDialog) // {{AFX_MSG_MAP (CTrayIconTestDlg) //}} AFX_MSG_MAP ON_MESSAGE (TIN_XT_TRAYICON, OnTrayNotification) END_MESSAGE_MAP () LRESULT CTrayIconTestDlg :: OnTrayNotification (WPARAM wParam, LPARAM lParam) {switch (LOWORD (lParam)) {case WM_RBUTTONUP: {cmenu Menu; if (! Menu.loadmenu (idR_Menu1)) {return 0;} cmenu * psubmenu = menu.getsubmenu (0); if (psubmenu == null) {Return 0;} // Display the menu At . the current mouse location There's a "bug" // (Microsoft calls it a feature) in Windows 95 that requires calling // SetForegroundWindow To find out more, search for Q135788 in MSDN // CPoint pos;.. GetCursorPos (& pos); menu.EnableMenuItem (ID_MENUITEM_MENU1, MF_DISABLED | MF_GRAYED); menu.CheckMenuItem (ID_MENUITEM_MENU2, MF_CHECKED); :: TrackPopupMenu (pSubMenu-> m_hMenu, 0, pos.x, pos.y, 0, this-> m_hWnd, NULL); menu . Detach ();} Break;} Return True;} Note: Be sure to return to True to avoid CXTTTRAYICON processing; use menu.enableMenuItem ID_MENUITEM_MENU1, MF_DISABLED | when MF_GRAYED) prohibits the menu item, must pay attention to add MF_GRAYED2 If the new class CTrayIconEx inherited from CXTTrayIcon, then you can override the message processing function CXTTrayIcon in: virtual afx_msg LRESULT OnTrayNotification (WPARAM wParam, LPARAM lParam);. Other processing same

转载请注明原文地址:https://www.9cbs.com/read-87157.html

New Post(0)