tags:

views:

64

answers:

1

I want to override the default behavior when text is pasted into a rich edit control. Specifically, I want to paste plain-text, not formatted rich-text. I guess that boils down to getting the data as a different clipboard format, but I don't know how to intercept the default behavior first.

For reference, I basically have:

class MyDialog : public CDialog
{
 CRichEditCtrl m_Edit;
};

And the relevant DDX_Control(pDX, IDC_EDIT1, m_Edit) set up in the cpp file. Since I don't think CRichEditCtrl has any facility built-in to control paste-formatting, how can I set this up most elegantly? When I Google, I find lots of people manually capturing CTRL+V, etc... which is not only horrible but won't work in languages where the paste key isn't V!

+3  A: 

You could intercept the EN_UPDATE instruction and re-format the text when you receive that? Only problem with this is that you don't know what new data has been added.

You may, though, find it easier to override the CRichEditCtrl with your own and intercept whichevere messages you want do processing on the incoming data and then call the parent class's implementation of that function. That way everytime something is added you are performing the necessary re-formats ...

Edit: To derive a class from CRichEditCtrl is pretty easy

class CMyRichEditCtrl : public CRicheditCtrl
{
    DECLARE_DYNAMIC( CMyRichEditCtrl )

protected:
    DECLARE_MESSAGE_MAP()

public:
    CMyRichEditCtrl();
    virtual ~CMyRichEditCtrl();
    // ... Rest of implementation here
};

You can then intercept messages in the message map as follows...

ON_MESSAGE( EM_PASTESPECIAL, &CMyRichEditCtrl::OnPasteSpecial )

and your handler will look like this:

LRESULT CMyRichEditCtrl::OnPasteSpecial( WPARAM wParam, LPARAM lParam )

wParam is the clipboard format and lParam contains either a NULL or a REPASTESPECIAL structure.

All you then need to do is make sure YOUR class is the one that receives all the messages and that can, easily be done, using the DoDataExchange function.

Define your member variable as:

CMyRichEditCtrl m_MyRichEditCtrl;

and add the following to DoDataExchange:

DDX_Control( pDX, IDC_MYRICHEDIT, m_MyRichEditCtrl );

All messages will now route through your implementation of the RichEditCtrl

You can intercept any message, including WM_PASTE, this way ...

Goz
On EN_UPDATE... I guess, but we do _have_ formatting in the control, we only want to ensure the user can't enter formatting.
John
On "override the CRichEditCtrl", can you go into a bit more detail perhaps? It sounds the better option, so far anyhow.
John
Awesome answer, thanks for taking the time.
John

related questions