To dynamically create an ActiveX control in a non-dialog window Insert CIRC.H in CMYVIEW.H, just before the CMyView class definition: #include "circ.h" Add a member variable (of type CCirc) to the protected section of the CMyView class definition located in CMYVIEW.H: class CMyView : public CView { ... protected:CCirc m_myCtl; ... }; Add a WM_CREATE message handler to class CMyView. In the handler function, CMyView::OnCreate, make a call to the control's Create function using the this pointer as the parent window: int CMyView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (MyView::OnCreate(lpCreateStruct) == -1) return -1;// ****** Add your code below this line ********** // m_myCtl.Create(NULL, WS_VISIBLE, CRect(50,50,100,100), this, 0); m_myCtl.SetCaption(_T("Control created"));// ****** Add your code above this line ********** // return 0; }
用RegOpenKey等windows api 查找HKEY_CLASSES_ROOT\CLSID
直接枚举CLSID,是很大的工作量呀。
1. in MFC Add a class from ActiveX control, say CMyClass use CMyClass.CreateControl to dynamically create a control 2. in Win32 CoCreateInstance 3. in ATL use CAxWindow class
#include <atlbase.h>
CComModule _Module;
#include <atlcom.h>
#include <atlwin.h>
#pragma comment(lib,"atl")
CComQIPtr<IWebBrowser2> m_spBrowser; //web控件
CAxWindow content_wnd;
......
if( _tcslen(m_tcHtmlFileName) > 0 )
{
RECT rc;
GetClientRect( &rc );
if(m_spBrowser==NULL)
{
LPOLESTR pstrbrowserid;
StringFromCLSID(IID_IWebBrowser2,&pstrbrowserid);
_bstr_t bstrbrowser(pstrbrowserid);
CoTaskMemFree(pstrbrowserid);
if(content_wnd.IsWindow())
content_wnd.DestroyWindow();
content_wnd.Create( m_hWnd, rc, LPCTSTR(bstrbrowser), WS_CHILD¦WS_VISIBLE¦WS_HSCROLL¦WS_VSCROLL );//create a browser control
HRESULT hrbrowser;
hrbrowser = content_wnd.QueryControl( IID_IWebBrowser2, reinterpret_cast<void**>(&m_spBrowser) );
}
Insert CIRC.H in CMYVIEW.H, just before the CMyView class definition:
#include "circ.h"
Add a member variable (of type CCirc) to the protected section of the CMyView class definition located in CMYVIEW.H:
class CMyView : public CView
{
...
protected:CCirc m_myCtl;
...
};
Add a WM_CREATE message handler to class CMyView.
In the handler function, CMyView::OnCreate, make a call to the control's Create function using the this pointer as the parent window:
int CMyView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (MyView::OnCreate(lpCreateStruct) == -1)
return -1;// ****** Add your code below this line ********** // m_myCtl.Create(NULL, WS_VISIBLE,
CRect(50,50,100,100), this, 0);
m_myCtl.SetCaption(_T("Control created"));// ****** Add your code above this line ********** // return 0;
}
Add a class from ActiveX control, say CMyClass
use CMyClass.CreateControl to dynamically create a control
2. in Win32
CoCreateInstance
3. in ATL
use CAxWindow class