ie编程的一个问题。我该如何捕捉BeforeNavigate2事件

chenzhou35 2002-05-09 07:10:06
使用MSHTML Editing的browse模式,希望在navigate之前判断一下它的url是什么。使用连接点+IDisppatch捕获事件,我应该advise到哪个连接点?

对com和ie编程不是很熟。不知道问题有没说清楚。请指教。
谢谢。
...全文
122 点赞 收藏 13
写回复
13 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
蒋晟 2002-05-10
不是每一个document都有webbrowser的,在某些版本的IE中,Frame的Document没有对应的webbrowser
参见http://www.csdn.net/Develop/read_article.asp?id=10175
回复
chenzhou35 2002-05-10
uuuuuuuup
回复
chenzhou35 2002-05-09
我把上面得代码改写成了如下代码:

IServiceProvider* isp ;
pDoc->QueryInterface(IID_IServiceProvider,(void **)&isp);
IWebBrowser2* iwb2;
hr = isp->QueryService( IID_IWebBrowserApp,IID_IWebBrowser2, (void**)&iwb2);

但是返回E_NOINTERFACE。都不知道是怎么回事了。
回复
蒋晟 2002-05-09
A
This is a common cause for COM consternation. You have the window, document, or browser and you know you should be able to get the others, but everywhere you turn, QueryInterface delivers a big fat NULL. The answer lies in the mysterious IServiceProvider whose job it is, well, to provide services. IServiceProvider is a great interface: it has only one method, QueryService. If you’re using ATL smart pointers, it looks like this. First, you have to get the IServiceProvider.

CComQIPtr<IServiceProvider> isp = pIHTMLDocument2;

This does a QueryInterface on the document for IServiceProvider. Once you have it, you can get the browser like so.

CComQIPtr<IWebBrowser2> iwb2;
isp->QueryService(IID_IWebBrowserApp,
IID_IWebBrowser2, (void**)&iwb2);

If all this seems confusing, there’s a good reason for it. A cardinal rule of COM is that QueryInterface must always return an interface to the object queried. But the document doesn’t implement IWebBrowser2; it only knows how to get the object that does. The document, browser, and window are all separate objects. In general, IServiceProvider is used whenever a bunch of separate but related COM objects together implement some kind of service. QueryInterface asks an object, do you implement this interface? QueryService tells a service provider, "get me whatever object implements this interface, please." With QueryService, the interface pointer returned may or may not be the same object as the one queried. Figure 6 illustrates the system. All the objects implement their various interfaces and store internal pointers to one another; IServiceProvider is your way to get whichever object implements a particular interface. IServiceProvider::QueryService chases the internal pointers to retrieve the object that implements the interface you want.
回复
chenzhou35 2002-05-09
通过IHTMLDocument2怎样可以获得IWebBrowser2的接口指针?
回复
蒋晟 2002-05-09
http://www.microsoft.com/msj/defaultframe.asp?page=/msj/0698/browser.htm&nav=/msj/0698/newnav.htm
June 1998

Keeping an Eye on Your Browser by Monitoring Internet Explorer 4.0 Events
回复
weblove 2002-05-09
留下你的E-MAIL,我给你个源代码
回复
蒋晟 2002-05-09
去看看MFC的CHTMLView里面怎么捕获的
你的lpUnk是Document吧?要用Browser的才可以
回复
chenzhou35 2002-05-09
但是我枚举了它的所有连节点,只有如下:
IID_IPropertyNotifySink
IID_HTMLDocumentEvents
IID_HTMLDocumentEvents2
IID_ITridentEventSink
IID_IDispatch
共5个。我已经试过1,2,5。都不能使我捕获到这个消息。而3,4放进去无法编译。说未定义。

我该怎么办?????
回复
chenzhou35 2002-05-09
快出手啊!!!!
下个星期我就要去度婚假了。如果弄不出来好难启程的 。:(
多帮忙!!!!!
回复
蒋晟 2002-05-09
BeforeNavigate2是Browser的事件,不是Document的
回复
chenzhou35 2002-05-09
是啊,我也知道。
befornaviage的ID是:DISPID_BEFORENAVIGATE2
但是,我的代码
CImpIDispatch::Invoke(
/* [in] */ DISPID dispIdMember,
/* [in] */ REFIID riid,
/* [in] */ LCID lcid,
/* [in] */ WORD wFlags,
/* [out][in] */ DISPPARAMS* pDispParams,
/* [out] */ VARIANT* pVarResult,
/* [out] */ EXCEPINFO* pExcepInfo,
/* [out] */ UINT* puArgErr)
{

if ( dispIdMember == DISPID_BEFORENAVIGATE2 )
int x = 0;//beforenavigate
if ( dispIdMember == DISPID_HTMLDOCUMENTEVENTS_ONMOUSEUP )
int y = 0;//mouse up

return S_OK;
}
却只能捕捉到鼠标up的事件。

advise的代码如下:
(其中:CImpIDispatch* m_pDocEvents; )

if ( lpUnk != NULL )
{

IConnectionPointContainer* pCPC;
hr = lpUnk->QueryInterface(IID_IConnectionPointContainer, (void **)&pCPC);
if (hr == S_OK)
{
hr = pCPC->FindConnectionPoint( DIID_HTMLDocumentEvents, &m_pConnectionPoint );
//我想是不是,这个地方的DIID_HTMLDocumentEvents该换成别的,如果是,该是什么呢?
if (hr == S_OK)
{
// Now we can advise the connectionpoint
hr = m_pConnectionPoint->Advise( m_pDocEvents, &m_dwConnectCookie );

}

pCPC->Release();
}
}
回复
蒋晟 2002-05-09
每个事件都有自己的ID的
回复
相关推荐
发帖
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
帖子事件
创建了帖子
2002-05-09 07:10
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……