Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Override Paste menu in CEdit

298 views
Skip to first unread message

Jonathan Arnold

unread,
Jun 1, 2004, 12:53:13 PM6/1/04
to
How do I override the Paste command in a CEdit control? Basically
I want to look at the clipboard and if it is a certain format,
paste it in a special way, otherwise I just want to call the normal
paste. I have subclassed the CEdit, and tried to hook it in place
of the regular CEdit, but my OnEditPaste method doesn't seem to be
getting called.

Jonathan Arnold

Jeff Partch

unread,
Jun 1, 2004, 1:05:39 PM6/1/04
to
"Jonathan Arnold" <jdarnold_online@insors_comm.com> wrote in message
news:%23czgzh$REHA...@TK2MSFTNGP12.phx.gbl...

Is your 'OnEditPaste method' a handler for the WM_PASTE message?
--
Jeff Partch [VC++ MVP]


Jonathan Arnold

unread,
Jun 1, 2004, 2:09:44 PM6/1/04
to
Jeff Partch wrote:

Actually, it is a handler for the ON_COMMAND ID_EDIT_PASTE message from
the menu. How would I add an OnEditPaste handler for the WM_PASTE
message?

Jonathan Arnold

Jeff Partch

unread,
Jun 1, 2004, 2:40:34 PM6/1/04
to
"Jonathan Arnold" <jdarnold_online@insors_comm.com> wrote in message
news:u9dDkMAS...@TK2MSFTNGP10.phx.gbl...

Oh, so this menu is like the Edit menu from your apps top-level menu and not
the edit's context menu? I'm not certain, but I don't think the command
message routing will get the message to your edit unless you override it to
do so. Anyway, I still think WM_PASTE is what you're after. If the wizard
won't help you then you'll have to add it by hand.

Jonathan Arnold

unread,
Jun 1, 2004, 3:05:26 PM6/1/04
to
Jeff Partch wrote:

> "Jonathan Arnold" <jdarnold_online@insors_comm.com> wrote in message
> news:u9dDkMAS...@TK2MSFTNGP10.phx.gbl...
>
>>Jeff Partch wrote:
>>
>>
>>>"Jonathan Arnold" <jdarnold_online@insors_comm.com> wrote in message
>>>news:%23czgzh$REHA...@TK2MSFTNGP12.phx.gbl...
>>>
>>>
>>>>How do I override the Paste command in a CEdit control? Basically
>>>>I want to look at the clipboard and if it is a certain format,
>>>>paste it in a special way, otherwise I just want to call the normal
>>>>paste. I have subclassed the CEdit, and tried to hook it in place
>>>>of the regular CEdit, but my OnEditPaste method doesn't seem to be
>>>>getting called.
>>>
>>>
>>>Is your 'OnEditPaste method' a handler for the WM_PASTE message?
>>
>>Actually, it is a handler for the ON_COMMAND ID_EDIT_PASTE message from
>>the menu. How would I add an OnEditPaste handler for the WM_PASTE
>>message?
>
> Oh, so this menu is like the Edit menu from your apps top-level menu and not
> the edit's context menu? I'm not certain, but I don't think the command
> message routing will get the message to your edit unless you override it to
> do so. Anyway, I still think WM_PASTE is what you're after. If the wizard
> won't help you then you'll have to add it by hand.

No, actually I *am* trying to override the context menu's Edit item. But I
think you put me on the right track. Here's what I did:

