Creating ActiveX Controls at Run Time
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 run time without a resource template entry, here are the programming steps:
Insert the component into your project. Visual Studio .NET will create the files for a wrapper class.
Add an embedded ActiveX control wrapper class data member to your dialog class or other C++ window class. An embedded C++ object will be constructed and destroyed along with the window object.
Choose Resource View from Visual C++ .NET's View menu. In Resource View, right-click on your RC file and choose Resource Symbols from the shortcut menu. Add an ID constant for the new control.
If the parent window is a dialog box, use Class View's Properties window to override CDialog::OnInitDialog. For other windows besides CDialog, use Class View's Properties window 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 box. The control will be properly destroyed when the parent window is destroyed.
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.
Tip | The code wizards available from Class View don'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 box 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. |