Hook windows control messages through subclassing

d_arin100's Avatar author of Hook windows control messages through subclassing
This is an article on Hook windows control messages through subclassing in Win32.
Rated 5.00 By 1 users
Subclassing is a very powerful tool in creating your own custom controls and modifying the standard windows interface. This is typically done by replacing the Window Procedure for a window with application-defined window procedure. Subclassing is a technique that allows an application to intercept messages destined for another window. This article contains subclassing in SDK as well as subclassing in MFC programs.

SDK subclassing

Sometimes we need to modify the functionality of control and customize it slightly depending on the application. Let we need a letter only edit control (A control which take only letter no numeric & other special character.). Here subclassing is a rescuer for resolving it. Subclassing involves replacing the Message Handlers of the control, and passing any unprocessed message to the controls Message Handler. By using SetWindowLong() we can specify a new Window Procedure for the control which needs to be subclass.

Code: Cpp
WNDPROC g_pOldProc;
int WINAPI WinMain(HINSTANCE hInstance,   HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, MyDialogProc, 0);
    return TRUE;
BOOL CALLBACK MyDialogProc(HWND hWndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
      HWND hMyEdit;
      switch (uMsg)
         case WM_INITDIALOG:
               hMyEdit = GetDlgItem(hWndDlg, IDC_EDIT);
               /// Subclass the Edit control
               g_pOldProc = (WNDPROC)SetWindowLong(hMyEdit, GWL_WNDPROC, (LONG)MyEditProc);
               return TRUE;
         case WM_COMMAND:
                      case IDOK:
                                EndDialog(hWndDlg, LOWORD(wParam));
                 return TRUE;
      return FALSE;
LRESULT CALLBACK MyEditProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
      TCHAR chChar;
      switch (message)
          case WM_CHAR:
                    chChar = (TCHAR) wParam;
                    /// Is the char is an alphabetic character
                              return 0;
      return CallWindowProc (g_pOldProc, hwnd, message, wParam, lParam);
Now any message to the edit control will first go through the "[COLOR=#990000]MyEditProc" Window Procedure, which will decide whether the message will go or not to the edit control's Window Procedure.

MFC Subclassing

Subclassing in a MFC program is similar to SDK subclassing. MFC Subclassing also done by replacing the message handlers of a control. First we need to inherit the class from a control class.
Code: Cpp
class CLetterEdit : public CEdit
         virtual ~CLetterEdit();
      afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
Then we need to add Message Handlers of the new class.
Code: Cpp
void CLetterEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
            /// The control's message handler has been called
            CEdit::OnChar(nChar, nRepCnt, nFlags);
Next we need to associate the window with an instance of our new class.
Code: Cpp
CLetterEdit m_edit;
DDX_Control(pDX, IDC_EDIT, m_edit);
shabbir like this
chyssa's Avatar
Newbie Member
Thanks for sharing your codes...
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Nominate this article for Article of the month - Oct 2009