class CMyEdit : public CEdit
{
public:
CMyEdit() : l{};

DECLARE_DYNAMIC(CMyEdit)

// Implementation.
protected:
//{{AFX_MSG(CMyEdit)
afx_msg void OnPaste();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

Implementation:

IMPLEMENT_DYNAMIC(CMyEdit, CEdit)

BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
//{{AFX_MSG_MAP(MyEdit)
ON_MESSAGE(WM_PASTE, OnPaste)
//}}AFX_MSG_MAP
END_MESSAGE_MAP() ;

// Get my parent, and try to do a paste
void CMyEdit::OnPaste()
{
... Do my special paste in here
}

Does this look about right? It seems to work. And then in my dialog, I added:

CMyEdit m_ctlKey1;

to the data section. Is there some other way I'm supposed to tie my control
into the dialog?


Jonathan Arnold

Jeff Partch

unread,
Jun 1, 2004, 3:29:25 PM6/1/04
to
"Jonathan Arnold" <jdarnold_online@insors_comm.com> wrote in message
news:uxVwrrAS...@TK2MSFTNGP09.phx.gbl...

> No, actually I *am* trying to override the context menu's Edit item. But I
> think you put me on the right track. Here's what I did:
>
> class CMyEdit : public CEdit
> {
> public:
> CMyEdit() : l{};
>
> DECLARE_DYNAMIC(CMyEdit)
>
> // Implementation.
> protected:
> //{{AFX_MSG(CMyEdit)
> afx_msg void OnPaste();
> //}}AFX_MSG
> DECLARE_MESSAGE_MAP()
> };
>
> Implementation:
>
> IMPLEMENT_DYNAMIC(CMyEdit, CEdit)
>
> BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
> //{{AFX_MSG_MAP(MyEdit)
> ON_MESSAGE(WM_PASTE, OnPaste)
> //}}AFX_MSG_MAP
> END_MESSAGE_MAP() ;
>
> // Get my parent, and try to do a paste
> void CMyEdit::OnPaste()
> {
> ... Do my special paste in here
> }

The ON_MESSAGE handler needs to have the prototype: LRESULT OnPaste(WPARAM,
LPARAM), and you should probably move all manual entries outside of the
ClassWizard AFX_xxx blocks.

Joseph M. Newcomer

unread,
Jun 1, 2004, 10:02:28 PM6/1/04
to
Actually, if you type Ctrl+V to an edit control, I believe it is intercepted and converted
to a WM_PASTE message. The ID_EDIT_PASTE command is converted to that, because an edit
control has no ID_EDIT_PASTE handler built in. So I'd suggest going back to the original
suggestion of intercepting WM_PASTE itself.
joe

On Tue, 01 Jun 2004 15:05:26 -0400, Jonathan Arnold <jdarnold_online@insors_comm.com>
wrote:

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Gary Chang

unread,
Jun 2, 2004, 2:14:04 AM6/2/04
to
Hi Jonathan,

To intercept WM_PASTE message of your derived CEdit class, you can override
its WindowProc function:
class CMyEdit : public CEdit
{
..
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyEdit)
protected:
virtual LRESULT WindowProc(UINT message, WPARAM wParam,LPARAM lParam);
//}}AFX_VIRTUAL
..
};

LRESULT CMyEdit::WindowProc(UINT message, WPARAM wParam,LPARAM lParam)
{
if(message == WM_PASTE){
//Do your specific processing here
//...
//return 0; you can ignore the default normal paste behavior
}

return CEdit::WindowProc(message, wParam, lParam);
}


>Is there some other way I'm supposed to tie my control
> into the dialog?

Drag drop a Edit Box control to your Form/Dialog in the Resource editor,
then use the Class Wizard\member Variable to attach the Edit Box control to
your derived CMyEdit class(Select the Control ID of that Edit control,
click Add Variable..., choose the Control in the Catagory listbox, select
CMyEdit in the Variable Type listbox...)


Thanks!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------

Jonathan Arnold

unread,
Jun 2, 2004, 11:35:23 AM6/2/04
to
Joseph M. Newcomer wrote:

> Actually, if you type Ctrl+V to an edit control, I believe it is intercepted and converted
> to a WM_PASTE message. The ID_EDIT_PASTE command is converted to that, because an edit
> control has no ID_EDIT_PASTE handler built in. So I'd suggest going back to the original
> suggestion of intercepting WM_PASTE itself.

Yes, that is what I did. I intercept the WM_PASTE message so I get both
Ctrl-V and the menu item. It does have the problem that I couldn't figure
out how to do a "normal" paste if the specialized data I was looking for
wasn't in the Clipboard, so I ended up covering all bases. Calling the
Edit controls Paste method, or sending it a WM_PASTE message, both just
ended up calling my Paste method again, not surprisingly.

Thanks to Jeff's suggestions, I cleaned up the code as he said and all looks
well.

Jonathan Arnold

Jeff Partch [MVP]

unread,
Jun 2, 2004, 12:24:55 PM6/2/04
to
"Jonathan Arnold" <jdarnold_online@insors_comm.com> wrote in message
news:u87L9aLS...@TK2MSFTNGP09.phx.gbl...

> Yes, that is what I did. I intercept the WM_PASTE message so I get both
> Ctrl-V and the menu item. It does have the problem that I couldn't figure
> out how to do a "normal" paste if the specialized data I was looking for
> wasn't in the Clipboard,

In a message handler I think you can just call Default() when you want the
control do do its usual thing.

Joseph M. Newcomer

unread,
Jun 2, 2004, 12:47:40 PM6/2/04
to
Why not something much simpler:

Override the CEdit. Add to the message map
ON_MESSAGE(WM_PASTE, OnPaste)

which is a lot more sensible than overriding WindowProc. This should only be done in
extremely exotic situations, of which this is not one.
joe

Joseph M. Newcomer [MVP]

0 new messages