(MFC Application series) Basic menu operations (Lesson 4) and basic mfc operations

Source: Internet
Author: User

(MFC Application series) Basic menu operations (Lesson 4) and basic mfc operations
The menu in the single-document application created by the Wizard is owned by the framework class, so the operations with the ticket must be carried out in the framework class. The following examples are all performed in the OnCreate function of the CMainFrame class. Note that GetMenu is a member function of the CWnd class, And GetSubMenu is a member function of the CMenu class.

I. Mark menu items

Call the CheckMenuItem function.
// Access the menu item GetMenu ()-> GetSubMenu (0)-> CheckMenuItem (0, MF_BYPOSITION | MF_CHECKED) through the menu item index ); // You can also use the menu item ID to access the menu item // GetMenu ()-> GetSubMenu (0)-> CheckMenuItem (ID_FILE_NEW, MF_BYCOMMAND | MF_CHECKED );
Ii. Use the SetMenuItemBitmaps function to set the graphic tag for the graphic tag menu item.
Static CBitmap bitmap; // It is defined as a member variable or a static variable. Otherwise, after the OnCreate function ends, the object is destructed. The corresponding resource is destroyed and the image cannot be displayed. bitmap. loadBitmapW (IDB_BITMAP1); GetMenu ()-> GetSubMenu (0)-> SetMenuItemBitmaps (1, MF_BYPOSITION, & bitmap, & bitmap );

You can use the GetSystemMetrics function to obtain the default width and height of the menu item markup image. The parameter SM_CXMENUCHECK or SM_CYMENUCHECK indicate the width and height of the menu item markup image respectively.

    CString str;    str.Format(L"cx=%d,cy=%d",GetSystemMetrics(SM_CXMENUCHECK),GetSystemMetrics(SM_CYMENUCHECK));    MessageBox(str);
3. Default menu items default menu items are displayed in bold. A sub-menu can have only one default menu item.
GetMenu ()-> GetSubMenu (0)-> SetDefaultItem (2, TRUE); // you can specify the default menu item.
4. Disable menu items. By default, all menu items are updated by the command update mechanism of MFC. If you want to change the menu item status, you must first set the member variable m_bAutoMenuEnable of the CMainFrame class to FALSE, which is generally carried out in the constructor of the CMainFrame class, so that you do not need to respond to the ON_UPDATE_COMMAND_UI or ON_COMMAND message, the menu command update mechanism is no longer used to determine which menu can be used and which menu cannot be used. The EnableMenuItem member function of the CMenu class will work normally.
    GetMenu()->GetSubMenu(0)->EnableMenuItem(3,MF_BYPOSITION|MF_DISABLED|MF_GRAYED);
5. When setting the window menu, if you define a local menu object, you must call the SetMenu function to set the window menu, call the Detach function of the menu object to separate the menu handle from the menu object.
SetMenu (NULL); // remove menu CMenu menu; menu. loadMenuW (IDR_MAINFRAME); SetMenu (& menu); // reload the menu. detach (); // separates the menu object from the menu handle, so that the menu resources corresponding to the menu will not be destroyed when the local variable is destructed.

Menu item mark menu item graphic mark disabled menu item default menu item

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.