Go4Expert (http://www.go4expert.com/)
-   Win32 (http://www.go4expert.com/articles/win32-tutorials/)
-   -   Hook windows control messages through subclassing (http://www.go4expert.com/articles/hook-windows-control-messages-t19754/)

d_arin100 13Oct2009 11:54

Hook windows control messages through subclassing
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);

chyssa 23Oct2009 16:56

Re: Hook windows control messages through subclassing
Thanks for sharing your codes...

shabbir 1Nov2009 19:50

Re: Hook windows control messages through subclassing
Nominate this article for Article of the month - Oct 2009

All times are GMT +5.5. The time now is 16:07.