Add web browsing features to our program

zhaozj2021-02-11  161

Add web browsing features to our program

There are a lot of articles to introduce how to join the browsing webpage in your own program, I have also made my own browser with VB. Most of the webbrowser controls and Internet Explorer Automation are used in SHDOCVW.DLL. SHDOCVW.DLL provides a COM interface that allows programmers to use WebBrowser controls and Internet Explorer Automation in their own programs. It also provides a series of Internet API functions to control Internet Explorer.

If you want to know what SHDOCvw.dll gives us, you can use the "Advanced Visual Basic Programming" (China Power Press) to browse the insider in ShDocvw.dll. You can also use the EXESCOPE resource editing tool to see what function in shdocvw.dll.

IE basic architecture (taken from "programmers" special issue)

IEXPLORER.EXESHDOCVW.DLL-WEBBROWSER CONTROL AND INTERNET EXPLORER AUTOMATION page displaying MSHTML.DLL - MSHTML, page parsing process, but also a COM server, the page elements defined as objects in HTML, the client access HTMLACTIVEX CONTROLACTIVEX SCRIPT ENGINEJAVA AppleTPlug in

There is a detailed help introduce WebBrowser control and Internet Explorer Automation in MSDN. Their properties, methods, and events are mostly the same, some attributes and methods WebBrowser controls are ignored. SHDOCVW.DLL provides a means to add web browsing features to our program or control an Internet Explorer instance. Here are some techniques I use in the app, I give you a reference in the form of code.

First, toolbar

BRWWebBrowser is an instance of a WebBrowser control, and the CommandStateChange event enables the forward and backwards in the toolbar.

Private sub brwwebbrowser_commandstatechange (Byval Command as long, byval enable as boolean)

SELECT CASE COMMAND

Case CSC_UpdateCommands

'Me.tbtoolbar.buttons (1) .enabled = enable

'Me.tbtoolbar.buttons (2) .enabled = enable

Case CSC_naviGateForward

The active state of the forward button of the toolbar

Me.TbtoolBar.Buttons (2) .enabled = enable

'The effective state change of the back button of the toolbar

Case CSC_naviGateBack

Me.TbtoolBar.Buttons (1) .enabled = enable

Case Else

End SELECT

End Sub

Navigate with WebBrowser methods

Private sub tbtoolbar_buttonclick (byval Button as mscomctllib.button)

ON Error ResMe next

Select Case Button.Key

Case "back"

BRWWEBBROWSER.GOBACK '

Case "forward"

BRWWEBBROWSER.GOFORWARD 'advancement

Case "refresh" brwwebbrowser.refresh 'refreshes

Case "home"

BRWWEBBROWSER.GOHOME 'to Home

Case "Search"

Me.TbtoolBar.Buttons ("htmlclass"). Value = TBRUNPRESSED

Me.TbtoolBar.Buttons ("History"). Value = TBRUNPRESSED

If Button.Value = TBRPRESSED THEN

Me.brwasearch.visible = true

Me.brwsearch.gosearch

m_blnissplitter = true

Else

Me.brwasearch.visible = false

Me.brwsearch.gosearch

m_blnissplitter = false

END IF

Me.uctlhibLhistroy1.visible = false

Me.uctorlclassurl1.visible = false

Call resizeControls (m_blnissplitter, me.imgsplitter.Left)

Case "stop"

BRWWEBBROWSER.STOP

Me.caption = brwwebbrowser.locationname & "-" & structuRentuserName

Case "htmlclass"

'Ifeton.value = TBRPRESSED THEN

'Me.tbtoolbar.buttons ("history"). Value = TBRUNPRESSED

'Me.tbtoolbar.Buttons ("Search"). Value = TBRUNPRESSED

'

'M_blnissplitter = TRUE

'Me.uctlclassurl1.visible = true

'Me.uctlhistroy1.visible = false

'

'Me.uctlclassurl1.buildtree (Normal)

'

'Else

'M_blnissplitter = false

'Me.uctlclassurl1.visible = false

'Me.uctlhistroy1.visible = false

'End if

'Call resizeControls (m_blnissplitter, me.imgsplitter.Left)

