How does a CRichEditCtrl know a paste operation ha

2019-01-26 20:41发布

It has methods like CRichEditCtrl::Copy(), CRichEditCtrl::Paste() which you can call, but I can't spot any messages the control is sent by Windows telling it to perform a paste operation. Does anyone know if such a thing exists? Or does CRichEditCtrl do something lower-level like monitoring WM_CHAR events? If so can I reuse any internal methods or would I just have to roll my own in order to override the standard paste functionality?

What I actually want is for my custom subclass (CMyRichEditCtrl : CRichEditCtrl) to ignore any formatting on text pasted in to the control. Either by getting the clipboard data in a different clipboard format, or by pasting it in as normal and immediately removing formatting on inserted text.

What I tried so far:

  1. Checking the message for WM_PASTE in CMyRichEditCtrl::PreTranslateMessage()
  2. Creating a method virtual void CMyRichEditCtrl::Paste()
  3. Putting a breakpoint on CRichEditCtrl::Paste() in afxcmn.inl
  4. Dumping every message passing through CMyRichEditCtrl::PreTranslateMessage()

Results:

1: No WM_PASTE message seen
2: It's never called
3: It's never hit... how?
4: The control never receives any WM_COMMAND, WM_PASTE or focus-related messages. Basically only mouse-move and key-press messages.

It seems other people have actually done this successfully. I'm wondering if my MFC version or something could be screwing it up, at this point.

5条回答
该账号已被封号
2楼-- · 2019-01-26 21:03

Handle EN_PROTECTED message.

ON_NOTIFY_REFLECT(EN_PROTECTED, &YourClass::OnProtected)

// call this from the parent class
void YourClass::Initialize()
{
    CHARFORMAT format = { sizeof(CHARFORMAT) };
    format.dwEffects = CFE_PROTECTED; 
    format.dwMask = CFM_PROTECTED;

    SetDefaultCharFormat(format);
    SetEventMask(ENM_PROTECTED);
}

void YourClass::OnProtected(NMHDR* pNMHDR, LRESULT* pResult)
{
    *pResult = 0; 

    ENPROTECTED* pEP = (ENPROTECTED*)pNMHDR;
    if (pEP->msg == WM_PASTE)
        pResult = 1; // prevent paste
}
查看更多
等我变得足够好
3楼-- · 2019-01-26 21:17

Use the ON_MESSAGE Macro on your derived class.

ON_MESSAGE(WM_PASTE, OnPaste)

LRESULT CMyRichEditCtrl::OnPaste(WPARAM, LPARAM)

If you open the RichEdit.h file, you will notice that some of the messages are on the range of WM_USER. Maybe this is how MFC handles the events for the Rich Edit Control.

查看更多
叼着烟拽天下
4楼-- · 2019-01-26 21:19

i have to perform like below

void MyRichEcit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{


if( ( GetKeyState(VK_CONTROL)<0 && nChar==88 ) || (nChar==VK_DELETE && GetKeyState(VK_SHIFT) < 0) ) //cut
    {

    }

if( ( GetKeyState(VK_CONTROL)<0 && nChar==86 ) || (nChar==VK_INSERT && GetKeyState(VK_SHIFT) < 0) ) //paste
    {

    }


    CWnd::OnKeyDown(nChar, nRepCnt, nFlags);    
}
查看更多
\"骚年 ilove
5楼-- · 2019-01-26 21:26

Windows defines messages for cut/copy/and paste. see WM_CUT.

It's probably responding to those messages rather than to WM_CHAR messages to know when to do clipboard operations.

查看更多
SAY GOODBYE
6楼-- · 2019-01-26 21:26

What happens when the user requests a paste action is usually that a WM_COMMAND message with the identifier ID_EDIT_PASTE is sent to the rich edit control. By default in MFC this is handled by CRichEditCtrl::OnEditPaste(), which calls Paste() on the edit control itself.

The way I'd go about this is to derive a class from CRichEditCtrl, add an OnEditPaste method and route the message to it with a

ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)

declaration, which should work. Alternatively, in your PreTranslateMessage you could look for WM_COMMAND with a wParam of ID_EDIT_PASTE.

By the way, I've solved a very similar problem to yours (paste without formatting) by just having an implementation of OnEditPaste with

void MyRichEdit::OnEditPaste()
{
  SendMessage(EM_PASTESPECIAL,CF_UNICODETEXT);
}

This responds to the paste request by sending a paste message to the control that insists that the data format is plain text.

Finally, I should point out that the above technique is sufficient to catch all pastes triggered from the user interface. However, it won't catch programmatically triggered pastes, when your code sends WM_PASTE to the edit control. In those cases it's easiest to just change your code. However, if you really want to intercept such cases, you have to get your hands dirty with COM and IRichEditOleCallback::QueryAcceptData. But you almost certainly don't want to go there :-)

查看更多
登录 后发表回答