本人也是刚接触activeX控件,自己通过vs2010编写了几个简单的例子,经测试工具测试过都可以用,在html文件添加之后如果双击文件用IE打开,会有加载控件之类的提示,但是在浏览器中输入url地址访问时,在页面中会出现一个红叉,修改IE的设置也没用。本文要解决的就是浏览器中出现红叉的问题。
步骤如下:
(1)在Ctrl.h文件中添加头文件
#include "objsafe.h"
#include "comcat.h"
(2)在Ctrl.h文件中添加以下3个方法的声明(注意:声明的位置是在类的外面,就这(1)中添加的两个头文件的下方就行)
HRESULT CreateComponentCategory(CATID catid, WCHAR *catDescription);
HRESULT RegisterCLSIDInCategory(REFCLSID clsid,CATID catid);
HRESULT UnRegisterCLSIDInCategory( REFCLSID clsid,CATID catid );
(3)在Ctrl.cpp文件中添加与(2)中对应3个方法
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 FALSE;
CATEGORYINFO catinfo;
catinfo.catid = catid;
catinfo.lcid = 0x0409;
int len = wcslen(catDescription);
if(len>127)
len = 127;
wcsncpy(catinfo.szDescription,catDescription,len);
catinfo.szDescription[len] = '\0';
hr = pcr->RegisterCategories(1, &catinfo);
pcr->Release();
return hr;
}
HRESULT RegisterCLSIDInCategory(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))
{
CATID rgcatid[1];
rgcatid[0] = catid;
hr = pcr->RegisterClassImplCategories(clsid,1,rgcatid);
}
if( pcr != NULL )
pcr->Release();
return hr;
}
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))
{
CATID rgcatid[1];
rgcatid[0] = catid;
hr = pcr->UnRegisterClassImplCategories(clsid,1,rgcatid);
}
if(pcr != NULL)
pcr->Release();
return hr;
}
(4)在Ctrl.cpp文件中替换一部分代码
找到如下类似的代码,然后注释掉
if(bRegister)
return AfxOleRegisterControlClass(
AfxGetInstanceHandle(),
m_clsid,
m_lpszProgID,
IDS_ICCARD,
IDB_ICCARD,
afxRegApartmentThreading,
_dwIcCardOleMisc,
_tlid,
_wVerMajor,
_wVerMinor);
}
else
return AfxOleUnregisterClass(m_clsid,m_lpszProgID);
把上述代码替换成:
if(bRegister)
{
HRESULT hr = S_OK;
hr = CreateComponentCategory(CATID_SafeForScripting,L"Controls that are safely scriptable");
if(FAILED(hr))
return FALSE;
hr = RegisterCLSIDInCategory(m_clsid,CATID_SafeForScripting);
if(FAILED(hr))
return FALSE;
hr = CreateComponentCategory(CATID_SafeForInitializing,L"Controls safely initizable from persisitent data");
if(FAILED(hr))
return FALSE;
hr = RegisterCLSIDInCategory(m_clsid,CATID_SafeForInitializing);
if(FAILED(hr))
return FALSE;
return AfxOleRegisterControlClass(
AfxGetInstanceHandle(),
m_clsid,
m_lpszProgID,
IDS_ICCARD, // 需要修改的位置
IDB_ICCARD, //需要修改的位置
afxRegApartmentThreading,
_dwIcCardOleMisc, //需要修改的位置
_tlid,
_wVerMajor,
_wVerMinor);
}
else
{
HRESULT hr = S_OK;
hr = UnRegisterCLSIDInCategory(m_clsid,CATID_SafeForScripting);
if(FAILED(hr))
return FALSE;
hr = UnRegisterCLSIDInCategory(m_clsid,CATID_SafeForInitializing);
if(FAILED(hr))
return FALSE;
return AfxOleUnregisterClass(m_clsid,m_lpszProgID);
}
(5)最后一步,把(4)中3个“需要修改的位置”地方修改成对应自己项目中的名称,然后生成控件,重新注册就行了。
总结:我用IIS服务器在我自己机子上测试的完全正常,但是在局域网内用别人机子访问了一下,控件位置还是显示红叉,还不知道怎么解决,希望大家有谁知道的可以告诉一下!