Contents - Index - Top


VB6 - Web Browser example

 

This example shows you how-to use the control as a basic browser.

 

First look at the Form_Load event.

 

Private Sub Form_Load()

    On Error Resume Next

    Me.Show

    tbToolBar.Refresh

    Form_Resize

 

    ' cboAddress.Move 50, lblAddress.Top + lblAddress.Height + 15

    EdgeWebBrowser.CreateWebView

    EdgeWebBrowser.EventsUseHexadecimal = True ' For VB we need to use the Hexadecimal event variants

    EdgeWebBrowser.UnlockControl "ExampleCompany", "WI5PO2-2KSU3Q-HWFXFU-IUMU2V-QF8P2F"

 

    If Len(StartingAddress) > 0 Then

        cboAddress.Text = StartingAddress

        cboAddress.AddItem cboAddress.Text

        'try to navigate to the starting address

        EdgeWebBrowser.Navigate StartingAddress

    End If

    cboAddress.AddItem "https://www.antwise.com"

    cboAddress.AddItem "https://www.antwise.com/Edge-WebView2-ActiveX.htm" ' will redirect to antview.dev

    cboAddress.AddItem "https://rijksmuseum.local/paintings.html" ' shows our local file paintings.html, see MapLocalFolderToHostName

    cboAddress.AddItem "https://todo.kasiban.com" ' stores data in the local browser indexed db, can test to see what happens if you delete the cache directory

    cboAddress.AddItem "https://badssl.com/download" ' stores data in the local browser indexed db, can test to see what happens if you delete the cache directory

    cboAddress.AddItem "https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core"

    cboAddress.AddItem "https://docs.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/"

End Sub

 

You always need to unlock the control with UnlockControl. You can do this globally if you have multiple AntView controls.

We're calling the CreateWebView method in order to instantiate the WebView2 control.

 

As Visual Basic doesn't support the Int64 datatype, we also set the property EventsUseHexadecimal to true so that we can use those events that have Int64 style datatypes.

There's also a bit on setting a few default URL's in the combo and a bit about resizing, which we're going to ignore for now.

 

That's the part on setting up the control. Now we can use it.

 

Private Sub MapLocalFolderToHostName()

  Dim Path As String

  Dim HostName As String

  

  Path = strGetVisualBasicDemoPath & "\html"

  HostName = "rijksmuseum.local"

  EdgeWebBrowser.SetVirtualHostNameToFolderMapping HostName, Path, hrakAllow

End Sub

 

Private Sub EdgeWebBrowser_OnCreateWebviewCompleted(ByVal HResult As Long)

  MapLocalFolderToHostName

End Sub

 

Once the WebView2 control is initialised, it will trigger the OnCreateWebViewCompleted event.

We are using that event here to setup a so called "virtual host name mapping". This is a technique where you map a local file path to a hostname. In our example we are mapping the folder "C:\Users\Public\Documents\AntView\Examples\Visual Basic\html" to hostname "rijksmuseum.local" so that we can access files in that folder using URL "https://rijksmuseum.local".

By setting this with SetVirtualHostNameToFolderMapping you can use features that normally won't be available with file based URI's.

 

The OnNavigationCompletedHex event is used to update the address combobox with the actual URL.

 

Private Sub EdgeWebBrowser_OnNavigationCompletedHex(ByVal IsSuccess As Boolean, ByVal WebErrorStatus As AntviewAx.TxWebErrorStatus, ByVal NavigationIdHex As String)

  '

  If IsSuccess Then

    cboAddress.Text = EdgeWebBrowser.Source

  End If

End Sub

 

We do something similar for the back/next buttons, but then in the OnHistoryChanged event:

 

Private Sub EdgeWebBrowser_OnHistoryChanged()

  tbToolBar.Buttons("Back").Enabled = EdgeWebBrowser.CanGoBack

  tbToolBar.Buttons("Forward").Enabled = EdgeWebBrowser.CanGoForward

End Sub

 

If we press "Enter" in the combo, or select an option from the address combo then it will nagivate to that page via the navigate method.

 

Private Sub cboAddress_Click()

    EdgeWebBrowser.Navigate cboAddress.Text

End Sub

 

Private Sub cboAddress_KeyPress(KeyAscii As Integer)

    On Error Resume Next

    If KeyAscii = vbKeyReturn Then

        cboAddress_Click

    End If

End Sub

 

From the toolbar, you can click on the print button and it will run the PrintCurrentDocument sub

 

Private Sub PrintCurrentDocument()

  Dim FilePath As String

  Dim Settings As String

  

  FilePath = "" ' Not specifying a path+filename will popup a file save as dialog

  Settings = "" ' JSON serialized string with options for settings

  ' We're going to add the following JSON:

  ' { "displayHeaderFooter" : true,

  '   "headerTitle" : "AntView PrintToPdf Demo",

  '   "printBackground" : true,

  '   "scale" : 0.5

  ' }

  Settings = "{"

  Settings = Settings & " ""displayHeaderFooter"": true, "

  Settings = Settings & " ""headerTitle"": ""AntView PrintToPdf Demo"", "

  Settings = Settings & " ""printBackground"": true, "

  Settings = Settings & " ""scale"": 0.5 "

  Settings = Settings & "}"

  EdgeWebBrowser.PrintToPdf FilePath, Settings

End Sub

 

to print the current page to Pdf via the PrintToPdf method.

You can supply a filepath to use, but as we didn't supply one, the control will ask where to save the Pdf via the standard save as windows file dialog.

 


AntView - The MS Edge WebView2 ActiveX control Date last changed: 09/25/2024