動態添加 ActiveX 控件添加到 ATL 複合控件

http://support.microsoft.com/kb/218442/zh-cn

將上述鏈接最後的zh-cn換成 en-us 就是對應的英文鏈接。

如何將 ActiveX 控件添加到 ATL 複合控件以編程方式在 Visual C++

文章編號: 218442 - 查看本文應用於的產品

概要

本文介紹如何以編程方式將 ActiveX 控件添加到 ATL 複合控件。這些技術還可用於動態創建任何窗口上的 ActiveX 控件。

更多信息

WM_INITDIALOG 處理程序中以編程方式將 ActiveX 控件添加到 ATL 複合控件的好地方。將下面的消息映射項並處理程序函數添加到 CComCompositeControl 派生類。

MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)

LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, 
   BOOL& bHandled)
{
   return 0;
}
				

現在可以使用以下方法之一來在 WM_INITDIALOG 的消息處理程序中以編程方式添加控件。CAxWindow 類是隻是一個包裝類。當超出範圍時,它不會銷燬相應的 ActiveX 控件,以便可以在堆棧上聲明它。通過將其作爲成員的複合控件的類,您可以使用 cwindow 外的方便的包裝函數 (CAxWindow 派生自 cwindow 外)。此外,在下列 ATL 創建 ProgID 的功能可以被替換 CLSID 或 URL。CAxWindow::CreateControl() 聯機文檔的詳細信息,請參閱。若要獲取的控件或容器接口,請使用 QueryControl() 和 QueryHost() 的 CAxWindow 成員或 AtlAxGetControl() 和 AtlAxGetHost() 的全局函數。
// 
// 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.
				

參考

VC + + 聯機文檔

有關詳細信息,請單擊下面的文章編號,以查看 Microsoft 知識庫中相應的文章:
192560如何向添加 ATL 控件包容支持任何窗口
194179示例: AtlEvnt.exe 創建 ATL 接收器使用 IDispEventImpl
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章