VC + + Modify software program Menu instance

Source: Internet
Author: User

Modify the software generally use exescope software can be on the software interface, title and other modifications, described here using VC + + modified CMenu Menu

。 Example 1.

Using Cwnd::getmenu () to access the main menu, GetMenu () returns a pointer to the CMenu object, which has some member functions that allow us to modify a menu.
1) How to implement Find a menu item:
The steps are as follows:
{
To modify the menu dynamically:
Get the Main Menu
cmenu* Pmainmenu = AfxGetMainWnd ()->getmenu ();
cmenu* psubmenu = NULL;
int i;
for (i=0; i< (int) pmainmenu->getmenuitemcount (); i++)
{
Psubmenu = Pmainmenu->getsubmenu (i);
if (psubmenu && psubmenu->getmenuitemid (0) = = id_file_new)
Break
}
CString s;
S.format ("%d", I);//The number of bits of the menu item.
AfxMessageBox (s);
ASSERT (Psubmenu);
}

2) dynamic Edit menu:
The steps are as follows (you can use the Psubmenu of the example above to add the menu you define yourself.):
1) Add a menu command called WZD2 with command ID idc_name_new1 to the menu, which can be used:
Psubmenu->appendmenu (0,idc_name_new1, "new&1");

2) Insert New2 before New1, can be used:
Psubmenu->insertmenu (Idc_name_new1,mf_bycommand,idc_name_new2, "new&2");

3) Change New1 into NEW3, can be used:
Psubmenu->modifymenu (IDC_NAME_NEW1,MF_BYCOMMAND,IDC_NAME_NEW3, "new&3");

4) Delete the second item in the menu and you can use:
Psubmenu->removemenu (1,mf_byposition);

==================== X-ray × X-ray x x-======================

Using the menu to accept user commands is a very simple method of interaction, but also a very effective method. The menu is usually stored in a file as a resource, so we can design the good one menu using the Resource Editor at design time.

The task we have to face is how to know when the user chooses the menu and what kind of item he chooses. When a user selects a valid menu item, the system sends an WM_COMMAND message to the app, indicating the source in the message's parameters. In MFC we only need to map one time to map a menu ID to a handler function. Here we process the menu message in the derived class of CView, and set the two message map for the same ID, and then the two mappings will function.

The On_command mapping functions to invoke the specified handler when the user chooses the menu. such as: On_command (Idm_command1, OnCommand1) invokes the ONCOMMAND1 member function when the menu is selected.

The role of the ON_UPDATE_COMMAND_UI (IDM_COMMAND1, OnUpdateCommand1) mapping is to determine its state by invoking the specified function when the menu is displayed. In this handler, you can set the allowed/forbidden state of the menu, what the display string is, and whether to tick the front. The parameter of the function is ccmdui* Pcmdui,ccmdui is a class provided by MFC specifically for the update command and can be called

Enable set allow/disable status
SetCheck set whether to hook in front
SetText Setting text

One example: There is a variable m_fselected in the CView derived class, and the two menu messages are processed in the viewport, when Idm_command1 is selected, the m_fselected is logically non-operational, and a hint is given when the Idm_command2 is selected At the same time, IDM_COMMAND1 determines the allowed/forbidden state of the menu based on the value of m_fselected, based on the text displayed by the M_fselected menu and whether the check mark is in front of the idm_command2.

void Cmenudview::oncommand1 ()
{
m_fselected=!m_fselected;
TRACE ("Command1 selected\n");
}

void Cmenudview::onupdatecommand1 (ccmdui* pcmdui)
{
Pcmdui->setcheck (m_fselected);//decide to check the status
Pcmdui->settext (m_fselected? ") Currently selected ":" Is not currently selected ");//determines which text is displayed
}


void Cmenudview::onupdatecommand2 (ccmdui* pcmdui)
{//Decide whether to allow
Pcmdui->enable (m_fselected);
}

