Creating ActiveX Controls at Runtime

You've seen how to use the dialog editor to insert ActiveX controls at design time. If you need to create an ActiveX control at runtime without a resource template entry, here are the programming steps:

  1. Insert the component into your project. ClassWizard will create the files for a wrapper class.

  2. Add an embedded ActiveX control wrapper class data member to your dialog class or other C++ window class. An embedded C++ object is then constructed and destroyed along with the window object.

  3. Choose Resource Symbols from Visual C++'s View menu. Add an ID constant for the new control.

  4. If the parent window is a dialog, use ClassWizard to map the dialog's WM_INITDIALOG message, thus overriding CDialog-::OnInitDialog. For other windows, use ClassWizard to map the WM_CREATE message. The new function should call the embedded control class's Create member function. This call indirectly displays the new control in the dialog. The control will be properly destroyed when the parent window is destroyed.

  5. In the parent window class, manually add the necessary event message handlers and prototypes for your new control. Don't forget to add the event sink map macros.

ClassWizard doesn't help you with event sink maps when you add a dynamic ActiveX control to a project. Consider inserting the target control in a dialog in another temporary project. After you're finished mapping events, simply copy the event sink map code to the parent window class in your main project.