|
|
|
Introduction
Have you ever wanted to disable the context menu popup of an edit control? Have you ever wanted to disallow user to copy or cut from your editbox. Have you ever wanted user to tease user by not allowing him to select some text from your long paragraph text. It will be fun.
The CSecureEdit class
The CSecureEdit class does these job by overriding CEdit function. The idea of this article is to learn how easy it would be to make your own CEdit derived class with enhanced functionality.
To prevent context menu in editbox
When user clicks right mouse button MFC calls CEdit::OnRButtonDown(nFlags, point); function which in turn generate popup menu. So it we dont want context menu in our editbox then do not call base class method. (Tip:other thing you can do is create your own menu to display your own items.) |
Click here to copy the following block | void CSecureEdit::OnRButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default return; //(block means do not call base class method:-)) //CEdit::OnRButtonDown(nFlags, point); } |
To prevent modification of your text
1.override OnChar() function it is called by framework before OnKeyUp() and after OnKeyDown(). Here also do not call base class method. |
Click here to copy the following block | void CSecureEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) { // TODO: Add your message handler code here and/or call default return;
//(block means do not call base class method:-)) //CEdit::OnChar(nChar, nRepCnt, nFlags); } |
2. override OnKeyDown() it is called by framework when nonsystem key is pressed when focus our editbox has input focus. Here also do not call base class method. (tip:other thing you can do is allow only some set of charecters like digits only.) Here i have allowed TAB key to pass focus to other control, otherwise block the call here. |
Click here to copy the following block | void CSecureEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { // only if Key Down is TAB if ( VK_TAB == nChar ) CEdit::OnKeyDown(nChar, nRepCnt, nFlags); else return; //(block means do not call base class method:-)) //CEdit::OnKeyDown(nChar, nRepCnt, nFlags); } |
To Select all text when control has input focus
override OnSetFocus() and call CEdit::SetSel() function to select all text in control. And disselect all text when input focus to our control is lost. It is done by overriding OnKillFocus(). Also when control get focus by using mouse. that is by single/double click of left button of mouse.
Known issues:
- It can also be with CStatic control but when size of control is limited in dialog then I would prefer editbox with scrollbar. - PrintScreen Key cannot secure content of your SecureEdit. ( :-)) ) - When user quickly select content of control and drag it allows to select some part. ( it is avoided by me by selecting all text again when left mouse button is released :-))
History
version 1.0 - intial release, disable right click, ctrl+c,v,x, ctrl+insert version 1.1 -dt:17-01-2005 - Select all text on setfocus/onclick and desselect on lost focus. - wrote some description. |
|
|
|
|
|