CRichEditCtrl in CDockablePane

Flaviu_ 1,011 Reputation points
2021-06-24T17:10:05.457+00:00

I intend to put a CRichEditCtrl in a CDockablePane, and I succeeded some way, but not completelly: I can write text in this embeeded CRichEditCtrl, but I have not formating and I noticed something strange: the keyboard shortcuts is functional only if I keep Shift key down. For instance, if I want to 'Paste' something, instead of Ctrl+V, I need to press Ctrl+Shift+V, and so on.

I attached here a sample project that reveal the problem.

Here is the code for creating CRichEditCtrl inside of CDockablePane:

int CClassView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
 if (CDockablePane::OnCreate(lpCreateStruct) == -1)
 return -1;

 CRect rectDummy;
 rectDummy.SetRectEmpty();
 constexpr DWORD dwStyle = WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_WANTRETURN;
 if (! m_wndRichEditCtrl.Create(dwStyle, rectDummy, this, IDC_RICHEDIT_QUERY))
 {
 TRACE(_T("Failed to create rich edit control\n"));
 return -1;      // fail to create
 }

 m_wndRichEditCtrl.PostMessage(EM_SETEVENTMASK, 0, ENM_MOUSEEVENTS | ENM_SCROLLEVENTS | ENM_KEYEVENTS);

 return 0;
}

And something strange: if I copy something from, let say, Visual Studio, and I paste it here, the formating is not kept, as I said ... why ?

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,761 questions
{count} votes

Accepted answer
  1. Jeanine Zhang-MSFT 9,771 Reputation points Microsoft Vendor
    2021-07-09T07:52:03.677+00:00

    Hi,

    As far as I'm concerned you could try to overload BOOL PreTranslateMessage (MSG* pMsg)

    Here is my code:

    BOOL CClassView::PreTranslateMessage(MSG* pMsg)  
    {  
    	// TODO: Add your specialized code here and/or call the base class  
      
    	if (pMsg->message == WM_KEYDOWN)  
    	{  
    		int nKey = pMsg->wParam;  
    		if ((nKey == 'C' || nKey == 'X'  
    			|| nKey == 'V') &&  
    			(::GetKeyState(VK_CONTROL) & 0x8000))  
    		{  
    			::TranslateMessage(pMsg);  
    			::DispatchMessage(pMsg);  
    			return(TRUE);  
    		}  
    	}  
    	  
      
      
      
    	return CDockablePane::PreTranslateMessage(pMsg);  
    }  
      
    

    Best Regards,

    Jeanine


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

6 additional answers

Sort by: Most helpful
  1. David Lowndes 4,716 Reputation points
    2021-06-24T21:31:04.603+00:00

    Plain Ctrl+V works for me with your project.

    As for why you're not seeing rich text pasted, you appear to have created the project with a plain edit view base, rather than CRichEditView - creating a new project using that as the view base allows pasting RTF for me.


  2. Jeanine Zhang-MSFT 9,771 Reputation points Microsoft Vendor
    2021-06-25T02:29:40.733+00:00

    Hi

    Ctrl+V also works for me with your project. I suggest you could try to check if the shortcut key of Edit.Paste is Ctrl+V. In your case, it might be Ctrl+Shift+V. Try changing it to Ctrl + V.

    109243-image.png

    Best Regards,

    Jeanine


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. Flaviu_ 1,011 Reputation points
    2021-06-27T12:44:41.2+00:00

    Yes, you are right, I need to re-route those commands from the main view to the panel, when the focus is inside of this panel.

    0 comments No comments

  4. Flaviu_ 1,011 Reputation points
    2021-06-28T09:12:51.437+00:00

    How is proper way to route the MFC commands from CEditView to CMyPanel ? Is the PreTranslateMessage the correct way ?

    As far as I know, this handler should be avoided ...

    P.S.
    Even if I use this handler, how can route all MFC messages ?


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.