解决方法: 在View.cpp中加入: void CMIEView::OnNewWindow2(LPDISPATCH* ppDisp, BOOL* Cancel) { // TODO: Add your specialized code here and/or call the base class CComPtr<IHTMLDocument2> pHTMLDocument2;
m_pBrowserApp->get_Document((IDispatch **)&pHTMLDocument2); if (pHTMLDocument2!=NULL) { CComPtr<IHTMLElement> pIHTMLElement; pHTMLDocument2->get_activeElement(&pIHTMLElement);
if (pIHTMLElement!=NULL) { variant_t url; HRESULT hr=pIHTMLElement->getAttribute(L"href", 0, &url); if (SUCCEEDED(hr)) { hr=m_pBrowserApp->Navigate2(&url, NULL, NULL, NULL, NULL);
{
::SendMessage(AfxGetMainWnd()->m_hWnd,WM_NEWURL,(WPARAM)ppDisp,NULL);
*Cancel=TRUE;
CHtmlView::OnNewWindow2(ppDisp, Cancel);
} (3)在CMainFrame中映射消息WM_NEWURL,实现打开新窗口,并对LPDISPATCH* ppDisp进行重定向.LRESULT CMainFrame::OnNewURL(WPARAM wParam,LPARAM lParam)
{
LPDISPATCH* ppDispatch=(LPDISPATCH*)wParam;
SendMessage(WM_COMMAND, ID_FILE_NEW, 0);
CChildFrame* pChildFrame = (CChildFrame*)GetActiveFrame();
*ppDispatch=((CxxxxView*)pChildFrame->GetActiveView())->GetApplication();
return 0;
} 如果要实现在当前窗口打开新文档,不弹出新窗口,则需要在当前HtmlView下新建一个隐藏HtmlView,在当前OnNewWindow2中重定向到隐藏的HtmlView,再在隐藏的HtmlView中重定向回来(原理是这样但没测试过,本来在OnNewWindow2中直接重定向到当前窗口但无反应,似乎需要重写向至新实例).
在View.cpp中加入: void CMIEView::OnNewWindow2(LPDISPATCH* ppDisp, BOOL* Cancel)
{
// TODO: Add your specialized code here and/or call the base class
CComPtr<IHTMLDocument2> pHTMLDocument2;
m_pBrowserApp->get_Document((IDispatch **)&pHTMLDocument2);
if (pHTMLDocument2!=NULL)
{
CComPtr<IHTMLElement> pIHTMLElement;
pHTMLDocument2->get_activeElement(&pIHTMLElement);
if (pIHTMLElement!=NULL)
{
variant_t url;
HRESULT hr=pIHTMLElement->getAttribute(L"href", 0, &url);
if (SUCCEEDED(hr))
{
hr=m_pBrowserApp->Navigate2(&url, NULL, NULL, NULL, NULL);
url.Clear();
if (SUCCEEDED(hr))
{
*Cancel=TRUE;
}
}
}
}
// CHtmlView::OnNewWindow2(ppDisp, Cancel);
}
注意1:记得在CMIEView类中声明,不然会报错。
注意2:记得在View.cpp中:
包含#include <atlbase.h>
#include <Mshtml.h>
#include <COMDEF.H>