CTreeCtrl 控件,在创建的时候已经设置了TVS_EDITLABELS属性,也映射了TVN_BEGINLABELEDIT TVN_ENDLABELEDIT消息。
可以重命名,但是必须是失去焦点后才能重命名有效,按回车不响应无效。如何才能使按回车就能重命名。就像资源管理器你的文件夹一样。

解决方案 »

  1.   

    如果你的类从CTreeCtrl继承的,例如CMyTreeCtrl,可以在BOOL CMyTreeCtrl::PreTranslateMessage(MSG* pMsg)方法里做如下处理。
    switch(pMsg->message)
             {
    case WM_KEYDOWN:
    if (pMsg->wParam == VK_RETURN)
                               {
    CEdit *pEdit = this->GetEditControl();
    if (NULL != pEdit)
                                        {
    pEdit->GetWindowTextA(str);
    NMTVDISPINFO msg;
    msg.hdr.hwndFrom = this->m_hWnd;
    msg.hdr.idFrom = this->GetDlgCtrlID();
    msg.hdr.code = TVN_ENDLABELEDIT;
    msg.item.mask = TVIF_TEXT;
    msg.item.pszText = str.GetBuffer(0);
    msg.item.cchTextMax = str.GetLength(); if (this->GetParent())
                                                  {
    this->GetParent()->SendMessage(WM_NOTIFY, (WPARAM)msg.hdr.idFrom, (LPARAM)&msg);
    }
    }
    }
    break;
    }
      

  2.   

    我给个简单的:
    先想方设法得到Enter的消息,我这里也用CMyTreeCtrl::PreTranslateMessage。switch(pMsg->message)
             {
    case WM_KEYDOWN:
    if (pMsg->wParam == VK_RETURN)
                               {
        this->SetFocus();
                                   //SetFocus()可以触发TVN_ENDLABELEDIT消息

    }
    break;
    }