问题描述
我在 VisualC++10 中使用 ATL 来托管浏览器控件.我的代码类似于这个例子:http://msdn.microsoft.com/en-us/library/9d0714y1(v=vs.80).aspx
I am using ATL in VisualC++10 to host browser control.My code is similar to this example: http://msdn.microsoft.com/en-us/library/9d0714y1(v=vs.80).aspx
不同之处在于我有主窗口,然后子窗口托管浏览器控件.2 分钟后,我必须关闭浏览器完全杀死浏览器 activeX 但这个子窗口应该是活的并做其他事情.但是不知何故这个浏览器控件仍然保留在那里,我可以看到滚动条或其他东西..
Difference is I have main window and then child window hosts the browser control.After 2 minutes i have to close the browser completely kill the browser activeX but this child window should be alive and do something else. But somehow this browser control still stays there, i can either see scrollbars or something..
我也尝试通过为现有子窗口创建子窗口,然后在关闭浏览器时销毁子窗口 - 但仍然不起作用!
I have also tried by creating child window to an existing child window, and at the time of closing browser I then destroy this child of a child - but still it does not work!
这就是我关闭的方式:
CLOSE()
{
m_spIWebBrowser2->Navigate(bstrURL, &vEmpty, &vEmpty, &vEmpty, &vEmpty);
m_spIWebBrowser2->Stop();
m_spIWebBrowser2->put_Visible(VARIANT_FALSE);
m_spIWebBrowser2->Quit();
DestroyWindow(m_wndChild.m_hWnd);
}
谢谢!
推荐答案
我在关闭浏览器控件时遇到了很多访问冲突"问题,这些是对我有用的步骤:
I had many problems with "access violation" when closing webbrowser control, these are the steps that worked for me:
- 不建议任何先前建议的事件(在我的情况下为 DWebBrowserEvents2).
- 如果您附加了点击事件,请像这样取消附加它们:
_variant_t v;v.vt = VT_DISPATCH;v.pdispVal = 0;IHTMLDocument2->put_onclick(v);
IWebBrowser2->Stop()
IWebBrowser2->ExecWB(OLECMDID_CLOSE, OLECMDEXECOPT_DONTPROMPTUSER, 0, 0)
- 通过 window.external.CloseWindow() 关闭浏览器窗口时,我遇到了未处理的异常,OLECMDID_CLOSE 修复了它.IWebBrowser2->put_Visible(VARIANT_FALSE)
IWebBrowser2->Release()
IOleInPlaceObject->InPlaceDeactivate()
IOleInPlaceObject->Release()
IOleObject->DoVerb(OLEIVERB_HIDE, NULL, IOleClientSite, 0, windowHandle_, NULL)
IOleObject->关闭(OLECLOSE_NOSAVE)
OleSetContainedObject(IOleObject, FALSE)
IOleObject->SetClientSite(NULL)
CoDisconnectObject(IOleObject, 0)
IOleObject->Release()
- Unadvise any previously advised events (DWebBrowserEvents2 in my case).
- If you've attached click events unattach them like this:
_variant_t v; v.vt = VT_DISPATCH; v.pdispVal = 0; IHTMLDocument2->put_onclick(v);
IWebBrowser2->Stop()
IWebBrowser2->ExecWB(OLECMDID_CLOSE, OLECMDEXECOPT_DONTPROMPTUSER, 0, 0)
- when closing browser window through window.external.CloseWindow() I had unhandled exceptions and OLECMDID_CLOSE fixed it.IWebBrowser2->put_Visible(VARIANT_FALSE)
IWebBrowser2->Release()
IOleInPlaceObject->InPlaceDeactivate()
IOleInPlaceObject->Release()
IOleObject->DoVerb(OLEIVERB_HIDE, NULL, IOleClientSite, 0, windowHandle_, NULL)
IOleObject->Close(OLECLOSE_NOSAVE)
OleSetContainedObject(IOleObject, FALSE)
IOleObject->SetClientSite(NULL)
CoDisconnectObject(IOleObject, 0)
IOleObject->Release()
IWebBrowser2->Quit()
不应为 WebBrowser 控件 (CLSID_WebBrowser) 调用,它仅适用于 Internet Explorer 对象 (CLSID_InternetExplorer).
IWebBrowser2->Quit()
should not be called for WebBrowser control (CLSID_WebBrowser), it is intended only for Internet Explorer object (CLSID_InternetExplorer).
为什么一定要这么难?
这篇关于如何彻底销毁 WebBrowser 控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!