#include "StdAfx.h"
#include "WebbrowserEventSinker.h"
#include "DocHostUIHandler.h"
#include <ExDispid.h>
#include <comdef.h>
#include <comdefsp.h>
/******************************************************************************
* IUnknown Interfaces -- All COM objects must implement, either
* directly or indirectly, the IUnknown interface.
******************************************************************************/
/******************************************************************************
* QueryInterface -- Determines if this component supports the
* requested interface, places a pointer to that interface in ppvObj if it is
* available, and returns S_OK. If not, sets ppvObj to NULL and returns
* E_NOINTERFACE.
******************************************************************************/
STDMETHODIMP WebbrowserEventSinker::QueryInterface(REFIID riid, void ** ppvObj)
{
if (riid == IID_IUnknown){
*ppvObj = static_cast<IUnknown*>(this);
}
else if (riid == IID_IDispatch){
*ppvObj = static_cast<IDispatch*>(this);
}
else if (riid == DIID_DWebBrowserEvents2){
*ppvObj = static_cast<IDispatch*>(this);
}
else{
*ppvObj = NULL;
return E_NOINTERFACE;
}
static_cast<IUnknown*>(*ppvObj)->AddRef();
return S_OK;
}
/******************************************************************************
* AddRef() -- In order to allow an object to delete itself when
* it is no longer needed, it is necessary to maintain a count of all
* references to this object. When a new reference is created, this function
* increments the count.
******************************************************************************/
STDMETHODIMP_(ULONG) WebbrowserEventSinker::AddRef()
{
return ++m_refCount;
}
/******************************************************************************
* Release() -- When a reference to this object is removed, this
* function decrements the reference count. If the reference count is 0,
* this function deletes this object and returns 0.
******************************************************************************/
STDMETHODIMP_(ULONG) WebbrowserEventSinker::Release()
{
m_refCount--;
if (m_refCount == 0)
{
//如果忘记调用UnAdviseWebbrowser()
UnAdviseWebbrowser();
//释放组件本身
delete this;
return 0;
}
return m_refCount;
}
/******************************************************************************
* IDispatch Interface -- This interface allows this class to be used as an
* Automation server, allowing its functions to be called by other COM
* objects.
******************************************************************************/
/******************************************************************************
* GetTypeInfoCount -- This function determines if the class supports type
* information interfaces or not. It places 1 in iTInfo if the class supports
* type information and 0 if it does not.
******************************************************************************/
STDMETHODIMP WebbrowserEventSinker::GetTypeInfoCount(UINT *iTInfo)
{
*iTInfo = 0;
return S_OK;
}
/******************************************************************************
* GetTypeInfo -- Returns the type information for the class. For classes
* that do not support type information, this function returns E_NOTIMPL;
******************************************************************************/
STDMETHODIMP WebbrowserEventSinker::GetTypeInfo(UINT iTInfo, LCID lcid,
ITypeInfo **ppTInfo)
{
return E_NOTIMPL;
}
/******************************************************************************
* GetIDsOfNames -- Takes an array of strings and returns an array of DISPIDs
* that correspond to the methods or properties indicated. If the name is not
* recognized, it returns DISP_E_UNKNOWNNAME.
******************************************************************************/
STDMETHODIMP WebbrowserEventSinker::GetIDsOfNames(REFIID riid,
OLECHAR **rgszNames,
UINT cNames, LCID lcid,
DISPID *rgDispId)
{
return E_NOTIMPL;
}
/******************************************************************************
* Invoke -- Takes a dispid and uses it to call another of the methods of this
* class. Returns S_OK if the call was successful.
******************************************************************************/
STDMETHODIMP WebbrowserEventSinker::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid,
WORD wFlags, DISPPARAMS* pDispParams,
VARIANT* pVarResult,
EXCEPINFO* pExcepInfo,
UINT* puArgErr)
{
//Validate arguments.
if ((riid != IID_NULL))
return E_INVALIDARG;
//if (dispIdMember != DISPID_STATUSTEXTCHANGE)//过于频繁的不显示了
//{
// TCHAR buf[128] = {0};
// _stprintf(buf, _T("WebbrowserEventSinker::Invoke dispId = %d\n"), dispIdMember);
// TRACE(buf);
//}
HRESULT hr = S_OK; // Initialize.
switch (dispIdMember)
{
//
// The parameters for this DISPID are as follows:
// [0]: URL to navigate to - VT_BYREF|VT_VARIANT
// [1]: An object that evaluates to the top-level or frame WebBrowser object corresponding to the event.
// Note: The IDocHostUIHandler association must be set up every time we navigate to a new page.
case DISPID_NAVIGATECOMPLETE2:
if (pDispParams->cArgs >= 2 && pDispParams->rgvarg[1].vt == VT_DISPATCH)
SetCustomDocUIHandler(pDispParams->rgvarg[1].pdispVal);
break;
case DISPID_DOCUMENTCOMPLETE:
if (pDispParams->cArgs >= 2 && pDispParams->rgvarg[1].vt == VT_DISPATCH)
{
IWebBrowser2Ptr pBrowser;
(pDispParams->rgvarg[1].pdispVal)->QueryInterface(IID_IWebBrowser2,(void**)&pBrowser);
if (pBrowser)
{
IHTMLDocument2Ptr pDocument;
IDispatchPtr spDisp;
pBrowser->get_Document(&spDisp);
pDocument = spDisp;
if (pDocument)
{
IHTMLElementPtr pElement;
IHTMLBodyElementPtr pBody; //IID_IHTMLBodyElement
pDocument->get_body(&pElement);
pBody = pElement;
if (pBody != NULL)
{
pBody->put_scroll((BSTR)L"no");
IHTMLStylePtr pStyle;
pElement->get_style(&pStyle);
pStyle->put_border((BSTR)L"none");
}
}
}
}
break;
default:
break;
}
return hr;
}
//////////////////////////////////////////////////////////////////////////
//
// 类的非接口函数实现
//
//////////////////////////////////////////////////////////////////////////
/// 静态函数,类的唯一入口,返回的指针通过Release释放(会自动调用UnAdviseWebbrowser)
WebbrowserEventSinker* WebbrowserEventSinker::AdviseWebbrowser(IWebBrowser2* pWebbrowser2)
{
WebbrowserEventSinker* pSink = NULL;
//设置连接点
try
{
if (pWebbrowser2)
{
IConnectionPointContainerPtr pConnPtContainer;
IConnectionPointPtr pConnectionPoint;
// Get IConnectionPointContainer interface for the server.
HRESULT hr = pWebbrowser2->QueryInterface( IID_IConnectionPointContainer, (void **)&pConnPtContainer );
if (SUCCEEDED(hr) && pConnPtContainer != NULL)
{
// Find the connection point for events that you are interested in.
hr = pConnPtContainer->FindConnectionPoint( DIID_DWebBrowserEvents2, &pConnectionPoint);
if (SUCCEEDED(hr) && pConnectionPoint != NULL)
{
//Create a WebbrowserEventSinker.
pSink = new WebbrowserEventSinker();
pSink->AddRef();
// Set up advisory connection.
hr = pConnectionPoint->Advise(pSink, &pSink->m_dwConnectionCookie);
if (SUCCEEDED(hr))
{
pSink->m_pConnectionPoint = pConnectionPoint;
pSink->m_pConnectionPoint->AddRef();
}
else
{
pSink->Release();
pSink = NULL;
}
}
}
}
}
catch (...)
{
}
return pSink;
}
WebbrowserEventSinker::WebbrowserEventSinker()
{
m_refCount = 0;
m_pConnectionPoint = NULL;
m_dwConnecti
去掉WebBrowser的边框和滚动条
5星 · 超过95%的资源 需积分: 50 105 浏览量
2011-08-30
16:09:41
上传
评论 2
收藏 7KB RAR 举报
超级管理员9527
- 粉丝: 46
- 资源: 9
最新资源
- 毕业设计-仿生六足机器人的制作全教程源码+电子元器件+程序代码+线路组件图+安装教程+搭建视频教程
- 基于ROS和webots的xrobot机械臂仿真初探C++源码
- 基于ROS的点焊机器人仿真与控制python源码+文档说明+使用说明+详细注释
- 基于vue实现的细粒度交通时空大数据分析系统+源代码+文档说明
- 安卓大作业-基于Electron的交通时空大数据分析挖掘系统客户端(Android)+源代码+文档说明+界面截图
- 基于Java的朱氏集团客户关系管理系统设计源码
- 基于C++的作业提交与批改系统设计源码
- 基于Vue2的移动端电影资讯网站设计源码
- 高分课程设计作业-基于QT的模仿宝石迷阵游戏C++源码+文档说明+界面截图
- 基于Apache Spark的Spark DistCP重实现设计源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页