I have been thinking about writing a script where I wanted to open some browsers within the interface. I thought I’d use the WinForms WebBrowser class. I put together a real simple script just to see how the control worked. The script does nothing except select between a couple different websites, but it does show one way the control could be used.

In a basic WinForms window setup I added the WebBrowser control configuring location, size and the URL. I also enabled the context menu (right click menu). That is the default for this property but since I am changing it in other WebBrowser controls I like to be explicit in each.

Add-Type -AssemblyName System.Windows.Forms
$URL1 = “http://www.wikipedia.com
$URL2 = “https://adminscache.wordpress.com
$URL3 = “http://www.microsoft.com

# WinForm Setup
$mainForm = New-Object System.Windows.Forms.Form
$mainForm.Font = “Comic Sans MS,9″
$mainForm.ForeColor = [System.Drawing.Color]::White
$mainForm.BackColor = [System.Drawing.Color]::DarkSlateBlue
$mainForm.Text = ” System.Windows.Forms.WebBrowser Class”
$mainForm.Width = 960
$mainForm.Height = 700

# Main Browser
$webBrowser1 = New-Object System.Windows.Forms.WebBrowser
$webBrowser1.IsWebBrowserContextMenuEnabled = $true
$webBrowser1.URL = $URL1
$webBrowser1.Width = 600
$webBrowser1.Height = 600
$webBrowser1.Location = “50, 25”
$mainForm.Controls.Add($webBrowser1)

The selection of websites is done by clicking a CheckBox next to a small window of the website. The selection browsers are setup like the main browser except they are smaller and I have disabled the context menu on them.

# First Selectable Browser
$webBrowser2 = New-Object System.Windows.Forms.WebBrowser
$webBrowser2.IsWebBrowserContextMenuEnabled = $false
$webBrowser2.URL = $URL1
$webBrowser2.Width = 200
$webBrowser2.Height = 150
$webBrowser2.Location = “700, 50”
$mainForm.Controls.Add($webBrowser2)

There is a CheckBox next to each selection browser. I check the first CheckBox and use it’s URL in the initial form load. I clear any checked boxes from the previous selection for a new URL .

# First Select Checkbox
$selectCheckbox1 = New-Object System.Windows.Forms.Checkbox
$selectCheckbox1.Location = “910,120”
$selectCheckbox1.Checked = $true
$selectCheckbox1.add_Click({
     $webBrowser1.URL = $URL1
     $selectCheckbox2.Checked = $false
     $selectCheckbox3.Checked = $false
     })
$mainForm.Controls.Add($selectCheckbox1)

WebBrowser

WebBrowser.ps1

Advertisements