MFC study notes 5 Menu

One. Message Classification

  • Command message: WM_COMMOND menu handler
  • Standard Message: view-> Properties -> Message
  • Advertisement message: Control handler

 

two. Menu Operation

In the menu operation int CMainFrame :: OnCreate (LPCREATESTRUCT lpCreateStruct)

1      // Flag
 2      // Get the menu
 3      // or the main menu 
. 4      the CMenu MENU * = the GetMenu (); 
 . 5      // Get the first submenu 
. 6      the CMenu * FileMenu = Menu-> the GetSubMenu ( 0 ); 
 . 7      // tag "New", if the first index, the MF_BYPOSITION 
. 8      fileMenu-> CheckMenuItem ( 0 , MF_BYPOSITION | MF_CHECKED);
 . 9      // flag "on", if the first ID, the MF_BYCOMMAND 
10      fileMenu-> CheckMenuItem (ID_FILE_OPEN, MF_BYCOMMAND | MF_CHECKED);
 11  
12      // set the default item, bold, a menu item is only a default
 13      //FALSE: ID
 14      // TRUE: position 
15      fileMenu-> SetDefaultItem (ID_FILE_SAVE, FALSE);
 16      // dimmed
 17      // required m_bAutoMenuEnable :: = FALSE the CFrameWnd; 
18 is      fileMenu-> EnableMenuItem, ( . 5 , MF_BYPOSITION | MF_DISABLED);
 . 19      
20      // remove menu 
21 is      the SetMenu (NULL);
 22 is  
23 is      // load menu 
24      the CMenu Menu2;
 25      menu2.LoadMenuW (IDR_MENU1);
 26 is      the SetMenu (& Menu2);
 27      // separator 
28      menu2.Detach ();    

 

Click B, A is highlighted update

1. Add an event handler for A

// menu updates are automatically updated, the flag bits 
void the CMainFrame :: OnUpdateA (CCmdUI * pCmdUI) 
{ 
    // the TODO: add this command updates the user interface handler code 
    IF (m_isUpdate == TRUE) 
    { 
        pCmdUI -> the Enable ( TRUE); 
    } 
    the else { 
        pCmdUI -> the Enable (FALSE); 
    } 
}

2.B add an event handler

. 1  void the CMainFrame :: ONB ()
 2  {
 . 3      // the TODO: this addition command handler code 
. 4      ! = M_isUpdate m_isUpdate;   // click update flag 
5 }

 

three. Left pop-up menu

. 1  void CMenuView :: The OnLButtonDown (UINT nFlags, a CPoint Point)
 2  {
 . 3      // the TODO: this addition message handler code, and / or calling the default values
 . 4  
. 5      // create menu items 
. 6      the CMenu MENU;
 . 7      menu.LoadMenuW (IDR_MENU1 );
 8      // Get the first submenu 
. 9      the CMenu subMenu * = menu.GetSubMenu ( 0 );
 10  
. 11      // coordinate converter 
12 is      ClientToScreen (& Point); 
 13 is      // submenu as the shortcut menu, right-click menu 
14      subMenu -> TrackPopupMenu (of TPM_LEFTALIGN | TPM_LEFTBUTTON,
 15          point.x, point.y,this);
16     
17     CView::OnLButtonDown(nFlags, point);
18 }

 

four. Dynamic Update window icon

1. First, import ico icon in the resource, in class CMainFrame: public CFrameWnd add an icon array in

 

2. Create a timer OnCreate and generates handlers

 

. 1  void the CMainFrame :: the OnTimer (UINT_PTR nIDEvent)
 2  {
 . 3      // the TODO: this addition message handler code, and / or calling the default values 
. 4      static  int I = 0 ;
 . 5      // modify header 
. 6      SetClassLong (m_hWnd, GCL_HICON, ( a LONG) icon [I]);
 . 7      I ++ ;
 . 8      IF (I == . 4 )
 . 9          I = 0 ;
 10      the CFrameWnd :: the OnTimer (nIDEvent);
 . 11 }

 

Guess you like

Origin www.cnblogs.com/sclu/p/11571469.html