The MFC CEdit class provides no simple way to prevent user from copying the contents of the corresponding Edit Control or pasting text into it or getting a context menu. Using subclassing and handling the WM_COPY
, WM_PASTE
, WM_CONTEXTMENU
these operations can be disabled in a very simple way.
The code sample is very short and self explanatory. Most of the code is AppWizard generated. CPDEdit.* are the 2 main files of interest.
CPDEdit.h
-----------
class CPDEdit: public CEdit
{
public:
DECLARE_MESSAGE_MAP()
afx_msg LRESULT OnCopy(WPARAM, LPARAM);
afx_msg LRESULT OnPaste(WPARAM, LPARAM);
afx_msg LRESULT OnContextMenu(WPARAM, LPARAM);
};
CPDEdit.cpp
-----------
BEGIN_MESSAGE_MAP(CPDEdit, CEdit)
ON_MESSAGE(WM_COPY, OnCopy)
ON_MESSAGE(WM_PASTE, OnPaste)
ON_MESSAGE(WM_CONTEXTMENU, OnContextMenu)
END_MESSAGE_MAP()
LRESULT CPDEdit::OnCopy(WPARAM, LPARAM) {
return 0;
}
LRESULT CPDEdit::OnPaste(WPARAM, LPARAM) {
return 0;
}
LRESULT CPDEdit::OnContextMenu(WPARAM, LPARAM) {
return 0;
}
CPDEdit
is the class derived from CEdit
used to subclass the edit control. It traps the WM_COPY
, WM_PASTE
and WM_CONTEXTMENU
messages and simply ignores them.
Within your Dialog class change the type of control from CEdit
to CPDEdit
:
CPDEdit m_edit;
And in the InitDialog()
function subclass the control:
m_edit.SubclassDlgItem(IDC_EDIT, this);
Note that if you are using DDX with a control type then you don't need to call SubclassDlgItem
because DDX internally calls it.