具有SVG文件和缩放支持的Web浏览器控件(VB.NET)

时间:2016-09-02 08:13:36

标签: vb.net winforms webbrowser-control

WebBrowser控件是否有显示SVG文件的示例,可以使用鼠标滚轮进行缩放?

1 个答案:

答案 0 :(得分:2)

这是一个简单的表单:

    Public Class Form1
    Inherits Form

    Private mZoomFactor As Short = 100

    Private Sub InitializeComponent()
        Me.WebBrowser1 = New System.Windows.Forms.WebBrowser()
        Me.WebBrowser1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.Controls.Add(Me.WebBrowser1)
    End Sub

    Friend WithEvents WebBrowser1 As System.Windows.Forms.WebBrowser

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        WebBrowser1.DocumentText = "<!DOCTYPE html><html><head><meta http-equiv=""X-UA-Compatible"" content=""IE=9""/></head><body><h1>My first SVG</h1><svg width=""100"" height=""100"">   <circle cx=""50"" cy=""50"" r=""40"" stroke=""green"" stroke-width=""4"" fill=""yellow"" />   Sorry, your browser does not support inline SVG.</svg></body></html>"
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'These two statements are needed, to get the MouseWheel event when the Control key is pressed.
        WindowState = FormWindowState.Normal
        WebBrowser1.Focus()
    End Sub

    Private Sub Form1_MouseWheel(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
        If ModifierKeys = Keys.Control AndAlso e.Delta <> 0 Then
            mZoomFactor += e.Delta / 120
            mZoomFactor = Math.Max(1, mZoomFactor)
            mZoomFactor = Math.Min(1000, mZoomFactor)
            WebBrowser1.Document.Body.Style = "zoom:" & mZoomFactor & "%"
        End If
    End Sub
End Class

对于要显示的svg,必须安装IE9并且必须在html的头部包含以下片段:

<meta http-equiv="X-UA-Compatible" content="IE=9"/>

要使WebBrowser控件获取MouseWheel事件,当按下Control键时,Form_Load处理程序中必须包含以下两个语句:

WindowState = FormWindowState.Normal
WebBrowser1.Focus()