Implementation of Dynamic menus in WTL

Source: Internet
Author: User

During code writing, the program needs to read the secondary node under the root node of CTreeViewCtrl and generate the child menu to add it to the right-click menu of the leaf list. The actual operation is as follows:
1) Delete the original menu
This step is very important, because dynamically generated menus will not be deleted as long as they are added.
// Delete the original menu first
CMenuHandle menu = pMenu-> GetSubMenu (0). GetSubMenu (MY_MOVE_FRIENT_MENU_POS); // MY_MOVE_FRIENT_MENU_POS is defined at the beginning of the program, right-click the position of the Level 2 menu
For (int I = menu. GetMenuItemCount (); I> 0; I --)
{
Menu. RemoveMenu (I, MF_BYPOSITION );
}
2) Secondary node for reading CTreeView

// Read the root node of CTreeViewCtrl first
HTREEITEM hRoot = m_wndLocalList.GetRootItem (); // read the first child node of CTreeViewCtrl, that is, the first child node
HTREEITEM hFirst = m_wndLocalList.GetChildItem (hRoot );
3) insert it to the sub-menu
While (hFirst! = NULL)
{
If (strFirst. Compare (strItemSelParent )! = 0)
{// Judgment statement, which does not insert the parent vertex of the subnode
Menu. InsertMenu (-1, MF_BYPOSITION | MF_STRING, (UINT) ID_MENU_ADD_FRIEND_TO_GROUP + (* piMenuID), strFirst );
* PiMenuID = * piMenuID + 1;
}
// After you perform the Insert menu operation or skip the insert operation, you need to obtain the next Node object hFirst = m_wndLocalList.GetNextItem (hFirst, TVGN_NEXT );
M_wndLocalList.GetItemText (hFirst, strFirst );}
4) In BEGIN_MSG_MAP () and END_MSG_MAP, add
COMMAND_RANGE_HANDLER (ID_MENU_ADD_FRIEND_TO_GROUP, ID_MENU_ADD_FRIEND_TO_GROUP + 100, OnMenuAddToFriendGroupRange) COMMAND_RANGE_HANDLERTo set the menu ID range. As long as your dynamic menu ID is in this range, ID_MENU_ADD_FRIEND_TO_GROUP is defined as a const int number and resource. h.
The program uses OnMenuAddToFriendGroupRange (WORD, WORD pos, HWND, BOOL &) to respond to the menu.
// Obtain the selected item HTREEITEM hItemSel = m_wndLocalList.GetSelectedItem ();
// Delete
M_wndLocalList.DeleteItem (hItemSel); // Insert a new project. hGroupItem can be obtained only by traversing and comparing it.
HTREEITEM hNewItem = m_wndLocalList.InsertItem (strSelectedText, hGroupItem, TVI_SORT); // information sent to the server last time
Add_to_group ();

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.