Click to See Complete Forum and Search --> : how to disable paste in CEdit control
Skuggi
May 10th, 1999, 09:21 PM
I have been trying to disable paste in edit controls and to disable the default popup menu.
could anyone help me please. I have to do this before tomorrow...
eric33
May 11th, 1999, 02:07 AM
I try something in a small sample and it seems to work.
Use the PreTranslate handler of the text box's window container.
This method is the simpliest one. You can too make your own CEdit control and then make a new PreTranslate handler in the control.
Here is a sample using the PreTranslate handler of the container window.
BOOL CMyWindow::PreTranslateMessage(MSG* pMsg)
{
// If the text box have the focus ..
if ( GetFocus()==&m_mycontrol )
{
// If CTRL+V or Shift+Insert
if ( pMsg->message == WM_KEYDOWN &&
( (pMsg->wParam == 'V' && (GetKeyState(VK_CONTROL) & ~1) ) ||
(pMsg->wParam == VK_INSERT && (GetKeyState(VK_SHIFT) & ~1) )
)
)
{
// Do not dispatch the message
return TRUE;
}
}
// Call the default handler
return CDialog::PreTranslateMessage(pMsg);
}
sally
May 11th, 1999, 02:21 AM
and if I right mouse click and do it via the paste command
I think the best way is to handle the paste command itself in a subclassed edit control
Sally
Sally
May 11th, 1999, 02:21 AM
and if I right mouse click and do it via the paste command
I think the best way is to handle the paste command itself in a subclassed edit control
Sally
eric33
May 11th, 1999, 02:32 AM
Ok you're right.
Yes you're right.
In this case subclass your own edit control and use the PreTranslate handler to not dispatch the WM_PASTE message.
I don't try this so you have to test it.
BOOL CMyEditControl::PreTranslateMessage(MSG* pMsg)
{
if ( pMsg->message == WM_PASTE )
// Do not dispatch the message
return TRUE;
return CEdit::PreTranslateMessage(pMsg);
}
Dan Haddix
May 11th, 1999, 02:33 AM
No problem, just follow these steps!!! First you will need to create your own class derived from CEdit, lets call it CNoPasteEdit, like so...
class CNoPasteEdit: public CEdit
{
public:
CNoPasteEdit();
~CNoPasteEdit();
protected:
// This line will need to be added by hand because WM_PASTE is not available in
// class wizard
afx_msg void OnPaste(WPARAM wParam, LPARAM lParam);
afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
DECLARE_MESSAGE_MAP()
};
Then you will need to edit the .cpp file for this class like so
CNoPasteEdit::CNoPasteEdit(){
// Put any construction code here
}
CNoPasteEdit:~:CNoPasteEdit(){
// Put any destruction code here
}
BEGIN_MESSAGE_MAP(CNoPasteEdit, CEdit)
// This line is needed because there is no default macro for WM_PASTE messages
// This line will also need to be added by hand
ON_MESSAGE(WM_PASTE, OnPaste)
ON_WM_CONTEXTMENU()
END_MESSAGE_MAP()
void CNoPasteEdit::OnPaste(WPARAM wParam, LPARAM lParam){
// Put any code here you want to execute when the user right clicks on the edit
// control. Just leave it blank to disable the menu
}
void CNoPasteEdit::OnContextMenu(CWnd* pWnd, CPoint point){
// Put any code here you want to execute when the user tries to paste into the edit
// conrtol. Just leave it blank to prevent pasting.
}
After creating this class you will need to go into class wizard and select your dialog class from the drop down list. Then switch over to the member variables tab, and double click the ID for the edit control you don't want to be able to paste. In the dialog that appears use whatever you want for the member name, just be sure to select CNoPasteEdit as the Variable Type. This sould be it. The edit control will no longer have a right click context menu or except pasting via Ctrl+V.
Aaron Maddux
August 4th, 1999, 02:33 PM
Is there some reason that the same code does not work to catch the WM_PASTE message inside a CRichEditCntrl? I have pasted the code into my derived CRichEditCntrl, but when I hit cntrl-v, I do not catch the WM_PASTE message in OnPaste. Any suggestions would be appreciated.
scarleton
March 16th, 2004, 03:39 PM
I need to disable cut/paste sometime and other times allow it. If I understand this all correctly, all I need to do to disable the cut/paste is to do nothing when OnPaste/OnContextMenu. What should I do to allow them?
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.