请问各位大神,mfc richedit 怎么强制将粘贴的内容变成不带格式的纯文本格式粘贴?

我的头文件:

 #pragma once


// CMyRichEditCtrl

class CMyRichEditCtrl : public CRichEditCtrl
{
    DECLARE_DYNAMIC(CMyRichEditCtrl)

public:
    CMyRichEditCtrl();
    virtual ~CMyRichEditCtrl();

    void SetBKGColor(COLORREF clrNormal, COLORREF clrFocus);

protected:
    BOOL m_bFocus;
    COLORREF m_clrFocus;
    COLORREF m_clrNormal;

    DECLARE_MESSAGE_MAP()

    virtual BOOL PreTranslateMessage(MSG* pMsg);
public:
    afx_msg void OnSize(UINT nType, int cx, int cy);
    afx_msg void OnEnKillfocus();
    afx_msg void OnEnSetfocus();
    afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnEnProtected(NMHDR *pNMHDR, LRESULT *pResult);
protected:
    virtual void PreSubclassWindow();
};

我的实现:

 // MyRichEditCtrl.cpp : 实现文件
//

#include "stdafx.h"
#include "MyRichEditCtrl.h"


// CMyRichEditCtrl

IMPLEMENT_DYNAMIC(CMyRichEditCtrl, CRichEditCtrl)


CMyRichEditCtrl::CMyRichEditCtrl()
{
    m_bFocus = FALSE;
    m_clrFocus = 0x001D170C;
    m_clrNormal = 0x001F190E;
}

CMyRichEditCtrl::~CMyRichEditCtrl()
{
}

BEGIN_MESSAGE_MAP(CMyRichEditCtrl, CRichEditCtrl)
    ON_CONTROL_REFLECT(EN_KILLFOCUS, &CMyRichEditCtrl::OnEnKillfocus)
    ON_CONTROL_REFLECT(EN_SETFOCUS, &CMyRichEditCtrl::OnEnSetfocus)
    ON_WM_MOUSEWHEEL()
    ON_WM_CREATE()
    ON_NOTIFY_REFLECT(EN_PROTECTED, &CMyRichEditCtrl::OnEnProtected)
END_MESSAGE_MAP()

// CMyRichEditCtrl 消息处理程序

void CMyRichEditCtrl::OnEnKillfocus()
{
    // TODO: 在此添加控件通知处理程序代码
    m_bFocus = FALSE;
    SetBackgroundColor(FALSE, m_clrNormal);
    Invalidate();
}

void CMyRichEditCtrl::OnEnSetfocus()
{
    // TODO: 在此添加控件通知处理程序代码
    m_bFocus = TRUE;
    SetBackgroundColor(FALSE, m_clrFocus);
    Invalidate();
}

BOOL CMyRichEditCtrl::PreTranslateMessage(MSG* pMsg)
{
    if(WM_KEYDOWN == pMsg->message)   
    {
        if(::GetFocus() == m_hWnd && ((GetKeyState(VK_CONTROL) & 0xFF00 ) == 0xFF00))   
        {  
            // 粘贴
            if( pMsg->wParam == 'V' || pMsg->wParam == 'v')  
            {
                this->PasteSpecial(CF_TEXT);
                return TRUE;
            }
        }
    } 
    return CRichEditCtrl::PreTranslateMessage(pMsg);
}
BOOL CMyRichEditCtrl::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    if (nFlags & MK_CONTROL)
    {
        return TRUE;
    }
    return CRichEditCtrl::OnMouseWheel(nFlags, zDelta, pt);
}

void CMyRichEditCtrl::SetBKGColor(COLORREF clrNormal, COLORREF clrFocus)
{
    m_clrNormal = clrNormal;
    m_clrFocus  = clrFocus;
    if (GetSafeHwnd())
    {
        SetBackgroundColor(FALSE, m_bFocus ? m_clrFocus : m_clrNormal);
    }
}

int CMyRichEditCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CRichEditCtrl::OnCreate(lpCreateStruct) == -1)
        return -1;

    // TODO:  在此添加您专用的创建代码
    SetBackgroundColor(FALSE, m_clrNormal);
    return 0;
}

void CMyRichEditCtrl::OnEnProtected(NMHDR *pNMHDR, LRESULT *pResult)
{
    ENPROTECTED *pEnProtected = reinterpret_cast<ENPROTECTED *>(pNMHDR);
    // TODO:  控件将不发送此通知,除非您重写
    // CRichEditCtrl::OnInitDialog() 函数,以将 EM_SETEVENTMASK 消息发送
    // 到该控件,同时将 ENM_PROTECTED标志“或”运算到 lParam 掩码中。

    // TODO:  在此添加控件通知处理程序代码
    switch (pEnProtected->msg)
    {
    case WM_KEYDOWN://按键,判断pEP->wParam
        {
        }
        break;
    case WM_PASTE://粘贴
        {
        }
        break;
    case WM_CUT://剪切
        {
        }
        break;
    case EM_SETCHARFORMAT:
        {
        }
        break;
    default:
        break;
    };

    *pResult = TRUE;
}

void CMyRichEditCtrl::PreSubclassWindow()
{
    // TODO: 在此添加专用代码和/或调用基类

    DWORD eventmask = GetEventMask() | ENM_PROTECTED;
    SetEventMask( eventmask );

    CRichEditCtrl::PreSubclassWindow();
}

我在PreTranslateMessage中截获Ctrl+V,但是好像不太靠谱,还是这种截获的方式不对,结果是我一直按着ctrl+v一直粘贴就会出现把原有格式粘贴上去的情况。在网上看到有帖子说使用EN_PROTECTED 反射消息,但是我怎么都接不到这个消息又是为何?已经浪费了太多时间,还请高手来帮忙解决困扰,不胜感激。

4个回答

GetClipboardData(CF_TEXT);
按理说指定了CF_TEXT就是纯文本了。你把wm_paste消息吃掉,避免RichEdit内置的粘贴起作用。

u010300403
Coder.Z 接不到WM_PASTE消息。
5 年多之前 回复

不可能啊,用spy++看

u010300403
Coder.Z 没有,听说可以通过EN_PROTECTED处理,但是EN_PROTECTED也接不到。
5 年多之前 回复

The EN_PROTECTED message notifies a rich edit control's parent window that the user is taking an action that would change a protected range of text. A rich edit control sends this notification message in the form of a WM_NOTIFY message.

To receive EN_PROTECTED notifications, specify ENM_PROTECTED in the mask sent with the EM_SETEVENTMASK message.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