life ideas

subclass control

SDK fashion

WNDPROC g_OldEdit;

LRESULT CALLBACK NewEditProc (HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)

TCHAR chCharCode;
switch (message)
case WM_CHAR:
chCharCode = (TCHAR) wParam;
if(chCharCode > 0x20 && !IsCharAlpha(chCharCode))
return 0;
return CallWindowProc (g_OldEdit, hwnd, message, wParam, lParam);

hwnd hWndEdit = GetDlgItem(hDlg, IDC_EDIT1);
//Replace the Window Procedure and Store the Old Window Procedure
g_OldEdit = (WNDPROC)SetWindowLong(hWndEdit, GWL_WNDPROC, (LONG)NewEditProc);

Using DDX to subclass a window at creation time by MFC class wizard
just declare a variable of CMyButton in class wizard

f you have added a window class to your project and want to subclass a window with an
object of this new class’ type, but the ClassWizard isn’t offering you that new object’s
type as an option, then you may need to rebuild the class wizard file.

Make a backup of your projects .clw file, delete the original file, then go into Visual
Studio and hit Ctrl+W. You will then be prompted for which files you want to have included
in the class scan. Ensure that the new class files are included!Your new class should now
be available as an option.

or you can just use the classwizard to declare a variable of CButton and then go into the
header file manually and change this to the class CMyButton.

Subclassing an existing window
Using DDX is simple, but doesn’t help us if we need to subclass a control that already exists.
For instance, say you want to subclass the Edit control in a combobox. You need to have the
combobox (and hence it’s child edit window) already created before you can subclass the edit

In this case you make use of the handy SubclassDlgItem or SubclassWindow functions. These two
functions allow you to dynamically subclass a window – in other words, attach an object of
your new window class type to an existing window.

For example, suppose we have a dialog containing a button with ID IDC_BUTTON1. That button
has already been created and we want to associate that button with an object of type CMyButton
so that the button behaves in the manner we want.

To do this we NEED to have an object of our new type already created. A member variable of your dialog or view class is perfect.

CMyButton m_btnMyButton;

1)Then call in your dialog’s OnInitDialog (or whereever is appropriate) call

m_btnMyButton.SubclassDlgItem(IDC_BUTTON1, this);//this ->is dialog itself

2)Alternatively suppose you already have a pointer to a window you wish to subclass, or
you are working within a CView or other CWnd derived class where the controls are created
dynamically or you dont’t wish to use SubclassDlgItem. Simply call

CWnd* pWnd = GetDlgItem(IDC_BUTTON1); // or use some other method to get
// a pointer to the window you wish
// to subclass
ASSERT( pWnd && pWnd->GetSafeHwnd() );

When you subclass a control, besides handling the message it receives, in MFC you
can also handle the notifications it sends to it’s parent window. This technique is
called Message Reflecting.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

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

Blog at

%d bloggers like this: