动态添加 ActiveX 控件添加到 ATL 复合控件

6 篇文章 0 订阅

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值