WebView2 Control Advanced Features Reference
Process Management
BrowserProcessId Property
- Description: Get the browser process ProcessID
- Type: Long
- Example:
vb
' Get and display browser process ID
Private Sub ShowBrowserProcessInfo()
Dim pid As Long
pid = WebView21.BrowserProcessId
Debug.Print "Browser process ID: " & pid
End Sub
ProcessFailed Event Handling
- Description: Handle failures in WebView2-related processes
- Example:
vb
Private Sub WebView21_ProcessFailed(ByVal Kind As wv2ProcessFailedKind)
Select Case Kind
Case wv2ProcessFailedKind.wv2ProcessFailedKindBrowserProcessExited
' Browser process exit
HandleBrowserCrash
Case wv2ProcessFailedKind.wv2ProcessFailedKindRenderProcessExited
' Render process exit
HandleRenderProcessCrash
Case wv2ProcessFailedKind.wv2ProcessFailedKindFrameRenderProcessExited
' Frame render process exit
HandleFrameProcessCrash
End Select
End Sub
Private Sub HandleBrowserCrash()
' Log error
LogError "Browser process crashed"
' Attempt recovery
If MsgBox("Browser process has terminated. Reload page?", _
vbQuestion + vbYesNo) = vbYes Then
WebView21.Reload
End If
End Sub
Private Sub HandleRenderProcessCrash()
' Log error
LogError "Render process crashed"
' Show error page
WebView21.LoadHtml "<html><body>" & _
"<h1>Page Rendering Failed</h1>" & _
"<p>Please refresh the page to try again.</p>" & _
"</body></html>"
End Sub
Suspend and Resume
Suspend/Resume Support Check
- Description: Check if suspend and resume functionality is supported
- Property:
SupportsSuspendResumeFeatures As Boolean
- Example:
vb
' Check for suspend/resume support
Private Function CanUseSuspendFeatures() As Boolean
CanUseSuspendFeatures = WebView21.SupportsSuspendResumeFeatures
End Function
Suspend Method
- Description: Suspend WebView2 processing and rendering
- Purpose: Used for implementing tabs, reducing resource usage of inactive tabs
- Example:
vb
' Suspend current tab
Private Sub SuspendTab()
If WebView21.SupportsSuspendResumeFeatures Then
WebView21.Suspend
End If
End Sub
Resume Method
- Description: Resume previously suspended WebView2 processing and rendering
- Example:
vb
' Resume suspended tab
Private Sub ResumeTab()
If WebView21.SupportsSuspendResumeFeatures Then
WebView21.Resume
End If
End Sub
IsSuspended Property
- Description: Get whether WebView2 is in suspended state
- Type: Boolean
- Example:
vb
' Check and handle suspension state
Private Sub HandleTabActivation()
If WebView21.SupportsSuspendResumeFeatures Then
If WebView21.IsSuspended Then
' Tab activated, resume processing
WebView21.Resume
End If
End If
End Sub
Developer Tools
OpenDevToolsWindow Method
- Description: Open the Developer Tools window
- Requirement: AreDevToolsEnabled must be True
- Example:
vb
' Open Developer Tools window
Private Sub cmdDevTools_Click()
If WebView21.AreDevToolsEnabled Then
WebView21.OpenDevToolsWindow
End If
End Sub
' Auto-open Developer Tools in debug mode
Private Sub WebView21_Ready()
#If DEBUG Then
If WebView21.AreDevToolsEnabled Then
WebView21.OpenDevToolsWindow
End If
#End If
End Sub
CallDevToolsProtocolMethod Method
- Description: Send DevTools Protocol messages
- Syntax:
CallDevToolsProtocolMethod(MethodName As String, ParamsAsJson As String, Optional CustomEventId As Variant)
- Example:
vb
' Disable JavaScript execution
WebView21.CallDevToolsProtocolMethod "Emulation.setScriptExecutionDisabled", "{""value"":true}"
' Set device emulation
Private Sub EmulateIPhone()
Dim params As String
params = "{" & _
"""width"": 375," & _
"""height"": 812," & _
"""deviceScaleFactor"": 3," & _
"""mobile"": true," & _
"""scale"": 1" & _
"}"
WebView21.CallDevToolsProtocolMethod "Emulation.setDeviceMetricsOverride", params
End Sub
' Call with callback
Private Sub GetDOMDocument()
WebView21.CallDevToolsProtocolMethod "DOM.getDocument", "{}", "GetDOM"
End Sub
Private Sub WebView21_DevToolsProtocolResponse(ByVal CustomEventId As Variant, _
ByVal JsonResponse As String)
If CustomEventId = "GetDOM" Then
Debug.Print "DOM structure: " & JsonResponse
End If
End Sub
Resource Handling
AddWebResourceRequestedFilter / RemoveWebResourceRequestedFilter Methods
- Description: Add/Remove Web resource request filters
- Syntax:
AddWebResourceRequestedFilter(Filter As String, Context As wv2WebResourceContext)
RemoveWebResourceRequestedFilter(Filter As String, Context As wv2WebResourceContext)
- Example:
vb
' Intercept all image requests
Private Sub InterceptImages()
' Add image filters
WebView21.AddWebResourceRequestedFilter "*.jpg", wv2WebResourceContext.wv2WebResourceContextImage
WebView21.AddWebResourceRequestedFilter "*.png", wv2WebResourceContext.wv2WebResourceContextImage
WebView21.AddWebResourceRequestedFilter "*.gif", wv2WebResourceContext.wv2WebResourceContextImage
End Sub
' Handle resource requests
Private Sub WebView21_WebResourceRequested(ByVal Request As WebView2Request, _
ByVal Response As WebView2Response)
' Check if it's an ad image
If InStr(1, Request.URI, "ad-") > 0 Then
' Return blank image
Response.Content = LoadBlankImage()
Response.Headers = "Content-Type: image/png"
End If
End Sub
' Stop intercepting
Private Sub StopIntercepting()
WebView21.RemoveWebResourceRequestedFilter "*.jpg", wv2WebResourceContext.wv2WebResourceContextImage
WebView21.RemoveWebResourceRequestedFilter "*.png", wv2WebResourceContext.wv2WebResourceContextImage
WebView21.RemoveWebResourceRequestedFilter "*.gif", wv2WebResourceContext.wv2WebResourceContextImage
End Sub
PDF Export
PrintToPdf Method
- Description: Save current page as PDF file
- Syntax:
PrintToPdf(OutputPath As String, [optional parameters])
- Example:
vb
' Basic PDF export
Private Sub ExportSimplePdf()
Dim pdfPath As String
pdfPath = App.Path & "\export.pdf"
WebView21.PrintToPdf pdfPath
End Sub
' Custom PDF export
Private Sub ExportCustomPdf()
Dim pdfPath As String
pdfPath = App.Path & "\custom.pdf"
' Custom PDF export options
WebView21.PrintToPdf pdfPath, _
wv2PrintOrientation.wv2PrintLandscape, _ ' Landscape orientation
1.0, _ ' Scale
210, _ ' Page width (mm)
297, _ ' Page height (mm)
20, _ ' Top margin (mm)
20, _ ' Bottom margin (mm)
20, _ ' Left margin (mm)
20, _ ' Right margin (mm)
True, _ ' Print backgrounds
False, _ ' Don't print selection only
True, _ ' Show headers and footers
"My Document", _ ' Header title
WebView21.DocumentURL ' Footer URL
End Sub
' Handle PDF export results
Private Sub WebView21_PrintToPdfCompleted()
MsgBox "PDF export successful!", vbInformation
End Sub
Private Sub WebView21_PrintToPdfFailed()
MsgBox "PDF export failed", vbCritical
End Sub
Multi-Window Support
Tab and New Window Handling
- Description: Handle new window requests and tab management
- Example:
vb
' Handle new window requests
Private Sub WebView21_NewWindowRequested(ByVal IsUserInitiated As Boolean, _
ByRef IsHandled As Boolean, ByVal Uri As String, _
ByVal HasPosition As Long, ByVal HasSize As Long, _
ByVal Left As Long, ByVal Top As Long, _
ByVal Width As Long, ByVal Height As Long, _
ByVal ShouldDisplayMenuBar As Long, _
ByVal ShouldDisplayStatus As Long, _
ByVal ShouldDisplayToolbar As Long, _
ByVal ShouldDisplayScrollBars As Long)
' Implement tab support
If IsUserInitiated Then
CreateNewTab Uri
IsHandled = True
End If
End Sub
' Example new tab implementation
Private Sub CreateNewTab(ByVal Url As String)
' Create new WebView2 control
Dim newTab As WebView2
Set newTab = Controls.Add("WebView2Control.WebView2", "newTab" & TabCount)
' Configure new tab
With newTab
.Move TabContainer.Left, TabContainer.Top, TabContainer.Width, TabContainer.Height
.Visible = True
.Navigate Url
End With
' Update tab interface
AddTabButton Url
TabCount = TabCount + 1
End Sub
Touch Support
IsPinchZoomEnabled Property
- Description: Control whether pinch-to-zoom is enabled
- Requirement: Requires SupportsPinchZoomFeatures support
- Example:
vb
' Check and enable touch zoom
Private Sub EnableTouchFeatures()
If WebView21.SupportsPinchZoomFeatures Then
WebView21.IsPinchZoomEnabled = True
End If
End Sub
IsSwipeNavigationEnabled Property
- Description: Control whether swipe navigation is enabled
- Requirement: Requires SupportsSwipeNavigationFeatures support
- Example:
vb
' Configure touch navigation
Private Sub ConfigureTouchNavigation()
If WebView21.SupportsSwipeNavigationFeatures Then
' Enable swipe navigation
WebView21.IsSwipeNavigationEnabled = True
End If
End Sub
' Complete touch support configuration
Private Sub SetupTouchSupport(ByVal EnablePinchZoom As Boolean, _
ByVal EnableSwipe As Boolean)
If WebView21.SupportsPinchZoomFeatures Then
WebView21.IsPinchZoomEnabled = EnablePinchZoom
End If
If WebView21.SupportsSwipeNavigationFeatures Then
WebView21.IsSwipeNavigationEnabled = EnableSwipe
End If
End Sub