How to subclass a dialog item in ATL (183218)



The information in this article applies to:

  • The Microsoft Active Template Library (ATL) 2.0, when used with:
    • Microsoft Visual C++, 32-bit Editions 4.0
    • Microsoft Visual C++, 32-bit Editions 4.1
    • Microsoft Visual C++, 32-bit Enterprise Edition 4.2
    • Microsoft Visual C++, 32-bit Enterprise Edition 5.0
    • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
    • Microsoft Visual C++, 32-bit Professional Edition 4.2
    • Microsoft Visual C++, 32-bit Professional Edition 5.0
    • Microsoft Visual C++, 32-bit Professional Edition 6.0
    • Microsoft Visual C++, 32-bit Learning Edition 6.0
    • Microsoft Visual C++ .NET (2002)
    • Microsoft Visual C++ .NET (2003)
  • The Microsoft Active Template Library (ATL) 2.1, when used with:
    • Microsoft Visual C++, 32-bit Editions 4.0
    • Microsoft Visual C++, 32-bit Editions 4.1
    • Microsoft Visual C++, 32-bit Enterprise Edition 4.2
    • Microsoft Visual C++, 32-bit Enterprise Edition 5.0
    • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
    • Microsoft Visual C++, 32-bit Professional Edition 4.2
    • Microsoft Visual C++, 32-bit Professional Edition 5.0
    • Microsoft Visual C++, 32-bit Professional Edition 6.0
    • Microsoft Visual C++, 32-bit Learning Edition 6.0
    • Microsoft Visual C++ .NET (2002)
    • Microsoft Visual C++ .NET (2003)
  • The Microsoft Active Template Library (ATL) 3.0, when used with:
    • Microsoft Visual C++, 32-bit Editions 4.0
    • Microsoft Visual C++, 32-bit Editions 4.1
    • Microsoft Visual C++, 32-bit Enterprise Edition 4.2
    • Microsoft Visual C++, 32-bit Enterprise Edition 5.0
    • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
    • Microsoft Visual C++, 32-bit Professional Edition 4.2
    • Microsoft Visual C++, 32-bit Professional Edition 5.0
    • Microsoft Visual C++, 32-bit Professional Edition 6.0
    • Microsoft Visual C++, 32-bit Learning Edition 6.0
    • Microsoft Visual C++ .NET (2002)
    • Microsoft Visual C++ .NET (2003)

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.

Modification Type:MajorLast Reviewed:5/26/2005
Keywords:kbArchitecture kbCtrl kbDlg kbhowto KB183218 kbAudDeveloper