1、新建OCX工程
1.1新建MFC ActiveX Control工程,工程名称MyOcx,如下图:
1.2、工程建好后,在MyOcx.idl文件中,找到下图部分,uuid则为该新建ocx的id号。
1.3、在MyOcxCtrl.cpp文件中的OnDraw函数描绘了该ocx的样式,如下图(绘制了一个矩形和一个椭圆),如要在ocx中嵌入对话框,也在该函数内部修改。
1.4、如需引用新的插件,修改MyOcx.cpp文件中的InitInstance函数,添加AfxEnableControlContainer(); 如下图所示:
2、添加接口函数:
2.1、调出classview视图,在MyOcxLib节点下,选择_DMyOcx,如下图:
2.2、右键选择Add-Add Method,之后可以填写函数名称,返回值,函数参数等信息,如下图,其中传参数使用BSTR类型。
2.3、通过向导添加完函数后,工程文件中有几处会新增代码
(1)在MyOcxCtrl.cpp中,
(2)在MyOcxCtrl.h中,
(3)在MyOcx.idl中,
2.4、后期如要对接口函数新增参数,手动对应修改这几处即可。
3、构建安全控件
在默认环境下,编译的MFC Activex控件,只能在本地代码中运行,即在http://localhost/xxx/xxx.htm中执行,而在http://127.0.0.1/xxx/xxx.htm中提示无相关属性,需要设置其初始化和脚本运行的安全性。
3.1、使用MFC静态编译和静态库,如下图设置:
3.2、在MyOcx.cpp中添加下列代码:
**************************************************************************************
#include "comcat.h"
#include "strsafe.h"
#include "objsafe.h"
// CLSID_SafeItem - Necessary for safe ActiveX control
// Id taken from IMPLEMENT_OLECREATE_EX function in xxxCtrl.cpp
const CATID CLSID_SafeItem =
{ 0x36299202, 0x9ef, 0x4abf,{ 0xad, 0xb9, 0x47, 0xc5, 0x99, 0xdb, 0xe7, 0x78}};
// HRESULT CreateComponentCategory - Used to register ActiveX control as safe
HRESULT CreateComponentCategory(CATID catid, WCHAR *catDescription)
{
ICatRegister *pcr = NULL ;
HRESULT hr = S_OK ;
hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,
NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr);
if (FAILED(hr))
return hr;
// Make sure the HKCR\Component Categories\{..catid...}
// key is registered.
CATEGORYINFO catinfo;
catinfo.catid = catid;
catinfo.lcid = 0x0409 ; // english
size_t len;
// Make sure the provided description is not too long.
// Only copy the first 127 characters if it is.
// The second parameter of StringCchLength is the maximum
// number of characters that may be read into catDescription.
// There must be room for a NULL-terminator. The third parameter
// contains the number of characters excluding the NULL-terminator.
hr = StringCchLength(catDescription, STRSAFE_MAX_CCH, &len);
if (SUCCEEDED(hr))
{
if (len>127)
{
len = 127;
}
}
else
{
// TODO: Write an error handler;
}
// The second parameter of StringCchCopy is 128 because you need
// room for a NULL-terminator.
hr = StringCchCopy(catinfo.szDescription, len + 1, catDescription);
// Make sure the description is null terminated.
catinfo.szDescription[len + 1] = '\0';
hr = pcr->RegisterCategories(1, &catinfo);
pcr->Release();
return hr;
}
// HRESULT RegisterCLSIDInCategory -
// Register your component categories information
HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid)
{
// Register your component categories information.
ICatRegister *pcr = NULL ;
HRESULT hr = S_OK ;
hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,
NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr);
if (SUCCEEDED(hr))
{
// Register this category as being "implemented" by the class.
CATID rgcatid[1] ;
rgcatid[0] = catid;
hr = pcr->RegisterClassImplCategories(clsid, 1, rgcatid);
}
if (pcr != NULL)
pcr->Release();
return hr;
}
// HRESULT UnRegisterCLSIDInCategory - Remove entries from the registry
HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid)
{
ICatRegister *pcr = NULL ;
HRESULT hr = S_OK ;
hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,
NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr);
if (SUCCEEDED(hr))
{
// Unregister this category as being "implemented" by the class.
CATID rgcatid[1] ;
rgcatid[0] = catid;
hr = pcr->UnRegisterClassImplCategories(clsid, 1, rgcatid);
}
if (pcr != NULL)
pcr->Release();
return hr;
}
**************************************************************************************
其中,注意const CATID CLSID_SafeItem ={ 0x36299202, 0x9ef, 0x4abf,{ 0xad, 0xb9, 0x47, 0xc5, 0x99, 0xdb, 0xe7, 0x78}};的取值,其对应的CLSID位置在MyOcxCtrl.cpp文件中IMPLEMENT_OLECREATE_EX所包括,如下图(注意前3个参数在内层的{}外,后8个参数在内层{}中):
3.3、在MyOcx.cpp中修改函数DllRegisterServer和函数DllUnregisterServer,修改如下:
**************************************************************************************
STDAPI DllRegisterServer(void)
{
HRESULT hr; // HResult used by Safety Functions
AFX_MANAGE_STATE(_afxModuleAddrThis);
if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))
return ResultFromScode(SELFREG_E_TYPELIB);
if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
return ResultFromScode(SELFREG_E_CLASS);
// Mark the control as safe for initializing.
hr = CreateComponentCategory(CATID_SafeForInitializing,
L"Controls safely initializable from persistent data!");
if (FAILED(hr))
return hr;
hr = RegisterCLSIDInCategory(CLSID_SafeItem,
CATID_SafeForInitializing);
if (FAILED(hr))
return hr;
// Mark the control as safe for scripting.
hr = CreateComponentCategory(CATID_SafeForScripting,
L"Controls safely scriptable!");
if (FAILED(hr))
return hr;
hr = RegisterCLSIDInCategory(CLSID_SafeItem,
CATID_SafeForScripting);
if (FAILED(hr))
return hr;
return NOERROR;
}
STDAPI DllUnregisterServer(void)
{
HRESULT hr; // HResult used by Safety Functions
AFX_MANAGE_STATE(_afxModuleAddrThis);
if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))
return ResultFromScode(SELFREG_E_TYPELIB);
if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))
return ResultFromScode(SELFREG_E_CLASS);
// Remove entries from the registry.
hr=UnRegisterCLSIDInCategory(CLSID_SafeItem,
CATID_SafeForInitializing);
if (FAILED(hr))
return hr;
hr=UnRegisterCLSIDInCategory(CLSID_SafeItem,
CATID_SafeForScripting);
if (FAILED(hr))
return hr;
return NOERROR;
}
**************************************************************************************
4、在控件中嵌入对话框
4.1、在资源中新建对话框,修改对话框的4项属性:Static Edge改为True,Style改为Child,Visible改为True,Control改为True
4.2、通过向导将对话框绑定一个类,CDlgTest。
4.3、在MyOcxCtrl.h中新建对话框的成员变量,CDlgTest m_dlg;
4.4、重载类CMyOcxCtrl中的消息WM_CREATE的OnCreate函数,添加m_dlg.Create(IDD_DIALOG1, this);
4.5、修改类CMyOcxCtrl中的OnDraw函数,添加m_dlg.MoveWindow(rcBounds, TRUE);
4.6、此后按对话框的操作直接修改CDlgTest对话框类即可。