MFC -CRichEditCtrl Paste Text

abc abc 351 Reputation points
2020-09-08T12:29:17.97+00:00

Hi,

In MFC dialog based application, I have subclassed CRicheditctrl.

In the cricheditctrl, If a text is cut/copy from same richeditctrl, I need to allow pasting of text otherwise, if a text is copied from other control or microsoft word kind of applications, I should not allow pasting of text.

How can we come to know that a text is cut/copied from same control or from different control?

Thanks in advance

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,690 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Castorix31 84,471 Reputation points
    2020-09-08T14:21:16.297+00:00

    A way is with a Clipboard Listener by testing the focused window from where a text is copied

    For example, a test in a C++/Win32 app, hWnd being the main window :

    AddClipboardFormatListener(hWnd);
    

    then in the WndProc :

    case WM_CLIPBOARDUPDATE:
    {
        GUITHREADINFO gui;
        ZeroMemory(&gui, sizeof(gui));
        HWND hWndForeground = GetForegroundWindow();
        if (hWndForeground != NULL)
        {
            gui.cbSize = sizeof(gui);
            GetGUIThreadInfo(GetWindowThreadProcessId(hWndForeground, NULL), &gui);
        }
        HWND hWndFocus = gui.hwndFocus;
        // Test here if hWndFocus = your RichEdit window
        // For test, checking of the class name
        WCHAR wsClass[255];
        GetClassName(hWndFocus, wsClass, 255);
    }
    break;
    
    1 person found this answer helpful.

  2. RLWA32 45,236 Reputation points
    2020-09-09T09:52:00.683+00:00

    when typing ctrl+x, ctrl+c, after returning from pretranslatemessage only clipboard sequence number is changing so when to call getclipboardsequencenumber?

    Assuming that your CRichEditCtrl handlers for Ctrl+x and Ctrl+c use WM_CUT and WM_COPY it can also handle Ctrl+v to use WM_PASTE and override CRichEditCtrl::WindowProc to handle copy/cut/paste. Don't forget that the rich edit control keyboard interface provides other keys that perform copy/cut/paste operations. Refer to rich-edit-shortcut-keys Following is an example WindowProc override.

    LRESULT CMyRichEditCtrl::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)  
    {  
     // TODO: Add your specialized code here and/or call the base class  
     LRESULT lResult;  
      
     switch (message)  
     {  
     case WM_CUT:  
     case WM_COPY:  
     lResult = CRichEditCtrl::WindowProc(message, wParam, lParam);  
     m_ClipboardSequenceNumber = ::GetClipboardSequenceNumber();  
     break;  
     case WM_PASTE:  
     if (m_ClipboardSequenceNumber == ::GetClipboardSequenceNumber())  
     lResult = CRichEditCtrl::WindowProc(message, wParam, lParam);  
     else  
     lResult = 0;  
     break;  
     default:  
     lResult = CRichEditCtrl::WindowProc(message, wParam, lParam);  
     }  
      
     return lResult;  
    }  
      
    
    1 person found this answer helpful.
    0 comments No comments

  3. Sheng Jiang 蒋晟 206 Reputation points
    2020-09-08T19:23:07.73+00:00

    IRichEditOleCallback::GetClipboardData gives you the opportunity to supply your own clipboard data object when copying from RichEdit. You can implement it in such a way that your clipboard data object exposes a private clipboard format in addition to those exposed by the data object returned by IRichEditOleCallback::GetClipboardData. Or if you want to block other applications from copying your content altogether, you can return a data object that only contains your private clipboard format and do the pasting yourself (See the WordPad sample in MFC samples for manual pasting) after decoding your content to original formats.

    IRichEditOleCallback::QueryAcceptData gives you a way to refuse content being pasted/dropped to your control. You =r implementation can return an error code when your private clipboard format is not available (COleDataObject::IsDataAvailable) in the data object.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.