Creating a multi-page Web browser with Visual C #

Source: Internet
Author: User
Tags definition bool implement interface reference
visual| Browser | page | page

First, Introduction

As we all know, the current more popular web browsers such as Mozilla Firefox and MyIE2 have multiple page browsing function, each open a new page will automatically generate a new tab page, the page is also very easy to close. This design idea allows users to browse multiple Web pages when the desktop is very concise, but also to avoid the user waiting for a single page display distress. Because these browsers generally support the operation of multiple file formats, it is also extremely convenient to browse multiple files on the local machine.

This article uses Visual C # to detail how to implement this multiple page browsing feature. At the same time, also implemented the following additional functions: Print, Print preview, page properties, options, find, view the page source files, and so on.

Second, the key technical analysis

The key to solving the problem is programming the NewWindow2 event for the browser control webbrowser. When a new window is generated when a file needs to be displayed, the NewWindow2 event is activated. Note that this event occurs before a new window of the WebBrowser control is generated. For example, this event occurs as a response to a window.open method that navigates to a new window or a script control. In order to declare that when a new window is opened, we will use our own browser program, the parameter ppdisp should be set to the Application object. At this point, if you select "Open in a new window", a new window will be created to display the Web page. You can also set the RegisterAsBrowser to true, which will cause the newly generated WebBrowser control to participate in the window naming conflict issue. For example, if the name of a window is used at another point in the script, the control is useful instead of creating a new window because the control checks all existing window names before opening a new window to avoid naming conflicts. In the example of this article, as a response to this event, we dynamically create a tab page and generate a WebBrowser control as its child control by calling the Createnewwebbrowser () method- Each child control here has a tag property that contains information about the control. For details, please see the following source code:

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;

}
Note that each WebBrowser control has a tag, which I define as a simple class that contains the unique information associated with the control. Please see:

public class He_webbrowsertag
{
public int _tabindex = 0;
public bool _canback = FALSE;
public bool _canforward = FALSE;
}

Third, the implementation of the "find", "view page source file", "Options" dialog box features

Note This routine uses an undisclosed GUID that can be changed in future systems.

1. Define IOleCommandTarget interface

For the definition of one. NET interface to obtain a reference to a COM interface, follow these steps:

1) endowed. NET interface corresponding to the GUID value of the COM interface;

2 contains a type declaration for all methods in the interface;

3 contains references to Mshtml.dll and Shdocvw.dll files in Visual C #. NET project, please comply with the following:

A. Under the Project menu, click Add Reference;

B. Click the "COM" tab;

C. Double-click Microsoft HTML Object Library and Microsoft Internet Controls.

4 You should include the following interface declaration before the program namespace declaration to add a reference 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;
}

IOleCommandTarget Interop Definition

[ComImport,

Guid ("b722bccb-4e68-101b-a2bc-00aa00404770"),

InterfaceType (Cominterfacetype.interfaceisiunknown)]

public interface IOleCommandTarget
{
Important: The order of the following methods is important because we use early binding in this example, as described in MSDN for references to. net/com Interop.
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);
}
2. Define a GUID for Cgid_iwebbrowser

You must define the GUID of the Cgi_iwebbrowser to inform mshtml how to handle your command ID. Implemented in. NET as follows:

Private Guid Cmdguid = new GUID ("ed016940-bd5b-11cf-ba4e-00c04fd70816");
Private Enum Misccommandtarget {find = 1, Viewsource, Options}
3. Call EXEC () method

Note that the following three procedures successfully invoke exec () if there is already an contained instance of a browser control named WebBrowser.

Private MSHTML. HTMLDocument GetDocument ()
{
Try
{
Mshtml. HTMLDocument htm = (mshtml. HTMLDocument) axwebbrowser2.document;
return HTM;
}
Catch
{
Throw (New Exception ("Cannot get file object from WebBrowser control)");
}
}

How to view the source code

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);
}
}

Ways to display the Options dialog box

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 the corresponding cmdid of this procedure is handled in Internet Explorer
, the exception code block here will always be activated, even if the dialog box and its operation are successful.
Of course, you can use the browser to select settings to prevent this error from appearing.
However, even with this hint, there is no damage to your host.
}
}
Iv. Summary

In this paper, the basic principle of how to implement a multi page browsing program is described in detail through C #. Welcome my colleagues to criticize and correct me. In addition, the source program attached to this article is debugged on the Windows 2000/.net 2003/internet Explorer 6 platform.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.