Call mnumanclass_click

Case "History"

'Ifeton.value = TBRPRESSED THEN

'Me.tbtoolbar.buttons ("htmlclass"). Value = TBRUNPRESSED

'Me.tbtoolbar.buttons ("search"). Value = TBRUNPRESSED'

'M_blnissplitter = TRUE

'Me.uctlhibLhistroy1.visible = TRUE

'Me.uctlclassurl1.visible = false

'Me.uctlhistroy1.buildtree (0)

'Else

'M_blnissplitter = false

'Me.uctlhistroy1.visible = false

'Me.uctlclassurl1.visible = false

'Me.uctlhistroy1.buildtree (0)

'End if

'Call resizeControls (m_blnissplitter, me.imgsplitter.Left)

'

Call mnumanhistory_click

Case "Printout"

BRWWEBBROWSER.SETFOCUS

ON Error ResMe next

BRWWEBBROWSER.EXECWB OLECMDID_PRINT, OLECMDEXECOPT_DODEFAULT 'Print

Case "status"

'M_blnstatusbarshow = cbool (Button.Value)

Me.tbtoolbar.customize

'Me.tbtoolbar.savetoolbar

Case "Help"

Call MNUHELPABOUT_CLICK

Case "exit"

Call MnufileClose_Click

Case Else

EXIT SUB

End SELECT

End Sub

(I am sorry that there are many garbage code above.)

Second, the status bar

The ProgressChange event of the webbrowser control is used to display a progress bar; the statustextChange event updates the information of the status bar pane, reflects the status of the WebBrowser control.

Private sub bwwebbrowser_downloadbegin ()

ProgressShow True

End Sub

Sub ProgressShow (visible as boolean) 'shows a progress bar

Me.sbrhtml.Panels (2) .visible = Visible

Progress1.visible = visible

IF Visible Then Progress1.move Sbrhtml.Panels (2) .left 10, Sbrhtml.top (Sbrhtml.height - SBRHTML.HEIGHT) / 2 10, SBRHTML.PANELS (2) .width - 20

End Sub

Private sub brwwebbrowser_progresschange (Byval ProgressMax as long)

ON Error ResMe next

Progress1.max = progressMaxif Progress> 0 THEN

Progress1.value = progress

Else

Progress1.value = ProgressMax

END IF

End Sub

Private sub brwwebbrowser_statustextchange (Byval Text As String)

Me.sbrhtml.Panels (1) .text = text

Me.sbrhtml.refresh

End Sub

Private sub bwwebbrowser_downloadcomplete ()

ON Error ResMe next

Me.caption = brwwebbrowser.locationname

Me.cboaddress = me.brwwebbrowser.locationURL 'address bar's current address

ProgressShow False

End Sub

Third, address bar

PRIVATE MBDONTNAVIGATENOW AS Boolean 'Is the variable in the navigation status

Private sub cboaddress_click () 'Select the line in the drop-down list

IF MbdontnaVigatenow Then Exit Sub

BRWWEBBROWSER.NAVIGATE CBOADDRESS.TEXT 'Navigate to the address in the drop-down list text

End Sub

Private sub cboaddress_KeyPress (Keyascii AS Integer)

ON Error ResMe next

If keyascii = vbkeyreturn then 'inputs the address in the drop-down list

CBoaddress_Click

END IF

End Sub

The NavigateComplete2 event adds the navigation address to the drop-down list (if there is nothing in the list).

Private sub brwwebbrowser_navigatecomplete2 (Byval PDISP As Object, URL AS VARIANT)

'On Error Resume Next

DIM I as integer

Dim bfound as boolean

DIM strTemp () AS STRING

Me.caption = brwwebbrowser.locationname

Find whether the address is already in the list

For i = 0 to cboaddress.listcount - 1

If CBOADDRESS.LIST (i) = brwwebbrowser.locationURL THEN

bFound = true

EXIT for

END IF

Next i

MbdontnaVigatenow = TRUE

If bfound kilove

CBoaddress.RemoveItem I 'removal

END IF

CBoaddress.AddItem BRWWEBBBROWSER.LOCATIONURL, 0 'Add

