打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Multi Tab WebBrowser


Introduction


One web browser feature that is growing in popularity is the ability to open different tabs, or sub-pages, within the same browser window. Using tabs to separate multiple pages keeps the desktop tidy, while browsing multiple sites.


The web browser is also efficient for viewing multiple local files as it supports many files formats.


This code shows how to implement this feature, and also implements the following features: Print, Print Preview, Page properties, Options screen, Find Dialog, View page source, and more.


Implementing the Multi Tab Feature


This feature is mainly implemented by the NewWindow2 event.


The NewWindow2 event occurs when a new window is to be created for displaying a resource. This event occurs before a new window is created from the WebBrowser control. For example, this event occurs in response to a navigation that is targeted to a new window or to a scripted window.open method.


To specify that your browser program is to be used when a new window is opened, set the ppDisp parameter equal to the Application object. In this scenario, if a user chooses to open a web page in a new window, the new window in your program is used to display the new web page.


Additionally, set the RegisterAsBrowser property to true. This setting causes the new WebBrowser control to participate in window-name resolution. For example, if the window name is used elsewhere in the script, this control is used instead of a newly created one because the control examines all the existing window names before opening a new window.


On this event, we dynamically create a new instance of a tab page and a web browser as child by calling the CreateNewWebBrowser() method, where each web browser object has a tag object containing additional data on the corresponding web browser:

private void axWebBrowser1_NewWindow2(object sender,                 AxSHDocVw.DWebBrowserEvents2_NewWindow2Event e){  AxSHDocVw.AxWebBrowser _axWebBrowser = CreateNewWebBrowser();  e.ppDisp = _axWebBrowser.Application;  _axWebBrowser.RegisterAsBrowser = true;}private AxSHDocVw.AxWebBrowser CreateNewWebBrowser(){  AxSHDocVw.AxWebBrowser _axWebBrowser = new AxSHDocVw.AxWebBrowser();  _axWebBrowser.Tag = new HE_WebBrowserTag();  TabPage _TabPage = new TabPage();  _TabPage.Controls.Add(_axWebBrowser);  _axWebBrowser.Dock = DockStyle.Fill;  _axWebBrowser.BeforeNavigate2 += new            AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler(           this.axWebBrowser1_BeforeNavigate2);  _axWebBrowser.DocumentComplete += new            AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(           this.axWebBrowser1_DocumentComplete);  _axWebBrowser.NavigateComplete2 += new            AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(           this.axWebBrowser1_NavigateComplete2);  _axWebBrowser.NavigateError += new            AxSHDocVw.DWebBrowserEvents2_NavigateErrorEventHandler(           this.axWebBrowser1_NavigateError);  _axWebBrowser.NewWindow2 += new            AxSHDocVw.DWebBrowserEvents2_NewWindow2EventHandler(           this.axWebBrowser1_NewWindow2);  _axWebBrowser.ProgressChange += new            AxSHDocVw.DWebBrowserEvents2_ProgressChangeEventHandler(           this.axWebBrowser1_ProgressChange);  _axWebBrowser.StatusTextChange += new            AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEventHandler(           this.axWebBrowser1_StatusTextChange);  _axWebBrowser.TitleChange += new            AxSHDocVw.DWebBrowserEvents2_TitleChangeEventHandler(           this.axWebBrowser1_TitleChange);  _axWebBrowser.CommandStateChange += new            AxSHDocVw.DWebBrowserEvents2_CommandStateChangeEventHandler(           this.axWebBrowser1_CommandStateChange);  tabControl1.TabPages.Add(_TabPage);  tabControl1.SelectedTab = _TabPage;  return _axWebBrowser;}

Each instance of a web browser will have in its tag an HE_WebBrowserTag object that holds some details on that web browser object:

public class HE_WebBrowserTag{  public int _TabIndex = 0;  public bool _CanBack = false;  public bool _CanForward = false;}

Invoke the Find, View Source, and Options Dialog Boxes


Warning: this sample uses an undocumented command-group GUID that is subject to change in the future. Although this sample was tested to work correctly with Internet Explorer 6 and earlier, there is no guarantee that these techniques will continue to work successfully in future versions.


Define IOleCommandTarget in Visual C# .NET


