How to make a read only Edit box

6 01 2009




We are familiar with Edit box control, a rectangular child window in which the user can enter text. But some times we need to make a read only Edit box that means non-writable.








This could achieve by overriding the PreTranslateMessage function on our CDialog derived class  and thus we can easily process on messages sent to our window before they are hand over to child windows. In our particular case we need to prevent the WM_KEYDOWN message write anything to our particular Edit box.

Return value of the PreTranslate function determines whether the particular message need to be dispatched or not. Nonzero if the message was translated and should not be dispatched; 0 if the message was not translated and should be dispatched.




BOOL CMyDialog:: PreTranslateMessage(MSG* pMsg)
   //m_MyEditBox - read only edit box
   if(m_MyEditBox.m_hWnd == pMsg->hwnd)
      if(pMsg->message == WM_KEYDOWN)
         //Nullifying the WM_KEYDOWN message
         pMsg->wParam=NULL ;
   return CDialog:: PreTranslateMessage(pMsg);




All windows message could be nullified within the PreTranslateMessage function.




2 responses

15 07 2011

erm why would you need to lock and edit box, well then again it could be easier to read it from. ok when you go to place the edit box etc.

soruce code

you have to add ES_READONLY to your create window code(if your using win 32) that should then lock that edit box for input

15 07 2011
Sanoop S P

Hello Jake,

Thanks for your comment. Definitely ES_READONLY makes edit box to read only.
This is an alternate way to do the same without changing the default look of the edit box.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: