VC,webbrowser控件中弹出新网页窗口

用webbrowser控件浏览网页时,常弹出新的网页窗口,若不做任何控制的话,会在默认浏览器(一般是IE)中打开,这样就在新的窗口打开了,原程序就很难控制了,且存在webbrowser控件和IE的session共享问题。解决方法是让新打开的网页在自己的对话框中打开,具体步骤如下:



一、新建基于对话框的MFC程序,对话框ID为IDD_SHELL_DIALOG,添加Microsoft Web Browser控件(ID为IDC_EXPLORER),并利用向导生成IDC_EXPLORER的控件型变量m_webBrowser,Navigate()就可以连接要浏览的网页了。







[cpp] view plaincopy

VARIANT vInfo;

VariantInit(&vInfo);

m_webBrowser.Navigate("http://×××", &vInfo, &vInfo, &vInfo, &vInfo);





二、要为弹出的网页也新建一个对话框(ID为IDD_DIALOG_NEW),利用向导添加类生成CNewDlg类( : public CDialog)。



并在该对话框中添加Microsoft Web Browser控件(ID为IDC_EXPLORER_NEW),



利用向导生成IDC_EXPLORER的控件型变量m_newwebBrowser,



三、在原对话框(IDD_SHELL_DIALOG)中添加webbrowser控件(IDC_EXPLORER)的NewWindow3()事件,当事件触发时,显示新对话框,并用新的对话框中的webbrowser控件(IDC_EXPLORER_NEW)打开URL地址就行了



[cpp] view plaincopy

void CShellDlg::NewWindow3Explorer(LPDISPATCH* ppDisp, BOOL* Cancel, unsigned long dwFlags, LPCTSTR bstrUrlContext, LPCTSTR bstrUrl)

{

// TODO: 在此处添加消息处理程序代码

* Cancel = TRUE;

VARIANT vInfo;

CNewDlg * newdlg = new CNewDlg;

newdlg->Create(IDD_DIALOG_NEW);

newdlg->ShowWindow(SW_SHOW);

newdlg->m_newwebBrowser.Navigate(bstrUrl, &vInfo, &vInfo, &vInfo, &vInfo);

}





四、万一在新窗口中操作又产生新窗口,咋办?那就仿照第三步在新对话框(IDD_DIALOG_NEW)中添加webbrowser控件(IDC_EXPLORER_NEW)的NewWindow3()事件,反正新窗口是new的,只要一个模板就行了,不必再添加新对话框了。



[cpp] view plaincopy

void CNewDlg::NewWindow3ExplorerNew(LPDISPATCH* ppDisp, BOOL* Cancel, unsigned long dwFlags, LPCTSTR bstrUrlContext, LPCTSTR bstrUrl)

{

// TODO: 在此处添加消息处理程序代码

* Cancel = TRUE;

VARIANT vInfo;

CNewDlg * newdlg = new CNewDlg;

newdlg->Create(IDD_DIALOG_NEW);

newdlg->ShowWindow(SW_SHOW);

newdlg->m_newwebBrowser.Navigate(bstrUrl, &vInfo, &vInfo, &vInfo, &vInfo);

}

注意:我把下面这些代码去掉之后就可以了

VARIANT vInfo;

CNewDlg * newdlg = new CNewDlg;

newdlg->Create(IDD_DIALOG_NEW);

newdlg->ShowWindow(SW_SHOW);

newdlg->m_newwebBrowser.Navigate(bstrUrl, &vInfo, &vInfo, &vInfo, &vInfo);

此时程序可以编译过,但是当打开新窗口时,并不会进入到OnNewWindow3Explorer1函数中;经过分析发现把事件定义稍微改一下即可

ON_EVENT(CTestIeDlg, IDC_EXPLORER1, 273 /* NewWindow3 */, OnNewWindow3Explorer1, VTS_PDISPATCH VTS_PBOOL VTS_BSTR VTS_BSTR)
改为下面内容:

ON_EVENT(CTestDlg, IDC_EXPLORER1, 273 /* NewWindow3 */, OnNewWindow3Explorer1, VTS_PDISPATCH VTS_PBOOL VTS_I4 VTS_BSTR VTS_BSTR)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Chrome based Browser Engine for .NET EO.WebBrowser is a web browser engine based on Google's Chromium project but with native .NET programming interface --- don't worry, it's not a wrapper around the Chrome browser installed on your machine. In fact EO.WebBrowser has the whole browser engine embedded inside a single .NET DLL. In another word, it has zero external dependency. Just reference and use. Why use EO.WebBrowser? • Based on Google's Chrome Project EO.WebBrowser uses the same core Google's Chrome and Apple Safari uses. It does not rely on IE. The engine is much faster and safer. • Zero External Dependency What if user updates/uninstall their browser? What if user disables JavaScript in Internet Explorer's settings dialog? These questions does not exist with EO.WebBrowser because everything is embedded inside our DLL files. • Native .NET components written in C# Because it's written in .NET, you can use it with any .NET based language/development tool. The same DLL works for both 32 bit and 64 bit environments; • Easy to use Programming Interface EO.WebBrowser offers core components that can be used in any Windows application, as well as wrapper controls for both Windows Forms applications and WPF applications; • Extensive Customization Options EO.WebBrowser offers extensive customization options that allow you to customize context menu, hot keys, JavaScript dialogs, file dialogs, focus and window control. Together these features allow you to seamlessly integrate the browser engine into your application; • .NET code -> JavaScript code Turn any web page into an integral part of your application -- both visually and programmatically. You can execute JavaScript code and access all the JavaScript objects directly from your .NET code. Access their properties or even call a JavaScript function are all different options available to you; • JavaScript code -> .NET code Things always go both ways --- and this is reflected in our programming interface as well. You can call JavaScript code from .NET code, and the other way around is also true --- you can call .NET code from your JavaScript code. This allows your Web page to seamlessly interact with the host application; • Custom Resource Handler Want to keep an eye on everything? Or want to keep everything to yourself? We got you covered. EO.WebBrowser offers ability to intercept and modify all requests that originate from the browser engine. For example, you can automatically deny all request sent to a specific host. It also offers you the ability to implement custom protocols or custom resource handlers. For example, you can implement a custom request handler to load images from your database instead of a Web server;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值