To define a .NET interface to obtain a reference to a Component Object Model (COM) interface, follow these steps:



  1. Assign the interface the GUID of the appropriate COM interface.
  2. Include type declarations for all the methods of the interface.
  3. Include references to the Mshtml.dll file and the Shdocvw.dll file.

    To do this in your Visual C# .NET project, follow these steps:



    1. Click Add Reference on the Project menu.
    2. Click the COM tab.
    3. Double-click Microsoft HTML Object Library and Microsoft Internet Controls.

  4. Include the following interface declaration just before your application's namespace declaration to add a reference to the Microsoft HTML (MSHTML) IOleCommandTarget interface:
    using System;using System.Runtime.InteropServices;[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]public struct OLECMDTEXT{    public uint cmdtextf;    public uint cwActual;    public uint cwBuf;    [MarshalAs(UnmanagedType.ByValTStr,SizeConst=100)]public char rgwz;}[StructLayout(LayoutKind.Sequential)]public struct OLECMD{    public uint cmdID;    public uint cmdf;}// Interop definition for IOleCommandTarget.[ComImport,Guid("b722bccb-4e68-101b-a2bc-00aa00404770"),InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]public interface IOleCommandTarget{    //IMPORTANT: The order of the methods is critical here. You    //perform early binding in most cases, so the order of the methods    //here MUST match the order of their vtable layout (which is determined    //by their layout in IDL). The interop calls key off the vtable ordering,    //not the symbolic names. Therefore, if you     //switched these method declarations    //and tried to call the Exec method     //on an IOleCommandTarget interface from your    //application, it would translate     //into a call to the QueryStatus method instead.    void QueryStatus(ref Guid pguidCmdGroup, UInt32 cCmds,        [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)]         OLECMD[] prgCmds, ref OLECMDTEXT CmdText);    void Exec(ref Guid pguidCmdGroup, uint nCmdId, uint nCmdExecOpt,               ref object pvaIn, ref object pvaOut);}

Define the Command GUID for CGID_IWebBrowser


You must also define the GUID for CGI_IWebBrowser to inform MSHTML how to process your command IDs. Use the Microsoft .NET Framework class GUID to do this:

private Guid cmdGuid = new Guid("ED016940-BD5B-11CF-BA4E-00C04FD70816");private enum MiscCommandTarget { Find = 1, ViewSource, Options }

Call Exec()


Finally, encapsulate the calls to the Exec method in three separate method calls. Each call assumes the existence of a hosted instance of the WebBrowser control that is named webBrowser:

private mshtml.HTMLDocument GetDocument(){    try    {        mshtml.HTMLDocument htm = (mshtml.HTMLDocument)axWebBrowser2.Document;        return htm;    }    catch    {        throw (new Exception("Cannot retrieve" +                " the document from the WebBrowser control"));    }}public void ViewSource(){    IOleCommandTarget cmdt;    Object o = new object();    try    {        cmdt = (IOleCommandTarget)GetDocument();        cmdt.Exec(ref cmdGuid, (uint)MiscCommandTarget.ViewSource,        (uint)SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref o, ref o);    }    catch(Exception e)    {        System.Windows.Forms.MessageBox.Show(e.Message);    }}public void Find(){    IOleCommandTarget cmdt;    Object o = new object();    try    {        cmdt = (IOleCommandTarget)GetDocument();        cmdt.Exec(ref cmdGuid, (uint)MiscCommandTarget.Find,        (uint)SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref o, ref o);    }    catch(Exception e)    {        System.Windows.Forms.MessageBox.Show(e.Message);    }}public void InternetOptions(){    IOleCommandTarget cmdt;    Object o = new object();    try    {        cmdt = (IOleCommandTarget)GetDocument();        cmdt.Exec(ref cmdGuid, (uint)MiscCommandTarget.Options,        (uint)SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref o, ref o);    }    catch    {        // NOTE: Because of the way that this CMDID is handled in Internet Explorer,        // this catch block will always fire, even though the dialog box        // and its operations completed successfully. You can suppress this        // error without causing any damage to your host.    }}



     
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Implementing a PopUp blocker into a WebBrowse...
重写 WebBrowser 获取 网络连接错误信息
WebBrowser打开Word文档的一些注意事项
Visual C#开发浏览器过程浅析 - 51CTO.COM
在中WebBrowser加载Excel后获取excel对象
Using ActiveX Controls in .NET Applications | The Essentials for Using COM in Managed Code | InformI
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服