CBoaddress.listIndex = 0

MbdontnaVigatenow = false

End Sub

Fourth, menu

The ExecwB method for WebBrowser control and Internet Explorer Automation provides a lot of commands to execute to the user, and the command acts on the OLE object. However, there are many commands to perform invalid for WebBrowser controls. Please see MSDN.

Private sub mnuedigcut_click ()

BRWWEBBROWSER.SETFOCUSON ERROR RESUME NEXT

BRWWEBBROWSER.EXECWB OLECMDID_CUT, OLECMDEXECOPT_DODEFAULT

'Cut

End Sub

Private sub mnueditcopy_click ()

ON Error ResMe next

BRWWEBBROWSER.SETFOCUS

BRWWEBBROWSER.EXECWB OLECMDID_COPY, OLECMDEXECOPT_DODEFAULT

'copy

End Sub

Private sub mnueditfind_click ()

ON Error ResMe next

BRWWEBBROWSER.SETFOCUS

BRWWEBBROWSER.EXECWB OLECMDID_FIND, OLECMDEXECOPT_DODEFAULT

'Find, (invalid)

End Sub

Private sub mnueDitpaste_click ()

ON Error ResMe next

BRWWEBBROWSER.SETFOCUS

BRWWEBBROWSER.EXECWB OLECMDID_PASTE, OLECMDEXECOPT_DODEFAULT

'Paste

End Sub

Private sub mnueditslectedall_click ()

BRWWEBBROWSER.SETFOCUS

BRWWEBBROWSER.EXECWB OLECMDID_SELECTALL, OLECMDEXECOPT_DODEFAULT

'select all

End Sub

Private sub mnufileAttrib_click ()

Me.brwwebbrowser.setfocus

ON Error ResMe next

BRWWEBBROWSER.EXECWB OLECMDID_PROPERTIES, OLECMDEXECOPT_DODEFAULT

'Display the properties of the web page

End Sub

Private sub mnufilenew_click ()

DIM FRMNEW AS New frMmainexPloer 'New Window

Frmnew.show

Set frmnew = nothing

End Sub

Private sub mnufileopen_click ()

'Brwwebbrowser.setfocus

'On Error Resume Next

'Brwwebbrowser.execwb olecmdid_open, OLECMDEXECOPT_DODEFAULT

'turn on

'The following is open from the original way

DIM SFILE AS STRING

With dlgcommondialog

.Dialogtitle = "Open the web"

.Cancelerror = false

'Todo: Set the flag and properties of the CommON Dialog control

.Filter = "HTML file (* .html, *. Htm) | * .html; * HTM | text file (* .txt) | * .txt | ASP file (* .asp) | * .asp" & _

"| Graphic file (* .bmp; *. Jpg; *. Jpeg; *. Gif) | * .bmp; *. Jpg; *. Jpeg; *. Gif | all files (*. *) | *. *"

.Showopen

If len (.filename) = 0 THEN

EXIT SUB

END IF

sfile = .filename

End with

'Todo: Add the code that handles the file opened

BRWWEBBROWSER.NAVIGATE SFILE

End Sub

Private sub mnufileprint_click () BRWWebBrowser.setfocus

ON Error ResMe next

BRWWEBBROWSER.EXECWB OLECMDID_PRINT, OLECMDEXECOPT_DODEFAULT

'print

End Sub

Private sub mnufilesave_click ()

BRWWEBBROWSER.SETFOCUS

ON Error ResMe next

BRWWEBBROWSER.EXECWB OLECMDID_SAVE, OLECMDEXECOPT_DODEFAULT

'save

'The following is to save the web with the original way

'Dim Sfile As String

'

'With dlgcommondialog

'.Dialogtitle = "save"

'.Filter = "html file (* .html, *. Htm) | * .html; * HTM | text file (* .txt) | * .txt | ASP file (* .asp) | * .asp" & _

'"| Graphic file (* .bmp; *. Jpg; *. JPEG; *. Gif) | * .bmp; *. Jpg; *. Jpeg; *. Gif | All files (*. *) | *. * "

'.Showsave

'End with

End Sub

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

New Post(0)