How does a CRichEditCtrl know a paste operation has been performed?

人走茶凉 提交于 2019-11-29 11:48:07

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
}

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.

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 :-)

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.

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);    
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!