MFC IE 事件挂载
来源:互联网 发布:北京网络安全员招聘网 编辑:程序博客网 时间:2024/05/01 02:42
void CIEToolhelperDlg::GetIWebBrowser2_3(CString url)
{
if (SUCCEEDED(OleInitialize(NULL)))
{
CComPtr<IConnectionPointContainer> spConnectionPointer;
CComPtr<IConnectionPoint> spConnp;
// CComQIPtr < IWebBrowser2 > spWebBrowser2;
IWebBrowser2 * spWebBrowser3;
CComPtr < IUnknown > spUnk;
spUnk.CoCreateInstance(CLSID_InternetExplorer,spUnk, CLSCTX_LOCAL_SERVER);
CoCreateInstance(CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER,
IID_IWebBrowser2, (void**)&spWebBrowser3);
if(spWebBrowser3)
{
VARIANT vEmpty;
VariantInit(&vEmpty);
BSTR bstrURL = url.AllocSysString();
HRESULT hr = spWebBrowser3->Navigate(bstrURL, &vEmpty, &vEmpty, &vEmpty, &vEmpty);
if (SUCCEEDED(hr))
{
spWebBrowser3->put_Visible(VARIANT_TRUE);
}
else
{
spWebBrowser3->Quit();
}
}
// ieevent=new IEEventInterface;
// pSink=new EventSink;
// pSink->m_ie=spWebBrowser3;
this->m_sink2=new EventSink2;
this->m_sink2->m_ie=spWebBrowser3;
// IID iid = IID_IUnknown;
// LPUNKNOWN pUnkSink=this->pSink->GetIDispatch(FALSE);
LPUNKNOWN pUnkSink=this->m_sink2->GetIDispatch(FALSE);
if(!AfxConnectionAdvise(spWebBrowser3, DIID_DWebBrowserEvents2, pUnkSink, FALSE, &dwCookie))
{
this->MessageBox("connect to IE event faild");
}
// spWebBrowser2.Release();
///
///
// OleUninitialize();
}
}
////////////sink
BEGIN_DISPATCH_MAP(EventSink2, CCmdTarget)
//{{AFX_DISPATCH_MAP(EventSink2)
// NOTE - the ClassWizard will add and remove mapping macros here.
DISP_FUNCTION_ID(EventSink2,"DocumentComplete",DISPID_DOCUMENTCOMPLETE,DocumentComplete,VT_EMPTY,VTS_DISPATCH VTS_PVARIANT)
DISP_FUNCTION_ID(EventSink2,"onclick", DISPID_HTMLELEMENTEVENTS2_ONCLICK,OnClick,VT_VARIANT,VTS_DISPATCH)
//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()
// Note: we add support for IID_IEventSink2 to support typesafe binding
// from VBA. This IID must match the GUID that is attached to the
// dispinterface in the .ODL file.
// {0BBBA45B-EF3F-423F-BBCD-9ED9697F74A4}
static const IID IID_IEventSink2 =
{ 0xbbba45b, 0xef3f, 0x423f, { 0xbb, 0xcd, 0x9e, 0xd9, 0x69, 0x7f, 0x74, 0xa4 } };
BEGIN_INTERFACE_MAP(EventSink2, CCmdTarget)
INTERFACE_PART(EventSink2, IID_IEventSink2, Dispatch)
END_INTERFACE_MAP()
/////////////////////////////////////////////////////////////////////////////
// EventSink2 message handlers
void EventSink2::DocumentComplete(IDispatch *pDisp, VARIANT *URL)
{
HRESULT hr;
IUnknown* pUnkBrowser = NULL;
IUnknown* pUnkDisp = NULL;
IDispatch* pDocDisp = NULL;
IHTMLDocument2* pDoc = NULL;
// Is this the DocumentComplete event for the top frame window?
// Check COM identity: compare IUnknown interface pointers.
hr = m_ie->QueryInterface(IID_IUnknown, (void**)&pUnkBrowser);
if (SUCCEEDED(hr))
{
hr = pDisp->QueryInterface(IID_IUnknown, (void**)&pUnkDisp);
if (SUCCEEDED(hr))
{
if (pUnkBrowser == pUnkDisp)
{
// This is the DocumentComplete event for the top frame.
// This page is loaded, so we can access the DHTML Object Model.
hr = m_ie->get_Document(&pDocDisp);
if (SUCCEEDED(hr))
{
// Obtained the document object.
pDocDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pDoc);
if (SUCCEEDED(hr))
{
// Obtained the IHTMLDocument2 interface for the document object
ProcessDocument(pDoc);
//AfxMessageBox(_T("finish....."));
}
pDocDisp->Release();
}
}
pUnkDisp->Release();
}
pUnkBrowser->Release();
}
}
void EventSink2::ProcessDocument(IHTMLDocument2 *pDoc)
{
HRESULT hr;
IHTMLElementCollection* pElemColl = NULL;
hr = pDoc->get_all(&pElemColl);
if (SUCCEEDED(hr))
{
// Obtained element collection.
ProcessElementCollection(pElemColl);
pElemColl->Release();
}
}
void EventSink2::ProcessElementCollection(IHTMLElementCollection *pElemColl)
{
HRESULT hr;
IDispatch* pElemDisp = NULL;
IHTMLElement* pElem = NULL;
_variant_t varID("su");
_variant_t varIdx(0L);
hr = pElemColl->item(varID, varIdx, &pElemDisp);
if (SUCCEEDED(hr))
{
hr = pElemDisp->QueryInterface(IID_IHTMLElement, (void**)&pElem);
if (SUCCEEDED(hr))
{
// Obtained element with ID of "myID".
ConnectEvents(pElem);
pElem->Release();
}
pElemDisp->Release();
}
}
BOOL EventSink2::OnClick(IHTMLEventObj *pEvtObj)
{
::AfxMessageBox("Button clicked!");
return TRUE;
}
void EventSink2::ConnectEvents(IHTMLElement *pElem)
{
HRESULT hr;
IConnectionPointContainer* pCPC = NULL;
IConnectionPoint* pCP = NULL;
DWORD dwCookie;
// Check that this is a connectable object.
hr = pElem->QueryInterface(IID_IConnectionPointContainer, (void**)&pCPC);
if (SUCCEEDED(hr))
{
// Find the connection point.
hr = pCPC->FindConnectionPoint(DIID_HTMLElementEvents2, &pCP);
if (SUCCEEDED(hr))
{
// Advise the connection point.
// pUnk is the IUnknown interface pointer for your event sink
hr = pCP->Advise(this->GetIDispatch(FALSE), &dwCookie);
if (SUCCEEDED(hr))
{
// Successfully advised
}
pCP->Release();
}
pCPC->Release();
}
}
- MFC IE 事件挂载
- IE事件 (消息及事件)
- W3C事件与IE事件
- IE窗口关闭事件
- 捕获IE键盘事件
- javascript IE窗体事件
- 模拟IE浏览器事件
- IE事件处理程序
- IE OnKeyDwon事件使用
- IE中的事件对象
- IE监听事件
- IE 事件两次响应
- ie集成ocx事件
- 屏蔽IE相关事件
- IE事件处理程序
- IE事件处理程序
- MFC 键盘响应事件
- MFC鼠标事件
- “WriteHuge”: 不是“CFile”的成员
- 无线AP下使用锐捷的一些事 【无线路由器 锐捷 无线上网 不能认证 AP】
- 价格奥秘-在超市遇见亚当斯密--第三章 为什么街角的咖啡馆总有足够的面包圈?
- 设计模式--单列模式
- 字典树(Trie tree)
- MFC IE 事件挂载
- 数据库的简单监控
- 全面接触Java集合框架
- 狗日的360浏览器 游戏AS弹窗口被阻止
- rsync 在CentOS等Linux系统下搭建及相关问题分析
- ora 04091:表XX发生了变化,触发器/函数不能读它(lin49940)
- Selenium-webdriver系列教程(10)——如何智能的等待页面加载完成
- java中的arrayList(动态数组)与静态数组
- Filezilla 搭建windows FTP