CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Jun 2004
    Location
    Chicago, United States
    Posts
    88

    MFC Edit Control: How to know when text is pasted from clipboard to 'CEdit' control?

    Q: How to know when text is pasted from clipboard to 'CEdit' control?

    A: In the destination application derive all edit controls from 'CEdit' and override 'OnChar()' and 'OnContextMenu()':

    Code:
    void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
    {
      if(nChar==22) // Ctrl-V
      TRACE("CTRL-V used to paste the text\n");
      CEdit::OnChar(nChar, nRepCnt, nFlags);
    }
    
    void CMyEdit::OnContextMenu(CWnd* pWnd, CPoint point) 
    {
      CMenu menu;
      menu.CreatePopupMenu();
      if(CanUndo())
        menu.AppendMenu(MF_STRING,1,_T("Undo"));
      else
        menu.AppendMenu(MF_STRING|MF_GRAYED,1,_T("Undo"));
    
      menu.AppendMenu(MF_SEPARATOR,2);
      int nSel1,nSel2;
      GetSel(nSel1,nSel2);
      if(nSel1!=nSel2)
        menu.AppendMenu(MF_STRING,3,_T("Cut"));
      else
        menu.AppendMenu(MF_STRING|MF_GRAYED,3,_T("Cut"));
    
      if(nSel1!=nSel2)
        menu.AppendMenu(MF_STRING,4,_T("Copy"));
      else
        menu.AppendMenu(MF_STRING|MF_GRAYED,4,_T("Copy"));
    
      if(::IsClipboardFormatAvailable(CF_TEXT))
        menu.AppendMenu(MF_STRING,5,_T("Paste"));
      else
        menu.AppendMenu(MF_STRING|MF_GRAYED,5,_T("Paste"));
    
      if(nSel1!=nSel2)
        menu.AppendMenu(MF_STRING,6,_T("Delete"));
      else
        menu.AppendMenu(MF_STRING|MF_GRAYED,6,_T("Delete"));
    
      menu.AppendMenu(MF_SEPARATOR,7);
      CString s;
      GetWindowText(s);
      if(s.IsEmpty())
        menu.AppendMenu(MF_STRING|MF_GRAYED,8,_T("Select All"));
      else
        menu.AppendMenu(MF_STRING,8,_T("Select All"));
      
      int nRet = menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD, point.x, point.y, this);
      switch(nRet)
      {
        case 1:
          Undo();
          break;
    
        case 3:
          Cut();
          break;
      
        case 4:
          Copy();
          break;
    
        case 5:
          TRACE("Popup menu used to paste the text\n");
          Paste();
          break;
      
        case 6:
          Clear();
          break;
      
        case 8:
          SetSel(0,-1);
          break;
      }
    }
    Last edited by Andreas Masur; July 25th, 2005 at 03:37 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured