1 创建mfc ActiveX工程
使用vs2105新建一个mfc ActiveX工程,工程名称为ocx_test,如果要在ocx控件中使用对话框,那么就勾选“在插入对象对话框中可用”
更改工程的属性,mfc的使用改为在静态库中使用mfc,字符集未未设置。
2 在控件中使用嵌入对话框
在工程的资源视图中插入一个对话框
然后将对话框的属性修改,style改为child,visible改为true
右键该对话框-添加类
为该对话框创建一个类,名称为CDialog1
在ocx_testCtrl.h文件中添加对话框的头文件
#include"Dialog1.h"//引入对话框类的头文件
在public中添加对话框的变量定义
CDialog1 dlg1;//声明对话框类的对象
接下来在类向导中为Cocx_testCtrl类添加消息响应VM_CREATE
在OnCreate函数中添加:
dlg1.Create(IDD_DIALOG1, this);
接着在onDraw函数改为如下代码:
/*pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
pdc->Ellipse(rcBounds);*/
dlg1.MoveWindow(rcBounds, true);
接下来生成工程,ocx会自动进行注册。
3 在网页中调用ocx
打开ocx_test.idl这个文件,从如下地方找到ocx的classID
新建一个html,html中的Classid就为上图中的值
<html>
<head>
<title>test</title>
<meta charset="utf-8" />
</head>
<body>
<object id=fp_atl border=0 classid=CLSID:16EEE8BE-67C3-4A6D-B892-DBE374724FA8 width="400" height="400";>
</object>
</body>
</html>
然后使用ie浏览器打开这个html,就可以看到网页中出现了含有对话框的ocx控件。
4 去掉浏览器弹出交互不安全提示
当使用上面创建的这种ocx,在浏览器中会有不安全提示
解决办法是在ocx_testCtrl.h文件中添加一个头文件
#include<ObjSafe.h>
在该类的protect的中添加消息声明:
//去掉安全警告 BEGIN
DECLARE_INTERFACE_MAP()
BEGIN_INTERFACE_PART(ObjectSafety, IObjectSafety)
STDMETHOD(GetInterfaceSafetyOptions)(REFIID riid, DWORD __RPC_FAR *pdwSupportedOptions, DWORD __RPC_FAR *pdwEnabledOptions);
STDMETHOD(SetInterfaceSafetyOptions)(REFIID riid, DWORD dwOptionSetMask, DWORD dwEnabledOptions);
END_INTERFACE_PART(ObjectSafety)
//去掉安全警告 END
接着在ocx_testCtrl.cpp文件中IMPLEMENT_OLECTLTYPE(Cocx_testCtrl, IDS_OCX_TEST, _dwocx_testOleMisc)后面添加:
//注意要将这个里面的类名替换为自己工程的那个类名
//去掉安全警告 BEGIN
BEGIN_INTERFACE_MAP(Cocx_testCtrl, COleControl)
INTERFACE_PART(Cocx_testCtrl, IID_IObjectSafety, ObjectSafety)
END_INTERFACE_MAP()
// Implementation of IObjectSafety
STDMETHODIMP Cocx_testCtrl::XObjectSafety::GetInterfaceSafetyOptions(
REFIID riid,
DWORD __RPC_FAR *pdwSupportedOptions,
DWORD __RPC_FAR *pdwEnabledOptions)
{
METHOD_PROLOGUE_EX(Cocx_testCtrl, ObjectSafety)
if (!pdwSupportedOptions || !pdwEnabledOptions)
{
return E_POINTER;
}
*pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER
| INTERFACESAFE_FOR_UNTRUSTED_DATA;
*pdwEnabledOptions = 0;
if (NULL == pThis->GetInterface(&riid))
{
TRACE("Requested interface is not supported.\n");
return E_NOINTERFACE;
}
// What interface is being checked out anyhow?
OLECHAR szGUID[39];
int i = StringFromGUID2(riid, szGUID, 39);
if (riid == IID_IDispatch)
{
// Client wants to know if object is safe for scripting
*pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
return S_OK;
}
else if (riid == IID_IPersistPropertyBag
|| riid == IID_IPersistStreamInit
|| riid == IID_IPersistStorage
|| riid == IID_IPersistMemory)
{
// Those are the persistence interfaces COleControl derived controls support
// as indicated in AFXCTL.H
// Client wants to know if object is safe for initializing from persistent data
*pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
return S_OK;
}
else
{
// Find out what interface this is, and decide what options to enable
TRACE("");
return E_NOINTERFACE;
}
}
STDMETHODIMP Cocx_testCtrl::XObjectSafety::SetInterfaceSafetyOptions(
REFIID riid,
DWORD dwOptionSetMask,
DWORD dwEnabledOptions)
{
METHOD_PROLOGUE_EX(Cocx_testCtrl, ObjectSafety)
OLECHAR szGUID[39];
// What is this interface anyway?
// We can do a quick lookup in the registry under HKEY_CLASSES_ROOT\Interface
int i = StringFromGUID2(riid, szGUID, 39);
if (0 == dwOptionSetMask && 0 == dwEnabledOptions)
{
// the control certainly supports NO requests through the specified interface
// so it"s safe to return S_OK even if the interface isn"t supported.
return S_OK;
}
// Do we support the specified interface?
if (NULL == pThis->GetInterface(&riid))
{
TRACE1("%s is not support.\n", szGUID);
return E_FAIL;
}
if (riid == IID_IDispatch)
{
TRACE("");
TRACE("In other words, is the control safe for scripting?\n");
if (INTERFACESAFE_FOR_UNTRUSTED_CALLER == dwOptionSetMask
&& INTERFACESAFE_FOR_UNTRUSTED_CALLER == dwEnabledOptions)
{
return S_OK;
}
else
{
return E_FAIL;
}
}
else if (riid == IID_IPersistPropertyBag
|| riid == IID_IPersistStreamInit
|| riid == IID_IPersistStorage
|| riid == IID_IPersistMemory)
{
TRACE("");
TRACE("In other words, is the control safe for initializing from persistent data?\n");
if (INTERFACESAFE_FOR_UNTRUSTED_DATA == dwOptionSetMask
&& INTERFACESAFE_FOR_UNTRUSTED_DATA == dwEnabledOptions)
{
return NOERROR;
}
else
{
return E_FAIL;
}
}
else
{
TRACE1("", szGUID);
return E_FAIL;
}
}
STDMETHODIMP_(ULONG) Cocx_testCtrl::XObjectSafety::AddRef()
{
METHOD_PROLOGUE_EX_(Cocx_testCtrl, ObjectSafety)
return (ULONG)pThis->ExternalAddRef();
}
STDMETHODIMP_(ULONG) Cocx_testCtrl::XObjectSafety::Release()
{
METHOD_PROLOGUE_EX_(Cocx_testCtrl, ObjectSafety)
return (ULONG)pThis->ExternalRelease();
}
STDMETHODIMP Cocx_testCtrl::XObjectSafety::QueryInterface(
REFIID iid, LPVOID* ppvObj)
{
METHOD_PROLOGUE_EX_(Cocx_testCtrl, ObjectSafety)
return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
}
//去掉安全警告 END
然后重新生成项目,再次在网页中调用,就可以看到没有出现不安全警告了。