BOOL COXShellNamespaceNavigator::InvokeDefaultCommand(const LPSHELLFOLDER lpParentFolder, const LPITEMIDLIST lpRelativeIDL) const { // retrieve the default command ID HMENU hMenu=GetObjectContextMenu(lpParentFolder,lpRelativeIDL,CMF_DEFAULTONLY); if(hMenu==NULL) { TRACE(_T("COXShellNamespaceNavigator::InvokeDefaultCommand: GetObjectContextMenu() failed\n")); return FALSE; } CMenu menuPopup; VERIFY(menuPopup.Attach(hMenu)); if(menuPopup.GetMenuItemCount()==0) { TRACE(_T("COXShellNamespaceNavigator::InvokeDefaultCommand: there is no context menu for the specified object\n")); return FALSE; } int nDefaultCmdID=-1; #if _MFC_VER > 0x0421 nDefaultCmdID=menuPopup.GetDefaultItem(GMDI_GOINTOPOPUPS,FALSE); #else nDefaultCmdID=::GetMenuDefaultItem(menuPopup.GetSafeHmenu(), FALSE,GMDI_GOINTOPOPUPS); #endif if(nDefaultCmdID==-1) { TRACE(_T("COXShellNamespaceNavigator::InvokeDefaultCommand: there is no default menu item for the specified object\n")); return FALSE; } VERIFY(menuPopup.DestroyMenu()); return InvokeCommand(lpParentFolder,lpRelativeIDL,nDefaultCmdID,CMF_DEFAULTONLY); }
// CMainFrame::OnChangeFileMenu() is a menu command handler for // CMainFrame class, which in turn is a CFrameWnd-derived class. // It modifies the File menu by inserting, removing and renaming // some menu items. Other operations include associating a context // help id and setting default menu item to the File menu. // CMainFrame is a CFrameWnd-derived class. void CMainFrame::OnChangeFileMenu() { // Get the menu from the application window. CMenu* mmenu = GetMenu(); // Look for "File" menu. int pos = FindMenuItem(mmenu, _T("&File")); if (pos == -1) return; // Remove "New" menu item from the File menu. CMenu* submenu = mmenu->GetSubMenu(pos); pos = FindMenuItem(submenu, _T("&New\tCtrl+N")); if (pos > -1) submenu->RemoveMenu(pos, MF_BYPOSITION); // Look for "Open" menu item from the File menu. Insert a new // menu item called "Close" right after the "Open" menu item. // ID_CLOSEFILE is the command id for the "Close" menu item. pos = FindMenuItem(submenu, _T("&Open...\tCtrl+O")); if (pos > -1) submenu->InsertMenu(pos + 1, MF_BYPOSITION, ID_CLOSEFILE, _T("&Close")); // Rename menu item "Exit" to "Exit Application". pos = FindMenuItem(submenu, _T("E&xit")); if (pos > -1) { UINT id = submenu->GetMenuItemID(pos); submenu->ModifyMenu(id, MF_BYCOMMAND, id, _T("E&xit Application")); } // Associate a context help ID with File menu, if one is not found. // ID_FILE_CONTEXT_HELPID is the context help ID for the File menu // that is defined in resource file. if (submenu->GetMenuContextHelpId() == 0) submenu->SetMenuContextHelpId(ID_FILE_CONTEXT_HELPID); // Set "Open" menu item as the default menu item for the File menu, // if one is not found. So, when a user double-clicks the File // menu, the system sends a command message to the menu's owner // window and closes the menu as if the File\Open command item had // been chosen. if (submenu->GetDefaultItem(GMDI_GOINTOPOPUPS, TRUE) == -1) { pos = FindMenuItem(submenu, _T("&Open...\tCtrl+O")); submenu->SetDefaultItem(pos, TRUE); } }
LRESULT CTrayNotifyIcon::OnTrayNotification(WPARAM wID, LPARAM lEvent) { //Return quickly if its not for this tray icon if (wID != m_NotifyIconData.uID) return 0L; #ifdef _AFX CMenu* pSubMenu = m_Menu.GetSubMenu(0); ATLASSERT(pSubMenu); //Your menu resource has been designed incorrectly #else CMenuHandle subMenu = m_Menu.GetSubMenu(0); ATLASSERT(subMenu.IsMenu()); #endif //if (lEvent == WM_RBUTTONUP) //TODO on both events? if (lEvent == WM_RBUTTONUP || lEvent == WM_LBUTTONDBLCLK)//TODO on both events for version without UI and open { CPoint ptCursor; GetCursorPos(&ptCursor); ::SetForegroundWindow(m_NotifyIconData.hWnd); #ifdef _AFX ::TrackPopupMenu(pSubMenu->m_hMenu, TPM_LEFTBUTTON, ptCursor.x, ptCursor.y, 0, m_NotifyIconData.hWnd, NULL); #else ::TrackPopupMenu(subMenu, TPM_LEFTBUTTON, ptCursor.x, ptCursor.y, 0, m_NotifyIconData.hWnd, NULL); #endif ::PostMessage(m_NotifyIconData.hWnd, WM_NULL, 0, 0); } else if (lEvent == WM_LBUTTONDBLCLK) //double click received, the default action is to execute first menu item { ::SetForegroundWindow(m_NotifyIconData.hWnd); #ifdef _AFX UINT nDefaultItem = pSubMenu->GetDefaultItem(GMDI_GOINTOPOPUPS, FALSE); #else UINT nDefaultItem = subMenu.GetMenuDefaultItem(FALSE, GMDI_GOINTOPOPUPS); #endif if (nDefaultItem != -1) ::SendMessage(m_NotifyIconData.hWnd, WM_COMMAND, nDefaultItem, 0); } return 1; // handled }