如何重命名树控制项 [英] How rename tree control item

查看:85
本文介绍了如何重命名树控制项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我有树控制。我想在其上做以下事情。

当用户右键单击任何树项时,会打开一个弹出菜单。用户从中选择重命名菜单并重命名树项。



有人能告诉我怎么做吗?我正在使用VS2005。

Hi All

I have a tree control. I want to do following thing on it.
When user right click on any tree item a popup menu is opened. User select 'Rename' menu from this and rename the tree item.

Can anybody tell me how to do this? I am using VS2005.

推荐答案

典型的解决方案可能如下所示:

A typical solution might look like this:
BEGIN_MESSAGE_MAP(CMyTreeCtrl, CTreeCtrl)
    ON_WM_CONTEXTMENU()
END_MESSAGE_MAP()

void CMyTreeCtrl::OnContextMenu(CWnd* pWnd, CPoint point)
{
    HTREEITEM hCurSel = GetSelectedItem();
    if (point.x < 0 && point.y < 0) // if opened by keyboard (not mouse)
    {
        RECT rect;
        if (hCurSel)
        {
            GetItemRect(hCurSel, &rect, TRUE); // relative to control, text only
            ClientToScreen(&rect); // convert to screen coordinates
            point.SetPoint(rect.left, rect.bottom); // below selected item
        }
        else
        {
            GetWindowRect(&rect);
            point.SetPoint(rect.left, rect.top); // left/top of control
        }
    }
    else // Select the item that is at the clicked point
    {
        // When right clicking on the control we should get the focus first
        //  to show the active selection. 
        // Because many context commands use the selection (Copy, Cut, Clear).
        if (GetFocus() != this)
            SetFocus();
        CPoint pt(point); // we need window relative coordinates
        ScreenToClient(&pt);
        hCurSel = HitTest(pt);
        if (hCurSel != NULL)
            SelectItem(hCurSel);
        else // if not clicked on item (e.g. on scrollbar)
        { //  use default handling
            CTreeCtrl::OnContextMenu(pWnd, point);
            return;
        }
    }

    // Example loading menu from resources
    CMenu menu;
    VERIFY(menu.LoadMenu(IDR_MYTREECTRL_POPUP_MENU));
    CMenu *pSub = menu.GetSubMenu(0); // Get the pop-up menu.

    // Gray out or remove items according to various conditions
    if (NULL == hCurSel)
        pSub->EnableMenuItem(IDC_RENAME_ITEM, MF_GRAYED);

    // Show the popup menu.
    // Example uses TPM_RETURNCMD to return the ID of the selected menu item
    //  rather than calling the handler.
    int nCommand = pSub->TrackPopupMenuEx(
        TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_VERPOSANIMATION | TPM_RETURNCMD | TPM_NONOTIFY,
        point.x, point.y, 
        AfxGetMainWnd(), NULL);

    switch (nCommand)
    {
    case IDC_RENAME_ITEM : // rename menu item
        SetItemText(hCurSel, _T("New item text"));
        break;
    default : // all other menu items
        if (nCommand)
            SendMessage(WM_COMMAND, nCommand);
    }
}


在带有treeview的表单上,为System.Windows.Forms.ContextMenuStrip(它在你的工具箱上)创建对象



将treeview的ContextMenuStrip属性设置为此对象。



在上下文菜单条上添加菜单项重命名。



处理新添加菜单的OnClick事件......
On the form with treeview, create object for System.Windows.Forms.ContextMenuStrip (Its on your toolbox)

Set treeview's ContextMenuStrip property to this object.

Add menu item Rename on the Context menu strip.

Handle newly added menu's OnClick event....


这篇关于如何重命名树控制项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