RSSThe TSW Blog

Making a mini webbrowser with WebCoder scripting

October 22, 2007

So, I thought I would do a little WebCoder scripting. We need some more demo scripts, showing off the potential off the scripting system. I created a small webbrowser, inside of WebCoder, and I'm now sharing the result with you. It could use a bit of polishing, but it works, and it has the basic webbrowser functionality, in only 61 lines of commented and nicely structured code - I think that's pretty cool! :)

So, here it is. If you wish to try it out, be sure to copy/paste it exactly like it is - the Python language is whitespace sensitive :). I know that this script is not terribly useful, but hopefully it will give you an idea of how easy it is to create something cool. I hope you can extend on this and make something even cooler! :)

# We need stuff from the .NET class library - add the 
# required assemblies and then import the classes we need
clr.AddReferenceByPartialName("System.Windows.Forms")
from System.Windows.Forms import WebBrowser, ToolStrip, ToolStripButton, ToolStripTextBox, Form, DockStyle

# Create our form (the dialog window)
browserForm = Form()
browserForm.Text = "Mini browser"
browserForm.Height = 400
browserForm.Width = 600

# Create an instance of the WebBrowser control and place it correctly
browser = WebBrowser()
browser.Dock = DockStyle.Fill
browserForm.Controls.Add(browser)

# Create an instance of a ToolStrip control (the .NET toolbar) and place it in the top
toolbar = ToolStrip()
toolbar.Dock = DockStyle.Top

# Events for the buttons we are about to create
def BackButtonClick(s, e):
	browser.GoBack()
	
def ForwardButtonClick(s, e):
	browser.GoForward()

def GoButtonClick(s, e):
	browser.Navigate(addressBox.Text)

# Create a couple of buttons on the toolbar
backButton = ToolStripButton()
backButton.Text = "Go back";
backButton.Click += BackButtonClick
toolbar.Items.Add(backButton)

forwardButton = ToolStripButton()
forwardButton.Text = "Go forward";
forwardButton.Click += ForwardButtonClick
toolbar.Items.Add(forwardButton)

# Create a text field for entering URL's
addressBox = ToolStripTextBox()
toolbar.Items.Add(addressBox)

# Create a Go button
goButton = ToolStripButton()
goButton.Text = "Go!"
goButton.Click += GoButtonClick
toolbar.Items.Add(goButton)

# Add the toolbar to the form
browserForm.Controls.Add(toolbar)

# Navigate to the TSW blog
browser.Navigate("http://www.tsware.net/blog/")

# Show the form
browserForm.ShowDialog()
# Dispose the form, freeing up it's resources
browserForm.Dispose()
Kasper (TSW) @ 9:44 pm in WebCoder

Leave a Reply