Using the VSPrinter Control > Visual C++ Topics > Creating Controls Dynamically in ATL |
As in MFC, you can create controls dynamically with ATL. The steps required are these:
Insert the appropriate #import statement in the dialog header file or in the StdAfx.h file.
Add two member variables to your dialog or window class:
Example Title |
Copy Code
|
---|---|
CAxWindow m_wndPrinter; // host window IVSPrinterPtr m_spPrinter; // pointer to control |
When the dialog or window is created, create the control and its host window.
Example Title |
Copy Code
|
---|---|
// initialize ATL ActiveX hosting AtlAxWinInit(); // create the control (will fail if not registered) m_spPrinter.CreateInstance(__uuidof(VSPrinter))); ATLASSERT(m_spPrinter != NULL); // create the host window (nID = IDC_VSPRINTER1) // the nID is needed if you want to sink events. RECT rc; GetClientRect(&rc); m_wndPrinter.Create(m_hWnd, rc, NULL, WS_CHILD | WS_VISIBLE, 0, IDC_ VSPRINTER1); |
Attach the control to the host window.
Example Title |
Copy Code
|
---|---|
CComPtr<IAxWinHostWindow> spHost; m_wndPrinter.QueryHost(&spHost); spHost->AttachControl(m_spPrinter, m_wndPrinter); |
As in MFC, this approach requires you to hook up the event handlers manually. You can also copy wizard-generated code, which does two things: it adds an IDispEventImpl declaration to your class so it inherits the ATL event handling mechanisms and adds an event sink map to your class using BEGIN_SINK_MAP, SINK_ENTRY and END_SINK_MAP macros. You still need to add the call to AtlAdviseSinkMap in order to start getting the events.