开发者

Intercept pasting to a (rich) edit control

I want to override the default behavior when text is pasted into a rich edit control. Specifically, I wa开发者_运维知识库nt 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!


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 ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