Add Undo,Cut,Paste and Copy to the EditCtrl in the ListCtrl and TreeCtrl

This code shows how to Enable Ctrl Z, Ctrl V, Ctrl X and Ctrl C hot Keys for Cut,Paste and Copy in the EditCtrl that appears in the ListCtrl. Even the Ctrl + Insert and Shift + Insert work for this Code.

This is a slight modification to the Article written by Zafir Anjum.

#define VK_C        67
#define VK_V        86
#define VK_X        88
#define VK_Z        90

BOOL CMyListView::PreTranslateMessage(MSG* pMsg)
{
    // If edit control is visible in tree view control, sending a
    // WM_KEYDOWN message to the edit control will dismiss the edit
    // control.  When ENTER key was sent to the edit control, the parent
    // window of the tree view control is responsible for updating the
    // item's label in TVN_ENDLABELEDIT notification code.
    if ( pMsg->message == WM_KEYDOWN )
    {
        CListCtrl& oListctrl = GetListCtrl( );
        CEdit* edit = oListctrl.GetEditControl();
        if (edit)
        {
            if( GetKeyState( VK_CONTROL )
            {
                if( pMsg->wParam == VK_C )
                {
                    edit->Copy();
                    return TRUE;
                }
                if(  pMsg->wParam == VK_V )
                {
                    edit->Paste();
                    return TRUE;
                }
                if(  pMsg->wParam == VK_X )
                {
                    edit->Cut();
                    return TRUE;
                }
                if(  pMsg->wParam == VK_Z )
                {
                    edit->Undo();
                    return TRUE;
                }
            }
            if( pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE || 
                pMsg->wParam == VK_CONTROL || pMsg->wParam == VK_INSERT || 
                pMsg->wParam == VK_SHIFT )
            {
                edit->SendMessage(WM_KEYDOWN, pMsg->wParam, pMsg->lParam);
                return TRUE;
            }
        }
    }
    return CListView::PreTranslateMessage(pMsg);
}

Add this Function as a protected Member function to your ListView or ListCtrl header file.

    virtual BOOL PreTranslateMessage(MSG* pMsg);

Map functions for the Notification Messages LVN_BEGINLABELEDIT and LVN_ENDLABELEDIT as described in Zafir Anjum's Article. I have added this below for your convenience.
void CMyListView::OnBeginLabelEdit( NMHDR* /*pNMHDR*, LRESULT* pResult)
{
    // This is the Limit the size of the Intem Text to 127
    CListCtrl& oListctrl = GetListCtrl( );
    oListctrl.GetEditControl()->LimitText( 127 );
    *pResult = 0;
}

void CMyListView::OnEndLabelEdit( NMHDR * pNMHDR, LRESULT *  pResult )
{
    *pResult = TRUE;
}

Add the Declarations in the CMyListCtrl.h file. Similar Procedure will work for the TreeCtrls also.

I would like to thank Codeguru.com and its team for Publishing this article on their site. Please do post me any modification that will improve the above code.

Download demo project - 20 KB



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: November 13, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT APIs can be a great source of competitive advantage. The practice of exposing backend services as APIs has become pervasive, however their use varies widely across companies and industries. Some companies leverage APIs to create internal, operational and development efficiencies, while others use them to drive ancillary revenue channels. Many companies successfully support both public and private programs from the same API by varying levels …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds