Knowledge Base

How to subclass a dialog item in ATL

Article ID: 183218

Article Last Modified on 5/26/2005


APPLIES TO


This article was previously published under Q183218
NOTE: Microsoft Visual C++ .NET (2002) and Microsoft Visual C++ .NET (2003) support both the managed code model that is provided by the .NET Framework and the unmanaged native Windows code model. The information in this article applies to unmanaged Visual C++ code only.

SUMMARY

This article describes how to subclass a control on the dialog box resource template using ATL.

MORE INFORMATION

You need to perform the following steps to subclass a control, such as the ComboBox, on the dialog template in an ATL project:
  1. Declare a member of type CContainedWindow in the CDialogImpl-derived class for the Dialog Template as:
          CContainedWindow   m_ComboBox;
    						
  2. Initialize the CContainedWindow member in the constructor of the CDialogImpl-derived class (CMyDialog class) with the appropriate class name as:
          CMyDialog::CMyDialog() : m_ComboBox(_T("COMBOBOX"), this, 1)
    						
  3. Add WM_INITDIALOG and WM_DESTROY message handlers, and an Altered Message Map (ALT_MSG_MAP), to the default Message Map of the CDialogImpl-derived class. The Altered Message Map is used to trap messages that are sent to the subclassed control, as in the following example:

    Sample Code

    BEGIN_MSG_MAP(CMyDialog)
       MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
       MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
    
    ALT_MSG_MAP(1)   // Handlers for the subclassed combo box.
       MESSAGE_HANDLER(WM_RBUTTONDOWN, OnRBtnDown) // Trap a right[ASCII 150]click.
    END_MSG_MAP()
    						
  4. In the WM_INITDIALOG handler of the CDialogImpl-derived class, subclass the ComboBox as:
          m_ComboBox.SubclassWindow(::GetDlgItem(m_hWnd,IDC_SUB_COMBOBOX));
    						
  5. In the WM_DESTROY handler of the CDialogImpl derived class unsubclass the ComboBox as:
          m_ComboBox.UnsubclassWindow();
    						
The previous steps ensure that the ALT_MSG_MAP traps the messages meant for the subclassed control. They also work for a property page in an ATL control instead of a standard dialog box.

Additional query words: ocx kbATL200 kbATL210 kbATL300 kbvc400 kbvc410 kbvc420 kbvc500 kbvc600 kbserver kbctrl

Keywords: kbhowto kbarchitecture kbdlg kbctrl KB183218