CMenu *GetTemplateMenu() { int iPos; CMenu* pViewMenu = NULL; CMenu* pTopMenu = AfxGetMainWnd()->GetMenu(); for (iPos = pTopMenu->GetMenuItemCount()-1; iPos >= 0; iPos--) { CMenu* pMenu = pTopMenu->GetSubMenu(iPos); if (pMenu && pMenu->GetMenuItemID(0) == ID_VIEW_TOOLBAR) { pViewMenu = pMenu; break; } } ASSERT(pViewMenu != NULL); CMenu* pfMenu = NULL; for (iPos = pViewMenu->GetMenuItemCount()-1; iPos >= 0; iPos--) { pfMenu = pViewMenu->GetSubMenu(iPos); if(pfMenu) break; } ASSERT(pfMenu != NULL); return (pfMenu); }
LRESULT CTrayIcon::OnDefaultTrayNotification(WPARAM wID, LPARAM lEvent) { if(wID!=m_nid.uID || (lEvent!=WM_RBUTTONUP && lEvent!=WM_LBUTTONDBLCLK)) return 0; CMenu menu; if(!menu.LoadMenu(m_nid.uID)) return 0; CMenu* pSubMenu = menu.GetSubMenu(0); if (!pSubMenu) return 0; if(lEvent == WM_RBUTTONUP) { CPoint mouse; ::GetCursorPos(&mouse); ::TrackPopupMenu(pSubMenu->m_hMenu, 0, mouse.x, mouse.y, 0, m_nid.hWnd, nullptr); } else ::SendMessage(m_nid.hWnd, WM_COMMAND, pSubMenu->GetMenuItemID(0), 0); return 1; }
LRESULT CTrayNotifyIcon::OnTrayNotification(WPARAM wID, LPARAM lEvent) { //Return quickly if its not for this tray icon if (wID != m_NotifyIconData.uID) return 0L; CMenu* pSubMenu = m_Menu.GetSubMenu(0); ASSERT(pSubMenu); //Your menu resource has been designed incorrectly if (lEvent == WM_RBUTTONUP) //Clicking with right button brings up a context menu { CPoint pos; GetCursorPos(&pos); ::SetForegroundWindow(m_NotifyIconData.hWnd); ::TrackPopupMenu(pSubMenu->m_hMenu, 0, pos.x, pos.y, 0, m_NotifyIconData.hWnd, NULL); ::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); ::SendMessage(m_NotifyIconData.hWnd, WM_COMMAND, pSubMenu->GetMenuItemID(0), 0); } return 1; // handled }
void CMenuSpawn::RemapMenu(HMENU hMenu) { static int iRecurse = 0; iRecurse ++; CMenu pMenu; pMenu.Attach(hMenu); ASSERT(pMenu); int nItem = pMenu.GetMenuItemCount(); while ((--nItem)>=0) { UINT itemId = pMenu.GetMenuItemID(nItem); if (itemId == (UINT) -1) { CMenu pops(pMenu.GetSubMenu(nItem)); if (pops.GetHandleMenu()) RemapMenu(pops.GetHandleMenu()); if (bIsPopup || iRecurse > 0) { TCHAR cs[128]; memset(cs,0x00,128); pMenu.GetMenuString(nItem, cs, 128,MF_BYPOSITION); if (lstrlen(cs)>0) { SpawnItem * sp = AddSpawnItem(cs, (!bIsPopup && iRecurse == 1) ? -4 : -2); pMenu.ModifyMenu(nItem,MF_BYPOSITION|MF_OWNERDRAW, (UINT) -1, (LPCTSTR)sp); } } } else { if (itemId != 0) { UINT oldState = pMenu.GetMenuState(nItem,MF_BYPOSITION); if (!(oldState&MF_OWNERDRAW) && !(oldState&MF_BITMAP)) { ASSERT(oldState != (UINT)-1); TCHAR cs[128]; memset(cs,0x00,128); pMenu.GetMenuString(nItem, cs, 128, MF_BYPOSITION); SpawnItem * sp = AddSpawnItem(cs, itemId); if (itemId!=SC_CLOSE) pMenu.ModifyMenu(nItem,MF_BYPOSITION|MF_OWNERDRAW|oldState, (LPARAM)itemId, (LPCTSTR)sp); } } else { UINT oldState = pMenu.GetMenuState(nItem,MF_BYPOSITION); if (!(oldState&MF_OWNERDRAW) && !(oldState&MF_BITMAP)) { ASSERT(oldState != (UINT)-1); SpawnItem * sp = AddSpawnItem("--", -3); pMenu.ModifyMenu(nItem,MF_BYPOSITION|MF_OWNERDRAW|oldState, (LPARAM)itemId, (LPCTSTR)sp); } } } } iRecurse --; pMenu.Detach(); }
void CChildFrame::OnMDIActivate(BOOL bActivate, CWnd* pActivateWnd, CWnd* pDeactivateWnd) { CMDIChildWnd::OnMDIActivate(bActivate, pActivateWnd, pDeactivateWnd); if (bActivate) { CMenu* pTopMenu = AfxGetMainWnd()->GetMenu(); for(unsigned int i = 0; i < pTopMenu->GetMenuItemCount(); i++) { CMenu *pSubMenu = pTopMenu->GetSubMenu(i); if (pSubMenu) for(unsigned int j=0; j < pSubMenu->GetMenuItemCount(); j++) { if (pSubMenu->GetMenuItemID(j) == ID_PORTS_CREATE) { CMenu ResourceMenu; ResourceMenu.CreatePopupMenu(); for(int k=0;k < Port::portCount() ; k++) ResourceMenu.AppendMenu(MF_STRING,ID_RESOURCE_PORT_BEGIN + k, Port::portName( k ) ); pSubMenu->DeleteMenu(j,MF_BYPOSITION); pSubMenu->InsertMenu(j,MF_POPUP|MF_BYPOSITION,(unsigned int)ResourceMenu.Detach(),_T("Create")); return; } } } } }
void CEsmRefCellPage::OnContextMenu(CWnd* pWnd, CPoint Point) { CMenu Menu; CMenu* pPopup; BOOL Result; CCmdUI MenuState; int Index; /* Get the popup menu to display */ Result = Menu.LoadMenu(IDR_CELLREF_MENU); if (!Result) return; pPopup = Menu.GetSubMenu(0); if (pPopup == NULL) return; /* Force the update of the menu commands */ for (Index = 0; Index < (int) pPopup->GetMenuItemCount(); Index++) { MenuState.m_nID = pPopup->GetMenuItemID(Index); MenuState.m_nIndex = Index; MenuState.m_pMenu = pPopup; MenuState.m_pOther = NULL; MenuState.m_pSubMenu = NULL; MenuState.m_nIndexMax = pPopup->GetMenuItemCount(); if (MenuState.m_nID != 0) { OnCmdMsg(MenuState.m_nID, CN_UPDATE_COMMAND_UI, &MenuState, NULL); } } if (pWnd->GetDlgCtrlID() == IDC_OBJECTLIST) { pPopup->TrackPopupMenu(TPM_RIGHTBUTTON | TPM_LEFTALIGN, Point.x, Point.y, this); } }
// 参看:http://www.cnblogs.com/hcfalan/archive/2010/11/24/1886139.html BOOL CDlgImageViewer::ContinueModal() { if(m_ToolBar.IsWindowVisible()) { CFrameWnd* pParent = (CFrameWnd*)m_ToolBar.GetParent(); if(pParent) m_ToolBar.OnUpdateCmdUI(pParent, (WPARAM)TRUE); } CMenu* pMainMenu = GetMenu(); CCmdUI cmdUI; if (pMainMenu != NULL) { for (int n = 0; n < pMainMenu->GetMenuItemCount(); ++n) { CMenu* pSubMenu = pMainMenu->GetSubMenu(n); cmdUI.m_nIndexMax = pSubMenu->GetMenuItemCount(); for (int i = 0; i < cmdUI.m_nIndexMax; ++i) { cmdUI.m_nIndex = i; cmdUI.m_nID = pSubMenu->GetMenuItemID(i); cmdUI.m_pMenu = pSubMenu; cmdUI.DoUpdate(this, FALSE); } } } return CDialogEx::ContinueModal(); }
//Стандартный вызов ON_UPDATE_COMMAND_UI запрещает элементы меню, но не делает их grayed. //Чтобы все таки запрещенные элементы меню выглядели как grayed, нужно вызывать эту функцию //Note: эта функция не перерисовывает меню. //Возвращает true - если произошли изменения и нужно перерисовать меню, иначе false bool CMainFrame::_UpdateTopLevelMainMenu(void) { CMenu* pMenu = this->GetMenu(); std::vector<UINT> old_menu_state; //remember old states of all menu items { int menu_items_count = pMenu->GetMenuItemCount(); for(int i = 0; i < menu_items_count; ++i) old_menu_state.push_back(pMenu->GetMenuState(pMenu->GetMenuItemID(i),MF_BYCOMMAND)); } //Perform update CCmdUI ui; ui.m_nIndexMax = pMenu->GetMenuItemCount(); ui.m_pMenu = pMenu; for (ui.m_nIndex = 0; ui.m_nIndex < ui.m_nIndexMax; ui.m_nIndex++) { ui.m_nID = pMenu->GetMenuItemID(ui.m_nIndex); ui.DoUpdate(this, m_bAutoMenuEnable); } //Check: do we need to redraw menu? { int menu_items_count = pMenu->GetMenuItemCount(); for(int i = 0; i < menu_items_count; ++i) { UINT old_s = old_menu_state[i]; UINT new_s = pMenu->GetMenuState(pMenu->GetMenuItemID(i),MF_BYCOMMAND); if (old_s!=new_s) return true; //caller should redraw main menu } } //nothing changed: redraw is not needed return false; }
// 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); } }
void CMainFrame::OnClose() { if(m_pApp->m_iCurrentTasks>0) { AfxMessageBox("You have " + str(m_pApp->m_iCurrentTasks)+ " job(s) pending completion. Please wait for this to finish before closing."); } else { //disable close system menu. CMenu* pmenu = GetSystemMenu(FALSE); UINT size = pmenu->GetMenuItemCount( ); pmenu->EnableMenuItem(pmenu->GetMenuItemID(6), MF_DISABLED | MF_GRAYED); DrawMenuBar(); //instead of the 2 phase shutdown, i'm //just gonna risk leaking a little memory //and simplify the code. markme saw a crash here. m_pApp->Terminate(); //close the frame. CMDIFrameWnd::OnClose(); //ALERT. Unusual code. //This is a 2phase shutdown to ensure we don't leak any memory due to floating msgs coming //in from the server. /*if(m_ShutDownPhase==0) { if(E_FAIL==m_pApp->Terminate()) { //Terminate's already happened. Shutdown now. m_ShutDownPhase = 0; CMDIFrameWnd::OnClose(); return; } m_ShutDownPhase=-23; //number same as below. number not used anywhere else. PostMessage(WM_CLOSE); } else if(m_ShutDownPhase==-23) //see number above. { m_ShutDownPhase = 0; CMDIFrameWnd::OnClose(); } else ASSERT(0); */ } }
//************************************************************************************** void CBCGPTearOffManager::SetupTearOffMenus (HMENU hMenu) { ASSERT (hMenu != NULL); CMenu* pMenu = CMenu::FromHandle (hMenu); if (pMenu == NULL) { return; } int iCount = (int) pMenu->GetMenuItemCount (); for (int i = 0; i < iCount; i ++) { UINT uiID = pMenu->GetMenuItemID (i); if (uiID != (UINT) -1) { continue; } UINT uiState = pMenu->GetMenuState (i, MF_BYPOSITION); if (uiState & MF_MENUBARBREAK) { CString str; pMenu->GetMenuString (i, str, MF_BYPOSITION); if (str [0] != cIDChar) { UINT uiCtrlBarId = GetFreeTearOffID (); if (uiCtrlBarId == 0) // No more free IDs! { // Reserve more IDs in Initialize!!! ASSERT (FALSE); return; } Build (uiCtrlBarId, str); pMenu->ModifyMenu (i, MF_BYPOSITION, i, str); } } CMenu* pPopupMenu = pMenu->GetSubMenu (i); if (pPopupMenu != NULL) { SetupTearOffMenus (pPopupMenu->GetSafeHmenu ()); } } }
// // Find a statistic by its name (as stored in result file) and select it // as though it had been selected from the menu. // void CPageDisplay::SelectStatisticByName( int button, const char *statistic_name ) { unsigned int submenu_id, menu_item; if ( (button < 0) || (button >= NUM_STATUS_BARS) ) { ErrorMessage("Invalid button value in CPageDisplay::SelectStatisticByName()"); return; } if ( !statistic_name ) { ErrorMessage("Invalid string in CPageDisplay::SelectStatisticByName()"); return; } CMenu menu; VERIFY(menu.LoadMenu(IDR_POPUP_DISPLAY_LIST)); CMenu* pPopup = menu.GetSubMenu(0); ASSERT(pPopup != NULL); // we can use ASSERT here because this should work every time if it works at all for ( submenu_id = 0; submenu_id < MDisplayNumSubmenus; submenu_id++ ) { CMenu* pSubPopup = pPopup->GetSubMenu( submenu_id ); if ( pSubPopup == NULL ) { ErrorMessage("Could not get submenu in CPageDisplay::SelectStatisticByName()"); return; } for ( menu_item = 0; menu_item < pSubPopup->GetMenuItemCount(); menu_item++ ) { CString title; pSubPopup->GetMenuString( menu_item, title, MF_BYPOSITION ); if ( title.CompareNoCase(statistic_name) == 0 ) { selected_button = button; theApp.pView->OnMDisplay( submenu_id, pSubPopup->GetMenuItemID( menu_item ) ); return; } } } // No match found, just leave the current selection unchanged return; }
//**************************************************************************************** HMENU CBCGPToolbarSystemMenuButton::CreateMenu () const { ASSERT (m_hSystemMenu != NULL); HMENU hMenu = CBCGPToolbarMenuButton::CreateMenu (); if (hMenu == NULL) { return NULL; } //--------------------------------------------------------------------- // System menu don't produce updating command statuses via the // standard MFC idle command targeting. So, we should enable/disable // system menu items according to the standard system menu status: //--------------------------------------------------------------------- CMenu* pMenu = CMenu::FromHandle (hMenu); ASSERT_VALID (pMenu); CMenu* pSysMenu = CMenu::FromHandle (m_hSystemMenu); ASSERT_VALID (pSysMenu); int iCount = (int) pSysMenu->GetMenuItemCount (); for (int i = 0; i < iCount; i ++) { UINT uiState = pSysMenu->GetMenuState (i, MF_BYPOSITION); UINT uiCmd = pSysMenu->GetMenuItemID (i); if (uiState & MF_CHECKED) { pMenu->CheckMenuItem (uiCmd, MF_CHECKED); } if (uiState & MF_DISABLED) { pMenu->EnableMenuItem (uiCmd, MF_DISABLED); } if (uiState & MF_GRAYED) { pMenu->EnableMenuItem (uiCmd, MF_GRAYED); } } return hMenu; }
void Languages::Translate(CMenu& menu) { int count = menu.GetMenuItemCount(); for(int i=0;i<count;i++) { CMenu* subMenu = menu.GetSubMenu(i); if(subMenu) Translate(*subMenu); } //set translated text for every know control ID for(int i=0;i<count;i++) { UINT id = menu.GetMenuItemID(i); CString szText; if(GetItemText(id, szText)) VERIFY(menu.ModifyMenu(i, MF_BYPOSITION | MF_STRING, id, szText)); } }
//************************************************************************************** void CBCGPTearOffManager::Reset (HMENU hMenu) { int nCount = m_uiTearOffMenuLast - m_uiTearOffMenuFirst + 1; if (hMenu == NULL) // Reset all { for (int i = 0; i < nCount; i ++) { m_arTearOffIDsUsage [i] = 0; } return; } CMenu* pMenu = CMenu::FromHandle (hMenu); if (pMenu == NULL) { ASSERT (FALSE); return; } int iCount = (int) pMenu->GetMenuItemCount (); for (int i = 0; i < iCount; i ++) { CString str; pMenu->GetMenuString (i, str, MF_BYPOSITION); UINT uiTearOffID = Parse (str); if (uiTearOffID >= m_uiTearOffMenuFirst && uiTearOffID <= m_uiTearOffMenuLast) { m_arTearOffIDsUsage [uiTearOffID - m_uiTearOffMenuFirst] = 0; } if (pMenu->GetMenuItemID (i) == (UINT)-1) { CMenu* pPopupMenu = pMenu->GetSubMenu (i); ASSERT (pPopupMenu != NULL); Reset (pPopupMenu->GetSafeHmenu ()); } } }
LRESULT CTrayNotifyIcon::OnTrayNotification(WPARAM wID, LPARAM lEvent) { //Return quickly if its not for this tray icon if (wID != m_NotifyIconData.uID) return 0L; //As a default action use a menu resource with the same id //as this was created with CMenu popmenu; if (!popmenu.LoadMenu(m_NotifyIconData.uID)) return 0; //添加图标菜单 CMenu * pSubMenu = popmenu.GetSubMenu(0); if (!pSubMenu) return 0; if(lEvent == WM_RBUTTONUP) { //鼠标右键按下时,弹出相关菜单 CMenu *pSubMenu = (CMenu *)m_menu.GetSubMenu(0); CPoint point; GetCursorPos(&point); ::SetMenuDefaultItem(pSubMenu->m_hMenu, 0, TRUE); ::SetForegroundWindow(m_NotifyIconData.hWnd); // Display the File popup menu as a floating popup menu in the // client area of the main application window. pSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, point.x, point.y, AfxGetApp()->m_pMainWnd); } else if (lEvent == WM_LBUTTONDBLCLK) { ::SendMessage(m_NotifyIconData.hWnd, WM_COMMAND, pSubMenu->GetMenuItemID(0), 0); } return 1; // handled }
// Search the "Select" menu to see if a CRC setting name is in use. // Returns the ID (index into settings_) if found or -1 if not int CGeneralCRC::find_name(LPCTSTR name) { int retval = -1; CMenu menu; menu.Attach(select_menu_.m_hMenu); int lst = menu.GetMenuItemCount(); for (int ii = 0; ii < lst; ++ii) { CString ss; menu.GetMenuString(ii, ss, MF_BYPOSITION); if (ss.CompareNoCase(name) == 0) { retval = menu.GetMenuItemID(ii); break; } } menu.Detach(); return retval; }
//////////////////////////////////////////////////////////// // Desc: // Handles all the messages that are delivered to the // shell icon in the system tray. //////////////////////////////////////////////////////////// LRESULT CTrayIcon::OnNotification( WPARAM uID, LPARAM lEvent ) { if( ( uID != m_notifydata.uID ) || ( ( lEvent != WM_RBUTTONUP ) && ( lEvent != WM_LBUTTONDBLCLK ) ) || ( ! m_menu.GetSafeHmenu() ) ) { return 0; } CPoint pt; CMenu* pPop = m_menu.GetSubMenu( 0 ); ::SetMenuDefaultItem( pPop->GetSafeHmenu(), 0, TRUE); if( lEvent == WM_RBUTTONUP ) { // if right click we need to display popup menu. ::GetCursorPos( &pt ); ::SetForegroundWindow( m_notifydata.hWnd ); ::TrackPopupMenuEx( pPop->GetSafeHmenu(), 0, pt.x, pt.y, m_notifydata.hWnd, NULL ); }else{ ::SendMessage( m_notifydata.hWnd, WM_COMMAND, pPop->GetMenuItemID( 0 ), 0 ); } return 1; }
LRESULT CTrayNotifyIcon::OnTrayNotification(WPARAM wID, LPARAM lEvent) { //Return quickly if its not for this tray icon if (wID != m_NotifyIconData.uID) return 0L; //As a default action use a menu resource with the same id //as this was created with CMenu menu; if (!menu.LoadMenu(m_NotifyIconData.uID)) return 0; CMenu* pSubMenu = menu.GetSubMenu(0); if (!pSubMenu) return 0; if (lEvent == WM_RBUTTONUP) { //Clicking with right button brings up a context menu // Make first menu item the default (bold font) ::SetMenuDefaultItem(pSubMenu->m_hMenu, 0, TRUE); //Display and track the popup menu CPoint pos; GetCursorPos(&pos); ::SetForegroundWindow(m_NotifyIconData.hWnd); ::TrackPopupMenu(pSubMenu->m_hMenu, 0, pos.x, pos.y, 0, m_NotifyIconData.hWnd, NULL); } else if (lEvent == WM_LBUTTONDBLCLK) { // double click received, the default action is to execute first menu item ::SendMessage(m_NotifyIconData.hWnd, WM_COMMAND, pSubMenu->GetMenuItemID(0), 0); } return 1; // handled }
///////////////// // Default event handler handles right-menu and doubleclick. // Call this function from your own notification handler. // LRESULT CTrayIcon::OnTrayNotification(WPARAM wID, LPARAM lEvent) { if (wID!=m_nid.uID || (lEvent!=WM_RBUTTONUP && lEvent!=WM_LBUTTONDBLCLK)) return 0; // If there's a resource menu with the same ID as the icon, use it as // the right-button popup menu. CTrayIcon will interprets the first // item in the menu as the default command for WM_LBUTTONDBLCLK // CMenu menu; if (!menu.LoadMenu(m_nid.uID)) return 0; CMenu* pSubMenu = menu.GetSubMenu(0); if (!pSubMenu) return 0; if (lEvent==WM_RBUTTONUP) { // Make first menu item the default (bold font) //::SetMenuDefaultItem(pSubMenu->m_hMenu, 0, TRUE); // 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 mouse; GetCursorPos(&mouse); ::SetForegroundWindow(m_nid.hWnd); ::TrackPopupMenu(pSubMenu->m_hMenu, 0, mouse.x, mouse.y, 0, m_nid.hWnd, NULL); } else // double click: execute first menu item ::SendMessage(m_nid.hWnd, WM_COMMAND, pSubMenu->GetMenuItemID(0), 0);//如果用PostMessage是不是更好? return 1; // handled }
void CCementBD::RenameMenuItem(UINT ID_menu_item, CString new_name) { //--------------------------------------------------- //Формируем меню внешних модулей CCementDoc* pDoc = (CCementDoc*)GetDocument(); pDoc->m_DefaultMenu = ::LoadMenu(AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_KRSCEMENTTYPE)); if (pDoc->m_DefaultMenu == NULL) return; CMenu* menu = CMenu::FromHandle(pDoc->GetDefaultMenu()); if(menu) { int iPos, iMaxPos, jPos, jMaxPos; CMenu *submenu = NULL; iMaxPos = (int)menu->GetMenuItemCount(); for(iPos = 0; iPos < iMaxPos; iPos++) { submenu = menu->GetSubMenu(iPos); jMaxPos = (int)submenu->GetMenuItemCount(); for(jPos = 0; jPos < jMaxPos; jPos++) { if(submenu->GetMenuItemID(jPos) == ID_menu_item) break; } if (jPos < jMaxPos) break; } if(submenu == NULL || jPos >= jMaxPos || new_name.GetLength() == 0) return; BOOL flg = submenu->ModifyMenu(jPos, MF_BYPOSITION, ID_menu_item, new_name); //BOOL flg = submenu->ModifyMenu(ID_menu_item, MF_BYCOMMAND, ID_menu_item, new_name); CMDIFrameWnd* frame = ((CMDIChildWnd *) GetParent())->GetMDIFrame(); frame->MDISetMenu(menu, NULL); frame->DrawMenuBar(); } }
BOOL CSkinWin::HandleSysCommand(WPARAM wp, LPARAM lp) { int i; if ( lp == 0 ) { CMenu SysMenu; BOOL bBack = SysMenu.Attach( GetSystemMenu(m_hWnd, FALSE) ); if (!bBack) { return FALSE; } int count = SysMenu.GetMenuItemCount(); for( i = 0; i < count ; i++ ) if ( SysMenu.GetMenuItemID(i) == wp ) break; SysMenu.Detach(); if ( i >= count ) return FALSE; if ( wp == SC_MAXIMIZE ) Maximize(); else if ( wp == SC_MINIMIZE ) Minimize(); else if ( wp == SC_RESTORE && m_winstate == 1 ) Restore(); else if ( wp == SC_RESTORE && m_winstate == 2 ) { ShowWindow( m_hWnd, SW_RESTORE ); m_winstate = m_oldwinstate; } else SendMessage( m_hWnd, WM_SYSCOMMAND, wp, lp ); OnNcPaint(0); return TRUE; } return FALSE; }
// we use this as a hook to customize the language menu void CLangModelsDoc::OnUpdateLangProperties(CCmdUI* pCmdUI) { CWnd* pWnd = AfxGetMainWnd(); //CMenu* pTopMenu = pWnd->GetMenu(); // this is wierd but it seemed that we have to search for the menu // I know, it should be 3, but sometimes our commands ended up in the view menu /* CMenu* pMenu = NULL; for(int i=2; i < 5; i++) { CString s; pTopMenu->GetMenuString(i, s, MF_BYPOSITION ); if(s == "&Language") { pMenu = pTopMenu->GetSubMenu(i); break; } } */ CMenu* pMenu = pCmdUI->m_pMenu; if(!pMenu) // null when you make a choice in the menu? return; const int kSynthSeqIndex = 3; //const int kItemsAfterTransfer = 2; const CTypedPtrArray<CPtrArray, CLangModelsDoc*>& pLangDocs = getProject()->getLangDocs(); // remove existing transfer menu items for( int z=pMenu->GetMenuItemCount(); z >= 0; z--) { int iCMD = pMenu->GetMenuItemID(z); // is this a transfer sequence item? if(iCMD >= kTransferCMDBase && iCMD <= (kTransferCMDBase+ pLangDocs.GetSize()-1)) pMenu->DeleteMenu(z, MF_BYPOSITION ); } // now we should have the menu as in the resource editor // stick in an item for each transfer process sequence // note that this is written with the assumption that there // is one transfer sequence for each other language in the // project int iItem = kSynthSeqIndex; // which menu item int iCount = 0; // index into the transfer sequences for(int iTrg=0; iTrg<pLangDocs.GetSize(); iTrg++) { if(pLangDocs[iTrg] != this) { #ifndef hab17a1 CString s("Transfer "); if ((m_pLang->m_wDisplayFlags & m_pLang->kLocateTransferWithTarget) > 0) s += "From "; else s += "To "; #else // hab17a1 CString s("Transfer To "); #endif // hab17a1 s += pLangDocs[iTrg]->getLang()->getName(); s += " Sequence..."; pMenu->InsertMenu(iItem, MF_ENABLED | MF_BYPOSITION | MF_STRING , kTransferCMDBase + iCount, // cmd id s); iItem++; iCount++; } } pWnd->DrawMenuBar(); }
LRESULT CXTPTrayIcon::OnTrayNotification(WPARAM wParam, LPARAM lParam) { // Return quickly if its not for this tray icon if (wParam != m_niData.uID) { return 0L; } if (m_hWndNotify == NULL) { return 0L; } // Check to see if our notification window has already handled this // message, if so then return success. if (::SendMessage(m_hWndNotify, TIN_XTP_TRAYICON, wParam, lParam)) { return 1; } switch (LOWORD(lParam)) { // Sent when the balloon is shown (balloons are queued). case NIN_BALLOONSHOW: break; // Sent when the balloon disappears-for example, when the // icon is deleted. This message is not sent if the balloon // is dismissed because of a timeout or a mouse click. case NIN_BALLOONHIDE: break; // Sent when the balloon is dismissed because of a timeout. case NIN_BALLOONTIMEOUT: break; // Sent when the balloon is dismissed because of a mouse click. case NIN_BALLOONUSERCLICK: break; // The version 5.0 Shell sends the associated application a NIN_SELECT message // if a user selects a notify icon with the mouse and activates it with the ENTER key case NIN_SELECT: // intentional fall thru... // The version 5.0 Shell sends the associated application a NIN_KEYSELECT message // if a user selects a notify icon with the keyboard and activates it with the space bar or ENTER key case NIN_KEYSELECT: // intentional fall thru... // The version 5.0 Shell sends the associated application a WM_CONTEXTMENU // If a user requests a notify icon's shortcut menu with the keyboard case WM_CONTEXTMENU: // intentional fall thru... case WM_RBUTTONUP: { CMenu menu; if (!menu.LoadMenu(m_niData.uID)) { return 0; } CMenu* pSubMenu = menu.GetSubMenu(0); if (pSubMenu == NULL) { return 0; } // Make chosen menu item the default (bold font) ::SetMenuDefaultItem(pSubMenu->m_hMenu, m_uDefMenuItemID, m_bDefMenuItemByPos); // 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); ::SetForegroundWindow(m_hWndNotify); ::TrackPopupMenu(pSubMenu->m_hMenu, 0, pos.x, pos.y, 0, m_hWndNotify, NULL); ::PostMessage(m_hWndNotify, WM_NULL, 0, 0); menu.DestroyMenu(); ShellNotify(NIM_SETFOCUS); } break; case WM_LBUTTONDBLCLK: { // double click received, the default action is to execute default menu item ::SetForegroundWindow(m_hWndNotify); UINT uItem; if (m_bDefMenuItemByPos) { CMenu menu; if (!menu.LoadMenu(m_niData.uID)) { return 0; } CMenu* pSubMenu = menu.GetSubMenu(0); if (pSubMenu == NULL) { return 0; } uItem = pSubMenu->GetMenuItemID(m_uDefMenuItemID); menu.DestroyMenu(); } else { uItem = m_uDefMenuItemID; } ::SendMessage(m_hWndNotify, WM_COMMAND, uItem, 0); } } return 1; }
void CCoolMenuBarCtrl::ShowMenu() { if ( m_pHot == NULL || ! IsMenu( m_hMenu ) ) return; CMenu* pMenu = CMenu::FromHandle( m_hMenu )->GetSubMenu( m_pHot->m_nID - 1 ); if ( pMenu == NULL ) { m_pHot = m_pDown = NULL; Invalidate(); return; } UINT nFirstID = pMenu->GetMenuItemID( 0 ); if ( nFirstID == ID_WINDOW_CASCADE ) { UpdateWindowMenu( pMenu ); } m_pDown = m_pHot; Invalidate(); KillTimer( 1 ); TPMPARAMS tpm; CRect rc; GetItemRect( m_pDown, &rc ); ClientToScreen( &rc ); rc.DeflateRect( 1, 2 ); tpm.cbSize = sizeof(tpm); tpm.rcExclude = rc; m_pMenuBar = this; m_hMsgHook = SetWindowsHookEx( WH_MSGFILTER, MenuFilter, NULL, GetCurrentThreadId() ); CoolMenu.RegisterEdge( rc.left, rc.bottom, rc.Width() ); UINT nCmd = TrackPopupMenuEx( pMenu->GetSafeHmenu(), TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_VERTICAL|TPM_RETURNCMD, rc.left, rc.bottom, GetSafeHwnd(), &tpm ); UnhookWindowsHookEx( m_hMsgHook ); m_hMsgHook = NULL; m_pMenuBar = NULL; m_pDown = NULL; OnTimer( 1 ); if ( m_pHot != NULL ) { SetTimer( 1, 100, NULL ); m_bTimer = TRUE; } Invalidate(); UpdateWindow(); if ( nCmd ) GetOwner()->PostMessage( WM_COMMAND, nCmd ); }