I. Introduction
As we all know, popular Web browsers such as Mozilla Firefox and myie2 all have multi-page browsing functions. Each time a new page is opened, a new tab page is automatically generated, page closure is also very simple. This design concept makes the desktop very concise when users browse multiple web pages, and also avoids the distress of users waiting for a single page to display. Because these browsers generally support multiple file formats, it is extremely convenient to browse multiple files on a local machine.
This article uses Visual C # to describe how to implement this multi-page browsing function. At the same time, the following additional functions are also implemented: print, print preview, page attributes, options, search, view page source files, etc.
II. Key Technology Analysis
The key to solving the problem lies in programming the newwindow2 event of the browser control webbrowser. When a new window is generated by displaying a file, 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 the window. Open method that is directed to a new window or controlled by a script. To declare that when a new window is opened, we will use our own browser program. We should set the parameter ppdisp to the application object. If you select "open in New window", a new window is generated to display the web page. You can also set registerasbrowser to true, which causes the newly generated webbrowser control to participate in the conflict of window names. For example, if the name of a window is used in another part of the script, the control is used instead of generating a new window, the control checks all existing window names before opening a new window to avoid name conflicts. In this example, as a response to this event, we dynamically create a tab page and call createnewwebbrowser () method to generate a webbrowser control as its sub-control. Each sub-control has a tag attribute that contains information about the control. For details, see the source code below:
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. axwebbrowser=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 is defined as a simple class that contains unique information related to the control. See:
Public class he_webbrowsertag { Public int _ tabindex = 0; Public bool _ canback = false; Public bool _ canforward = false; } |
3. Implement functions such as "Search", "view page source file", and "option" dialog box.
Note that an undisclosed GUID is used in this routine and can be changed in future systems.
1. Define the iolecommandtarget Interface
To define a. net interface for reference on a COM interface, follow these steps:
1) Assign the guid value of the COM interface corresponding to the. NET interface;
2) contains the type declaration of all methods in the docking port;
3) include references to the mshtml. dll and shdocvw. DLL files. Follow these steps in the Visual C #. Net project:
A. Click "add reference" under the project menu ";
B. Click the "com" tab;
C. Double-click "Microsoft HTML Object Library" and "Microsoft Internet controls ".