Imports System.Net
Imports System.IO
Imports System.Text
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
...
#End Region
' Create class-level event handler variable
Private WithEvents WebPage As SHDocVw.DWebBrowserEvents_Event
' Initialize WebBrowser control
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebPage = DirectCast(WebBrowser.Application, SHDocVw.WebBrowser_V1)
End Sub
' Resize form controls in response to Form resize
Private Sub Form1_Layout(ByVal sender As Object, _
ByVal e As System.Windows.Forms.LayoutEventArgs) Handles MyBase.Layout
Dim sz As Size
Dim p As Point
' Resize groupbox to client area of form
GroupBox1.Width = Me.ClientSize.Width
' Resize and reposition TabControl to client area of form
TabControl1.Width = Me.ClientSize.Width
TabControl1.Height = Me.ClientSize.Height - GroupBox1.Size.Height
p.X = 0
p.Y = GroupBox1.Height
TabControl1.Location = p
End Sub
' Resize TabControl child controls in response to TabControl resize
Private Sub TabControl1_Layout(ByVal sender As Object, _
ByVal e As System.Windows.Forms.LayoutEventArgs) Handles MyBase.Layout
Dim h As Integer = TabControl1.TabPages(TabControl1.SelectedIndex).Height
Dim w As Integer = TabControl1.TabPages(TabControl1.SelectedIndex).Width
TextBoxHTML.Height = h
TextBoxHTML.Width = w
WebBrowser.Height = h
WebBrowser.Width = w
TextBoxVisitedURLs.Height = h
TextBoxVisitedURLs.Width = w
End Sub
' Load the user-entered URL into the WebBrowser
Private Sub LoadURL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
Cursor.Current = Cursors.WaitCursor
' Add http:// to string if necessary
If String.Compare(TextBoxURL.Text.Substring(0, 7), "http://") Then
TextBoxURL.Text = "http://" & TextBoxURL.Text.ToString()
End If
LoadBrowserView(TextBoxURL.Text.ToString())
Cursor.Current = Cursors.Default
End Sub
' Load the previous Web page
Private Sub PrevPage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PicturePrevPage.Click
Try
WebBrowser.GoBack()
Catch
Beep()
End Try
End Sub
' Load the next Web page
Private Sub NextPage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureNextPage.Click
Try
WebBrowser.GoForward()
Catch
Beep()
End Try
End Sub
' Load the specified URL into the WebBrowser control
Private Sub LoadBrowserView(ByVal URL As String)
Dim obj As System.Object = System.Reflection.Missing.Value
Try
WebBrowser.Navigate(URL, obj, obj, obj, obj)
Catch ex As Exception
Beep()
End Try
End Sub
' Page in process of downloading. OK to update URL textbox.
Private Sub WebPage_NavigateComplete(ByVal URL As String) Handles WebPage.NavigateComplete
TextBoxURL.Text = URL
End Sub
' Update the "Visited URLs" textbox with title and URL
Private Sub WebPage_TitleChange(ByVal sTitle As String) Handles WebPage.TitleChange
Static Dim PrevTitle = ""
' Ignore duplicate events for same page
If sTitle <> PrevTitle Then
TextBoxVisitedURLs.Text += sTitle & vbCrLf
TextBoxVisitedURLs.Text += " " & WebBrowser.LocationURL.ToString() & vbCrLf & vbCrLf
End If
PrevTitle = sTitle
End Sub
Private Sub TabControl1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.Click
' If "HTML View" tab selected, load HTML of current Web page
If TabControl1.SelectedIndex = 1 Then
If WebBrowser.LocationURL.ToString() <> "" Then
LoadHTMLView(TextBoxURL.Text)
End If
End If
' If "Visited URLs" tab selected, scroll to bottom of textbox
If TabControl1.SelectedIndex = 2 Then
TextBoxVisitedURLs.SelectionStart = TextBoxHTML.Text.Length
TextBoxVisitedURLs.SelectionLength = 0
TextBoxVisitedURLs.ScrollToCaret()
End If
End Sub
' Stream the HTML text of the specified URL into the tab's textbox
Private Sub LoadHTMLView(ByVal URL As String)
Static Dim PrevURL As String = ""
' Don't need to reload text if same URL
If URL = PrevURL Then Return
Try
' Initialize text stream reader for current Web page
Dim myHttpWebRequest As HttpWebRequest = WebRequest.Create(URL)
Dim myHttpWebResponse As HttpWebResponse = myHttpWebRequest.GetResponse()
Dim recvStream As Stream = myHttpWebResponse.GetResponseStream()
' Read the stream in the encoded "utf-8" format
Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")
Dim readStream As New StreamReader(recvStream, encode)
Dim sPage As String
While (readStream.Peek() > -1)
sPage += readStream.ReadLine() + vbCrLf
End While
TextBoxHTML.Text = sPage
PrevURL = URL
Catch ex As Exception
Beep()
End Try
End Sub
End Class
|