question

flaviu avatar image
0 Votes"
flaviu asked DavidLowndes-6766 commented

CRichEditCtrl in CDockablePane

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++
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Sorry, I forgot to attach the sample project, I'll do it in few moments.

0 Votes 0 ·
JeanineZhang-MSFT avatar image
0 Votes"
JeanineZhang-MSFT answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DavidLowndes-6766 avatar image
0 Votes"
DavidLowndes-6766 answered flaviu commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It is about CClassView which is derived from CDockablePane. It's about a pane from that test application, not CView from framework:

 class CClassView : public CDockablePane
 {
 public:
     CClassView() noexcept;
     virtual ~CClassView();
    
     void AdjustLayout();
     CRichEditCtrl* GetRichEditCtrl() { return &m_wndRichEditCtrl; }
    
 protected:
     CRichEditCtrl m_wndRichEditCtrl;
    
 // Overrides
 public:
    
 protected:
     afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
     afx_msg void OnSize(UINT nType, int cx, int cy);
     afx_msg void OnClose();
     afx_msg void OnSetFocus(CWnd* pOldWnd);
     afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
     DECLARE_MESSAGE_MAP()
 };


109510-image-2021-06-27-094144.png


0 Votes 0 ·
JeanineZhang-MSFT avatar image
0 Votes"
JeanineZhang-MSFT answered DavidLowndes-6766 commented

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.



image.png (35.1 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Have you tried on CDockable pane to do that ? Randomly, I derived CView from CEditView, but it is about CDockablePane which has a CRichEditCtrl memeber stretched on whole pane, see the code above.

0 Votes 0 ·

OK, with that clarified I can reproduce what you say.

If I re-assign the application's Ctrl+V accelerator to something else, then Ctrl+V works in your child rich edit control, so presumably you're just falling foul of MFCs command routing in that example.

0 Votes 0 ·
flaviu avatar image
0 Votes"
flaviu answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

flaviu avatar image
0 Votes"
flaviu answered DavidLowndes-6766 commented

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 ?

· 11
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The documentation notes the general principles; I'm rather rusty in such areas these days, but it's usually best to go with the flow, so rather than subvert the flow, I'd probably try to ensure that whatever I'm doing fitted into the general scheme by trying to have the panel with your rich edit control be the active view. See this also.


0 Votes 0 ·
flaviu avatar image flaviu DavidLowndes-6766 ·

Thank you a lot for your guidelines. I have tried as you said, but I guess I am failed:

 BOOL CBBBView::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
 {
     // TODO: Add your specialized code here and/or call the base class
    
     CMainFrame* pFrame = static_cast<CMainFrame*>(AfxGetMainWnd());
    
     if (pFrame->GetClassView() &&
         pFrame->GetClassView()->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
         return TRUE;
    
     return CEditView::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
 }

but the messages are still goes through CView, not on my Panel. It never returns TRUE this handler. I'll attach the sample, in few minutes.


0 Votes 0 ·

s!AtSPCxnvttzehHbKSnRd4BkOL_7g

New version of BBB original project, but which has OnCmdMsg handler in CView.


P.S. Just tell me if the link/project is not available.

0 Votes 0 ·
Show more comments
flaviu avatar image
0 Votes"
flaviu answered flaviu edited

I have left only one panel in test application, BBB, and then I've tried:

 BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
 {
  // TODO: Add your specialized code here and/or call the base class
    
  if (m_wndClassView.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
  return TRUE;
    
  return CMDIFrameWndEx::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
 }

Test here: https://1drv.ms/u/s!AtSPCxnvttzehHeYKvwdmBReEOC6?e=PyyYke


Still not working, the commands are not routing on panel.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

flaviu avatar image
0 Votes"
flaviu answered flaviu edited

I begin to believe that I am work on wrong approach. I did override OnCmdMsg on CMyEditView and I wrote:

 BOOL CBBBView::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
 {
  // TODO: Add your specialized code here and/or call the base class
    
  CMainFrame* pFrame = static_cast<CMainFrame*>(AfxGetMainWnd());
  if (pFrame->m_wndClassView.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
  return TRUE;
    
  return CEditView::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
 }

Didn't work.

I have tried to handle OnCmdMsg from CMainFrame:

  BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
  {
   // TODO: Add your specialized code here and/or call the base class
        
   if (m_wndClassView.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
   return TRUE;
        
   return CMDIFrameWndEx::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
  }

Still not working. This approach is wrong ? Why I need to add a "Shift" key for everything I do in CMyDockablePane ?

I work on this for several days, without any result. Any hint will be wonderful !

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.