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 ();