Visual C # Create multi-page web browser

xiaoxiao2021-03-31  206

Introduction

Everyone knows that there is currently a popular web browser such as Mozilla Firefox and Myie2, with multi-page browsing feature, each open a new page automatically generates a new tab page, and the shutdown of the page is very simple. This design idea makes the user very simple when browsing multiple web pages, and avoids the user's distress waiting for a single page. Since these browsers generally support operation of multiple file formats, it is also very convenient when browsing multiple files on the local machine.

This article uses Visual C to describe how to implement this multi-page browsing feature. At the same time, the following additional features are also implemented: print, print preview, page properties, options, lookups, view page source files, etc.

Key technical analysis

The key to solving the problem is the programming of the NewWindow2 event for the browser control webbrowser. When a file is required to generate a new window, the NewWindow2 event is activated. Note that the event occurs before the new window of the WebBrowser control. For example, the event occurs as a response to a new window or a new window or a script-controlled Window.Open method. To declare that when a new window is opened, we will use our own browser programs to set the parameter PPDISP as an Application object. At this point, if you choose

"

Open in a new window

"

Then, a window is generated 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 conflict of the window named. For example, if the name of a window is used in another place in the script, then the control is sent to the field, not a new window, because the control checks all existing windows before opening a new window. Name to avoid naming conflicts. In this example, as a response to the event, we dynamically create a TAB page, and generate a webbrowser control as its sub-control by calling the CreateNewwebBrowser () method - each sub-control has a control related information. TAG attribute. See the source code below for details:

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.DWEBBBROWSEREVENTS2_BEFORENAVIGATE2EVENTHANDLER

(

THIS

.axwebbrowser1_beforenavigate2;

_axwebbrowser.documentcomplete

=

New

AXSHDOCVW.DWEBBBBBROWSEREVENTS2_DocumentCompleteEventhandler

(

THIS

.axwebbrowser1_documentcomplete);

_axwebbrowser.navigateComplete2

=

New

Axshdocvw.dwebbrowserevents2_navigatecomplete2eventhandler

(

THIS

.axwebbrowser1_navigateComplete2);

_axwebbrowser.navigateerror

=

New

Axshdocvw.dwebbrowserevents2_navigateerRoreventhandler

(

THIS

.axwebbrowser1_navigate;

_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_titlechangeeeventhandler

(

THIS

.axwebbrowser1_titlechange);

_axwebbrowser.commandStateChange

=

New

AXSHDOCVW.DWEBBBROWSEREVENTS2_COMMANDSTATECHANGEVENTHANDLER

(

THIS

.axwebbrowser1_commandstatechange);

TabControl1.Tabpages.Add (_tabpage);

TabControl1.selectedTAB

=

_Tabpage;

Return

_axwebbrowser;

}

Note that every WebBrowser control has a tag, which is defined as a simple class, which is used to contain some of the unique information related to this control. Please see:

public

Class

HE_WebBrowsertag

{

public

int

_Tabindex

=

0

;

public

Bool

_CANBACK

=

False

;

public

Bool

_CANFORWARD

=

False

;

}

achieve

"

Look up

"

,

"

View page source files

"

,

"

Option

"

Dialog, etc.

A unprecedented GUID is used in this routine, which can change in the future system.

1, define the IOLCOMMANDTARGET interface

To define a .NET interface to get a reference about a COM interface, follow these steps:

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

2) Contains the type declaration of all methods in the interface;

3) Contains a reference to MSHTML.DLL and SHDOCVW.DLL files, in Visual C .NET project, follow: a. Click on the project menu

"

Add a reference

"

;

B. Click

"

COM

"

Tab;

C. Double click

"

Microsoft HTML Object Library

"

with

"

Microsoft Internet Controls

"

.

4) Before the program namespace declaration, the following interface declaration should be added to add reference references to Microsoft HTML (MSHTML) IOLCOMMANDTARGET 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

charr

RGWZ;

}

[Structlayout (layoutkind.sequential)]

public

Struct

OLECMD

{

public

Uint

CMDID;

public

Uint

CMDF;

}

//

Interop definition of IOLCOMMANDTARGET

[ComImport,

GUID

"

B722BCCB-4E68-101B-A2BC-00AA00404770

"

),

InterfacePe (CominterFactype.Interfaceisiunknown)]

public

Interface

IOLCOMMANDTARGET

{

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

The GUID of the CGI_iWebBrowser must be defined 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 the exec () method

Note that the following three processes successfully calls the premise of Exec (), there is already an inclusive instance of the browser control called WebBrowser.

Private

mshtml.htmldocument getDocument ()

{

Try

{

Mshtml.htmldocument HTM

=

(mshtml.htmldocument) AXWebBrowser2.document;

Return

HTM;

}

Catch {

Throw

(

New

EXCEPTION

"

You cannot get file objects from the webbrowser control

"

));

}

}

//

View the source code method

public

Void

ViewSource ()

{

IOLCMMANDTARGET 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 ()

{

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

}

}

//

Displays the method of "Options" dialog

public

Void

InternetOptions ()

{

IOLCMMANDTARGET 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 process corresponding to the CMDID is in Internet Explorer Processing

//

So the exception code block here will always be activated, even if the dialog is successful.

//

Of course, you can choose settings through the browser to ban such errors.

//

However, even if this prompt has, there is no harm to your host.

}

}

to sum up

This article uses VC # compiled, detail how to implement a multi-page browsing program. Welcome colleagues criticism.

转载请注明原文地址:https://www.9cbs.com/read-130860.html

New Post(0)