//
// WIN32 API CreateWindow() method
//
//register AtlAxWin class which implements ATL containment
//this is not needed for an ATL composite control
AtlAxWinInit();
//m_hWnd is the composite control handle<BR/>
//IDC_MYCTL is an ID that was added through the "View/Resource Symbols"
//menu option.
HWND hWnd = ::CreateWindow(_T("AtlAxWin"), _T("MSCAL.Calendar"),
WS_CHILD|WS_VISIBLE, 10, 10, 400, 300, m_hWnd,
(HMENU)IDC_MYCTL, _Module.GetModuleInstance(), NULL);
// make sure module handle corresponds to module in which 'AtlAxWin'
// class is registered.
-或-
//
// CAxWindow::Create() method
//
//register the AtlAxWin class which implements ATL containment
//this is not needed for an ATL composite control
AtlAxWinInit();
CAxWindow wnd;
RECT rect = {10,10,400,300};
//m_hWnd is the composite control handle
//rect is the size of ActiveX control in client coordinates
wnd.Create(m_hWnd, rect, _T("MSCAL.Calendar"),
WS_CHILD|WS_VISIBLE, 0, IDC_MYCTL);
如果需要初始化 ActiveX 控件的屬性,可以使用以下 2 方法:
//
// CAxWindow::CreateControl() method
//
// m_hWnd is usually the HWND of the activex controls' parent
CAxWindow wnd;
wnd.Attach(m_hWnd);
// Since CreateControl does not take a RECT param, the ActiveX
// control occupies the full area of the window. m_pStream is an IStream
// pointer containing the control's persisted properties
wnd.CreateControl(OLESTR("MSCAL.Calendar"), m_pStream);
// CreateControl[Ex] in turn just calls AtlAxCreateControl[Ex].
// So AltCreateControl[Ex]() could also be called directly.
// For example the above call would be
// AtlAxCreateControl(OLESTR("MSCAL.Calendar"), m_hwnd, m_pStream, NULL)
-或-
//
// CAxWindow::CreateControlEx() method
//
// CreateControlEx in addition to initializing properties, also
// allows you to specify a sink interface for events.
// m_hWnd is usually the HWND of the ActiveX control's parent.
CAxWindow wnd;
wnd.Attach(m_hWnd);
// control & container IUnknown interface pointers
LPUNKNOWN pUnkCtrl, pUnkCont;
// Since CreateControl does not take a RECT param, the ActiveX
// control occupies the full area of the window. DIID_DCalendarEvents is
// the soruce interface ID of the calendar control and m_pSink is a
// pointer to the sink object. For information on creating sinks
// please refer to knowledge base article Q194179
wnd.CreateControlEx(OLESTR("MSCAL.Calendar"), m_pStream, &pUnkCtrl,
&pUnkCont, DIID_DCalendarEvents, (IUnknown*)m_pSink);
-或-
//
// CAxWindow::AttachControl method
//
// This method is used to create the ActiveX control and activate
// it in 2 separate steps. m_hWnd is usually the HWND of the
// ActiveX control's parent.
CAxWindow wnd;
CLSID clsid;
LPUNKNOWN pUnkCtrl, pUnkCont;
HRESULT hr = CLSIDFromProgID(OLESTR("MSCAL.Calendar"), &clsid);
hr = CoCreateInstance(clsid, NULL, CLSCTX_ALL, IID_IUnknown,
(void**)&pUnkCtrl);
CComQIPtr <IPersistStreamInit> spPerStm(pUnkCtrl);
spPerStm->InitNew();
wnd.Attach(m_hWnd);
wnd.AttachControl(pUnkCtrl, &pUnkCont);
-或-
//
// IAxWinHostWindow method
//
// CAxWindow methods CreateControl[Ex] & AttachControl eventually call
// methods of IAxWinHostWindow. These methods can be called directly.
CAxWindow wnd;
RECT rect = {10,10,400,300};
// create the container window
wnd.Create(m_hWnd, rect, 0, WS_CHILD|WS_VISIBLE, 0, IDC_MYCTL);
CComPtr<IAxWinHostWindow> spHostWnd;
wnd.QueryHost(&spHostWnd);
// create the activex control
spHostWnd->CreateControl(OLESTR("MSCAL.Calendar"), wnd, NULL);
// Alternatively you could call CreateControlEx() or AttachControl()
// methods of IAxWinHostWindow as in previous examples.