void Cmenudview::oncommand2 ()
{//Prompt when selected
AfxMessageBox ("You have chosen Command2");
}

Again, some ways to manipulate menus through code, there is a class CMenu in MFC to handle menu-related functions. When generating a CMenu object, you need to load the order from the resource, load it by calling the bool Cmenu::loadmenu (UINT nIDResource), and then dynamically modify the menu to include the following functions:
cmenu* getsubmenu (int nPos) Gets a pointer to a submenu because a CMenu object can only represent a popup menu, and if an item in the menu is also a popup menu, you need to get the pointer through the function.

BOOL AppendMenu (UINT nflags, uint nIDNewItem = 0, LPCTSTR Lpsznewitem = NULL) Add an entry at the end, Nflag to Mf_separator to add a separator bar so that the other two The parameters are ignored, a menu item is added for Mf_string, Uidnewitem is the ID command value for the menus, and a pop-up menu item is added for Mf_popup, and Uidnewitem is a handle to the other menu hmenu. Lpsznewitem is the menu text description.

BOOL InsertMenu (UINT nposition, uint nflags, uint nIDNewItem = 0, LPCTSTR Lpsznewitem = NULL) is used to insert a menu at a specified location, where the variable is nposition Specified. If nflags contains Mf_byposition, it indicates that the insertion is in the nposition position if the containing Mf_bycommand represents the insertion at the command ID of the Nposition menu.

BOOL Modifymenu (UINT nposition, uint nflags, uint nIDNewItem = 0, LPCTSTR Lpsznewitem = NULL) are used to modify a menu for a location if nflags contains mf_by Position indicates a menu that modifies the nposition location if the containing Mf_bycommand represents a change to the menu where the command ID is nposition.

A BOOL removemenu (uint nposition, uint nflags) is used to remove a menu from a location. If nflags contains mf_byposition, the menu that indicates the deletion of the nposition location is included, if Mf_bycommand represents the Delete command ID for the menu at nposition.

BOOL AppendMenu (UINT nflags, uint nidnewitem, const cbitmap* PBMP) and bool InsertMenu (UINT nposition, uint nflags, uint nIDNewItem, const cbitmap* PBMP) can add a single-bit diagram menu, but such a menu is only reversed when selected, and is not aesthetically pleasing.

There is no menu in the view, only in the frame window, so you can only use AfxGetApp ()->m_pmainwnd->getmenu () to get the app's menu pointer.

Finally, how to pop a menu in a program, you must first load a menu resource, need to get a pointer to a popup menu and then call bool TrackPopupMenu (UINT nflags, int x, int y, cwnd* pWnd, Lpcrect lpRect = NULL) Pop-up menu, you need to specify (x, y) where the menu pops up, pwnd is the window pointer that receives the command message. Here is a code description method. Of course, in order to process the message, the menu command message should be mapped in the window indicated by pwnd.

CMenu menu;
Menu. LoadMenu (Idr_popup);
cmenu* Pm=menu. GetSubMenu (0);
CPoint pt;
GetCursorPos (&AMP;PT);
Pm->trackpopupmenu (Tpm_leftalign,pt.x,pt.y,this);

Another approach is to create a pop-up menu with Cmenu::createpopupmenu () and then use the TrackPopupMenu pop-up menu. Menus created with CreatePopupMenu can also be added as a popup in another menu. The following pseudocode demonstrates how to create a pop-up menu and make changes to it:

CMenu menu1,menu2;
Menu1. CreatePopupMenu
Menu1. InsertMenu (1)
Menu1. InsertMenu (2)
Menu1. InsertMenu (3)

Menu2. CreatePopupMenu
Menu2. AppendMenu (mf_popup,1,menu1. Detach ()) Add pop-up menu to or insertmenu ...
Menu2. InsertMenu ("string desc");
Menu. TrackPopupMenu (...)

end.

VC + + Modify software program Menu instance

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.