WebBrowser控件自动调整整个网页的大小

时间:2014-07-02 09:45:10

标签: vb.net web browser webbrowser-control

有没有办法让WebBrowser Control自动调整整个网页的大小? 像这样:

enter image description here

    Public Class Form1
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = "Web Browser"
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Navigate(TextBox1.Text)
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        WebBrowser1.GoBack()
    End Sub
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        WebBrowser1.GoForward()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        WebBrowser1.Refresh()
    End Sub
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        WebBrowser1.Navigate("http://www.google.pt")
    End Sub
End Class

我只有那个代码...... 我应该把代码放在哪里?

2 个答案:

答案 0 :(得分:1)

  • 首先,您必须在TimerControl
  • 中添加form
  • 并在下面给出休息

    Public Class Form1

        Dim pswaHeight As Object
        Dim pswaWidth As Object
        Dim pswaHeightInt As Integer
        Dim pswaWidthInt As Integer
    
        Private Enum Exec
            OLECMDID_OPTICAL_ZOOM = 63
        End Enum
    
        Private Enum execOpt
            OLECMDEXECOPT_DODEFAULT = 0
            OLECMDEXECOPT_PROMPTUSER = 1
            OLECMDEXECOPT_DONTPROMPTUSER = 2
            OLECMDEXECOPT_SHOWHELP = 3
        End Enum
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            pswaHeight = Screen.PrimaryScreen.WorkingArea.Height
            pswaWidth = Screen.PrimaryScreen.WorkingArea.Width
            pswaHeightInt = CInt(pswaHeight)
            pswaWidthInt = CInt(pswaWidth)
    
            Me.Text = "Web Browser"
    
        End Sub
    
        Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    
    
            If WebBrowser1.Width > Math.Round(pswaWidthInt / 5.6) And WebBrowser1.Width < Math.Round(pswaWidthInt / 2.97) Or WebBrowser1.Height > Math.Round(pswaHeightInt / 3.0) And WebBrowser1.Height < Math.Round(pswaHeightInt / 2.4) Then
                Try
                    Dim Res As Object = Nothing
                    Dim MyWeb As Object
                    MyWeb = Me.WebBrowser1.ActiveXInstance
                    MyWeb.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, execOpt.OLECMDEXECOPT_PROMPTUSER, 40, IntPtr.Zero)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
    
            If WebBrowser1.Width > Math.Round(pswaWidthInt / 2.97) And WebBrowser1.Width < Math.Round(pswaWidthInt / 2.58) Or WebBrowser1.Height > Math.Round(pswaHeightInt / 2.4) And WebBrowser1.Height < Math.Round(pswaHeightInt / 2.13) Then
                Try
                    Dim Res As Object = Nothing
                    Dim MyWeb As Object
                    MyWeb = Me.WebBrowser1.ActiveXInstance
                    MyWeb.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, execOpt.OLECMDEXECOPT_PROMPTUSER, 50, IntPtr.Zero)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
    
            If WebBrowser1.Width > Math.Round(pswaWidthInt / 2.58) And WebBrowser1.Width < Math.Round(pswaWidthInt / 2.23) Or WebBrowser1.Height > Math.Round(pswaHeightInt / 2.13) And WebBrowser1.Height < Math.Round(pswaHeightInt / 1.85) Then
                Try
                    Dim Res As Object = Nothing
                    Dim MyWeb As Object
                    MyWeb = Me.WebBrowser1.ActiveXInstance
                    MyWeb.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, execOpt.OLECMDEXECOPT_PROMPTUSER, 60, IntPtr.Zero)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
    
            If WebBrowser1.Width > Math.Round(pswaWidthInt / 2.23) And WebBrowser1.Width < Math.Round(pswaWidthInt / 1.89) Or WebBrowser1.Height > Math.Round(pswaHeightInt / 1.85) And WebBrowser1.Height < Math.Round(pswaHeightInt / 1.64) Then
                Try
                    Dim Res As Object = Nothing
                    Dim MyWeb As Object
                    MyWeb = Me.WebBrowser1.ActiveXInstance
                    MyWeb.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, execOpt.OLECMDEXECOPT_PROMPTUSER, 70, IntPtr.Zero)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
    
            If WebBrowser1.Width > Math.Round(pswaWidthInt / 1.89) And WebBrowser1.Width < Math.Round(pswaWidthInt / 1.6) Or WebBrowser1.Height > Math.Round(pswaHeightInt / 1.64) And WebBrowser1.Height < Math.Round(pswaHeightInt / 1.53) Then
                Try
                    Dim Res As Object = Nothing
                    Dim MyWeb As Object
                    MyWeb = Me.WebBrowser1.ActiveXInstance
                    MyWeb.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, execOpt.OLECMDEXECOPT_PROMPTUSER, 80, IntPtr.Zero)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
    
            If WebBrowser1.Width > Math.Round(pswaWidthInt / 1.6) And WebBrowser1.Width < Math.Round(pswaWidthInt / 1.2) Or WebBrowser1.Height > Math.Round(pswaHeightInt / 1.53) And WebBrowser1.Height < Math.Round(pswaHeightInt / 1.16) Then
                Try
                    Dim Res As Object = Nothing
                    Dim MyWeb As Object
                    MyWeb = Me.WebBrowser1.ActiveXInstance
                    MyWeb.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, execOpt.OLECMDEXECOPT_PROMPTUSER, 90, IntPtr.Zero)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
    
            If WebBrowser1.Width > Math.Round(pswaWidthInt / 1.2) AndAlso WebBrowser1.Height > Math.Round(pswaHeightInt / 1.16) Then
                Try
                    Dim Res As Object = Nothing
                    Dim MyWeb As Object
                    MyWeb = Me.WebBrowser1.ActiveXInstance
                    MyWeb.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, execOpt.OLECMDEXECOPT_PROMPTUSER, 100, IntPtr.Zero)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        End Sub
    
    
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    
        WebBrowser1.Navigate(TextBox1.Text)
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        WebBrowser1.GoBack()
    End Sub
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        WebBrowser1.GoForward()
    End Sub
    
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        WebBrowser1.Refresh()
    End Sub
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        WebBrowser1.Navigate("http://www.google.pt")
    End Sub
        End Class
    

答案 1 :(得分:0)

在 TabControls 的第一个 Tab 中,将文本设置为 URL 地址(或空格)并用额外的空格填充它以适应表单的宽度。 这样它看起来就像你浏览器的地址栏(如果你想要的话)。

还要确保在所有要通过表单外部代码控制的控件上将“修饰符”设置为公共。

最重要的是:不要在表单中使用“My.settings”,如果它不是第一次加载的表单(主加载的表单或模块)。 始终将设置存储在第一个加载的模块表单中 “我的.设置.保存”。任何在主要模块/表单中保存设置的尝试都会导致“我的设置”损坏(这是我偶然了解到的,并花了 3 个月的时间来解决发生损坏的原因)。