WebView2 控件高级功能参考
开发者工具
OpenDevToolsWindow 方法
- 描述:打开开发者工具窗口
- 要求:AreDevToolsEnabled 必须为 True
- 示例:
vb
' 打开开发者工具窗口
Private Sub cmdDevTools_Click()
If WebView21.AreDevToolsEnabled Then
WebView21.OpenDevToolsWindow
End If
End Sub
' 在调试模式下自动打开开发者工具
Private Sub WebView21_Ready()
#If DEBUG Then
If WebView21.AreDevToolsEnabled Then
WebView21.OpenDevToolsWindow
End If
#End If
End Sub
CallDevToolsProtocolMethod 方法
- 描述:发送 DevTools 协议消息
- 语法:
CallDevToolsProtocolMethod(MethodName As String, ParamsAsJson As String, Optional CustomEventId As Variant)
- 示例:
vb
' 禁用JavaScript执行
WebView21.CallDevToolsProtocolMethod "Emulation.setScriptExecutionDisabled", "{""value"":true}"
' 设置设备模拟
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
' 带回调的调用
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结构:" & JsonResponse
End If
End Sub
资源处理
AddWebResourceRequestedFilter / RemoveWebResourceRequestedFilter 方法
- 描述:添加/移除 Web 资源请求过滤器
- 语法:
AddWebResourceRequestedFilter(Filter As String, Context As wv2WebResourceContext)
RemoveWebResourceRequestedFilter(Filter As String, Context As wv2WebResourceContext)
- 示例:
vb
' 拦截所有图片请求
Private Sub InterceptImages()
' 添加图片过滤器
WebView21.AddWebResourceRequestedFilter "*.jpg", wv2WebResourceContext.wv2WebResourceContextImage
WebView21.AddWebResourceRequestedFilter "*.png", wv2WebResourceContext.wv2WebResourceContextImage
WebView21.AddWebResourceRequestedFilter "*.gif", wv2WebResourceContext.wv2WebResourceContextImage
End Sub
' 处理资源请求
Private Sub WebView21_WebResourceRequested(ByVal Request As WebView2Request, _
ByVal Response As WebView2Response)
' 检查是否是广告图片
If InStr(1, Request.URI, "ad-") > 0 Then
' 返回空白图片
Response.Content = LoadBlankImage()
Response.Headers = "Content-Type: image/png"
End If
End Sub
' 停止拦截
Private Sub StopIntercepting()
WebView21.RemoveWebResourceRequestedFilter "*.jpg", wv2WebResourceContext.wv2WebResourceContextImage
WebView21.RemoveWebResourceRequestedFilter "*.png", wv2WebResourceContext.wv2WebResourceContextImage
WebView21.RemoveWebResourceRequestedFilter "*.gif", wv2WebResourceContext.wv2WebResourceContextImage
End Sub
文档打印
PrintToPdf 方法
- 描述:将当前页面保存为 PDF 文件
- 语法:
PrintToPdf(OutputPath As String, [可选参数])
- 示例:
vb
' 基本的PDF导出
Private Sub ExportSimplePdf()
Dim pdfPath As String
pdfPath = App.Path & "\export.pdf"
WebView21.PrintToPdf pdfPath
End Sub
' 带自定义选项的PDF导出
Private Sub ExportCustomPdf()
Dim pdfPath As String
pdfPath = App.Path & "\custom.pdf"
' 自定义PDF导出选项
WebView21.PrintToPdf pdfPath, _
wv2PrintOrientation.wv2PrintLandscape, _ ' 横向打印
1.0, _ ' 缩放比例
210, _ ' 页面宽度(mm)
297, _ ' 页面高度(mm)
20, _ ' 上边距(mm)
20, _ ' 下边距(mm)
20, _ ' 左边距(mm)
20, _ ' 右边距(mm)
True, _ ' 打印背景
False, _ ' 不仅打印选中内容
True, _ ' 显示页眉和页脚
"我的文档", _ ' 页眉标题
WebView21.DocumentURL ' 页脚URL
End Sub
' 处理PDF导出结果
Private Sub WebView21_PrintToPdfCompleted()
MsgBox "PDF导出成功!", vbInformation
End Sub
Private Sub WebView21_PrintToPdfFailed()
MsgBox "PDF导出失败", vbCritical
End Sub
多窗口支持
标签页和新窗口处理
- 描述:处理新窗口请求和标签页管理
- 示例:
vb
' 处理新窗口请求
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)
' 实现标签页支持
If IsUserInitiated Then
CreateNewTab Uri
IsHandled = True
End If
End Sub
' 创建新标签页的示例实现
Private Sub CreateNewTab(ByVal Url As String)
' 创建新的WebView2控件
Dim newTab As WebView2
Set newTab = Controls.Add("WebView2Control.WebView2", "newTab" & TabCount)
' 配置新标签页
With newTab
.Move TabContainer.Left, TabContainer.Top, TabContainer.Width, TabContainer.Height
.Visible = True
.Navigate Url
End With
' 更新标签页界面
AddTabButton Url
TabCount = TabCount + 1
End Sub
触控支持
IsPinchZoomEnabled 属性
- 描述:控制是否启用触控缩放功能
- 要求:需要 SupportsPinchZoomFeatures 支持
- 示例:
vb
' 检查并启用触控缩放
Private Sub EnableTouchFeatures()
If WebView21.SupportsPinchZoomFeatures Then
WebView21.IsPinchZoomEnabled = True
End If
End Sub
IsSwipeNavigationEnabled 属性
- 描述:控制是否启用滑动导航功能
- 要求:需要 SupportsSwipeNavigationFeatures 支持
- 示例:
vb
' 配置触控导航
Private Sub ConfigureTouchNavigation()
If WebView21.SupportsSwipeNavigationFeatures Then
' 启用滑动导航
WebView21.IsSwipeNavigationEnabled = True
End If
End Sub
' 创建完整的触控支持配置
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