MFC menu bar related knowledge

1. The main member function of the CMenu class

       MFC provides the CMenu class for menu operations. The following is a brief introduction to several commonly used member functions.

       BOOL LoadMenu(UINT nIDResource);

       Load the menu resource and attach it to the CMenu object. The parameter nIDResource specifies the ID of the menu resource to be loaded. Returns TRUE if the menu is loaded successfully, otherwise returns FALSE.

       BOOL DeleteMenu(UINT nPosition,UINT nFlags);

       Removes a menu item from the menu. The parameter nPosition specifies the menu item to delete. The parameter nFlags is used to explain the meaning of nPosition. When it is MF_BYCOMMAND, it means that nPosition means the ID of the menu item. When it is MF_BYPOSITION, it means that nPosition means the position of the menu item, and the position of the first menu item is 0. Returns TRUE if the menu item was removed successfully, otherwise returns FALSE.

       BOOL TrackPopupMenu(UINT nFlags,int x,int y,CWnd* pWnd,LPCRECT lpRect = 0);

       Used to display a floating popup menu at the specified location. The parameter nFlags specifies the flags of screen coordinates and mouse position, which can be the following values:

       TPM_CENTERALIGN: The menu is centered in the horizontal direction relative to the coordinate value specified by parameter x
       TPM_LEFTALIGN: The left side of the menu is aligned with the coordinate value specified by parameter x
       TPM_RIGHTALIGN: The right side of the menu is aligned with the coordinate value specified by parameter x
       TPM_BOTTOMALIGN: The bottom of the menu Align with the coordinate value specified by the parameter y
       TPM_TOPALIGN: The top of the menu item is aligned with the coordinate value specified by the parameter y
       TPM_VCENTERALIGN: The menu is displayed vertically centered relative to the coordinate value specified by the parameter y

       Here are some of the more commonly used ones, and others can be found on MSDN. The parameter x specifies the horizontal screen coordinates of the pop-up menu, the parameter y specifies the screen coordinates in the vertical direction at the top of the menu, and the parameter pWnd specifies which window owns the pop-up menu. It cannot be NULL, and the parameter lpRect is ignored.

       UINT CheckMenuItem(UINT nIDCheckItem,UINT nCheck);

       Add a checkmark to or remove a checkmark from a menu item in the popup menu. The parameter nIDCheckItem specifies the menu item to be checked or unchecked. The parameter nCheck specifies the selected state of the menu item and how to determine the position of the menu item according to nIDCheckItem, which can be a combination of MF_CHECKED or MF_UNCHECKED and MF_BYPOSITION or MF_BYCOMMAND. The meanings of these flags are as follows:

       MF_BYCOMMAND: is the default value. Description parameter nIDCheckItem represents the ID of the menu item
       MF_BYPOSITION: Description parameter nIDCheckItem represents the position of the menu item, the position of the first menu item is 0
       MF_CHECKED: Add a check mark
       to the menu item MF_UNCHECKED: Remove the check mark for the menu item

       This function returns the previous state of the menu item: MF_CHECKED or MF_UNCHECKED, or 0xFFFFFFFF if the menu item does not exist.

       UINT EnableMenuItem(UINT nIDEnableItem,UINT nEnable);

       Activates, deactivates, or grays out menu items. The parameter nIDEnableItem specifies the menu item to be activated, deactivated, or grayed out. The parameter nEnable specifies the type of operation, which can be a combination of MF_DISABLED, MF_ENABLED, or MF_GRAYED and MF_BYCOMMAND or MF_BYPOSITION. The meanings of these values ​​are as follows:

       MF_BYCOMMAND: same as CheckMenuItem
       MF_BYPOSITION: same as CheckMenuItem MF_DISABLED: disable        menu
       item so that it cannot be selected but not grayed
       out
selected and greyed out

       This function returns the previous state of the menu item: MF_DISABLED, MF_ENABLED or MF_GRAYED

       CMenu* GetSubMenu(int nPos) const;

       Get the CMenu object of the popup menu. The parameter nPos specifies the position of the popup menu in the menu, and ID cannot be used. The return value is a pointer to a CMenu object. The m_hMenu member of the CMenu object is the handle of the popup menu specified by nPos. If no such CMenu object exists, it returns NULL, and then a temporary popup menu is created.

   

/** Function prototypes in MFC */

/**
  * @param nPosition The position index of the menu item, with 0 as the first
  * @param nFlags flag bit, MF_BYCOMMAND, MF_BYPOSITION, MF_STRING, can be used in combination
  * @param nIDNewItem resource ID
  * @param lpszNewItem menu content text
  */
BOOL ModifyMenu(
   UINT nPosition,
   UINT nFlags,
   UINT_PTR nIDNewItem = 0,
   LPCTSTR lpszNewItem = NULL
);

/**
  * @param nPosition The position index of the menu item, with 0 as the first
  * @param nFlags flag bit, MF_BYCOMMAND, MF_BYPOSITION, MF_STRING, can be used in combination
  * @param nIDNewItem resource ID
  * @param pBmp image resource
  */
BOOL ModifyMenu(
   UINT nPosition,
   UINT nFlags,
   UINT_PTR nIDNewItem,
   const CBitmap* pBmp
);


Detailed description: MF_BYPOSITION is mainly used to mark menu items that need to be operated by offset position; MF_BYCOMMAND is mainly used for command ID; MF_STRING is mainly used to change the text content of POPUP menu items. Among them, pay special attention to the fact that the resource ID must not be modified, otherwise the originally defined response event will lose its effect.


   2.  Menu message

       The menu can mainly send two kinds of messages: COMMAND message and UPDATE_COMMAND_UI message. Each menu item can create these two messages:

       COMMAND message: This message is sent when a menu item is clicked.

       UPDATE_COMMAND_UI message: used to maintain various states of menu items, including activated, disabled, grayed out, checked, unchecked, etc. This message is sent for all menu items each time the dropdown menu is opened. If a processing function is added for the message of the menu item in the class to which it belongs, the corresponding function will be executed to update the menu state. If the menu item does not have this message processing function or the COMMAND message processing function, it will be grayed out.


3. Use of EnableMenuItem

        

Main program source code:

[html] view plain copy

    *pMenu;  
    pMenu = AfxGetMainWnd()->GetMenu();  
    pMenu->EnableMenuItem(ID_RESET, MF_GRAYED);  
Among them, ID_RESET is the menu option object that needs to be operated.

CMenu::EnableMenuItem

Allow or disallow specified menu items

UINT EnableMenuItem(UINT nIDEnableItem, UINT nEnable); 

Return Value

Status MF_DISABLED, MF_ENABLED, MF_GRAYED, or -1 (inactive)

parameter

nIDEnableItem, the identifier of a menu item to be enabled or disabled. This parameter can be specified as a pop-up or standard menu entry.

nEnable, specifies the menu item action. For this function, only the following constants can be specified:

MF_BYCOMMAND,MF_BYPOSITION,MF_ENABLED,MF_DISABLED以及MF_GRAYED

  These values ​​have the following meanings:

  · MF_BYCOMMAND The specified parameter gives the command ID number of an existing menu item. This is the default value.

  · MF_BYPOSITION The specified parameter gives the position of an existing menu item. The position of the first item is 0.

  · MF_DISABLED disables the menu item so that it cannot be selected but not dimmed.

  · MF_ENABLED enables the menu item so that it can be selected and recovered from a grayed-out state.

  · MF_GRAYED invalidates the menu item so that it cannot be selected and grayed out at the same time.

But there will be problems, the menu items cannot be grayed out as expected, just add a line of code to the constructor of MainFrame: (very easy to use)
this->m_bAutoMenuEnable  =FALSE; 


4. The use of multiple menu bar switching

    First create two CMenu classes 

  

CMenu menu1;
CMenu menu2;

    Second add toggle event

 

static bool bchange;
if(bchange)//Use the first Menu
{
  menu1.load (IDR_MENU1);
  AfxGetMainWnd()->SetMenu(&menu1);//设置Menu
  AfxGetMainWnd()->DrawMenuBar();//Redraw
  menu1.Detach();
}
else//Switch to the second Menu
if(bchange)
{
  menu2.load (IDR_MENU1);
  AfxGetMainWnd()->SetMenu(&menu2);//设置Menu
  AfxGetMainWnd()->DrawMenuBar();//Redraw
  menu2.Detach();
}
bchange = !bchange;
ps: To achieve switching, you must have two Menu resources, or you can use dynamic changes (ModifyMenu to update the menu)


About Detach and Attch

Attach actually binds a HWND handle to a CWnd object (assignment to m_hWnd); //Attach() is to connect the WINDOWS window handle to the CWnd object.
Detach actually strips the HWND from a CWnd object (making m_hWnd as NULL). //Detach() is to disconnect the WINDOWS window handle from the CWnd object 1. To understand Attch(), you must first understand the difference between the C++ class object (MFC class object) and the handle, of course, you must understand the handle meaning. There can be several examples.
  CWnd wnd;//A CWnd class object is declared here, that is, a piece of memory is allocated in the memory, and a CWnd object is stored.
  In Window, each actual window is represented by a handle. HANDLE is a pointer to a pointer.
2. Each CWnd class object in MFC corresponds to a handle, and can only correspond to one handle.
MFC internally maintains a HWND to CWnd class mapping table. You can find which HWND corresponds to which CWnd class object.
Such as: CWnd* pWnd = CWnd::FromHandlePermanent(hwnd);

You can also create a temporary object for a HWND, such as CWnd* pWnd = CWnd::FromHandle(hwnd);

However, not every HWND has an object corresponding to it




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325870389&siteId=291194637