Create your own web browser on your SmartPhone
Windows Mobile 5.0 comes with a Web Browser (v6 is due out any day now). It runs on Pocket PCs and SmartPhones. That browser only allows one web page to show at a time: to show another page, you have to navigate away from the current page. I created a web browser for my first SmartPhone over a year ago that allows you to open multiple web pages.
If you have Visual Studio 2008 (or 2005), you can do it too, with the few hundred lines of code below.
This sample WebBrowser features:
- Go Forward/Back
- adding and editing of favorites
- full screen view
- multiple web pages open
- address bar editing.
Start VS 2008 (or 2005). Choose File->New->Project->Visual Basic->Smart Device Project->Ok.
(You can also run this same sample as a standard VB Windows Form application to see it running on your PC. File->New->Project->Visual Basic->Windows->Windows Forms Application. You can actually have 2 projects in one solution: one for the SmartPhone, one for your PC, both sharing the same code: PhoneBrowser.vb. Then you can right click on the desired project and Set As Startup Project. Be careful of having 2 copies of the code: One copy actually references the Compact .Net Framework, and is in a different project type).
In the wizard that shows, choose Target Platform: Windows Mobile 5.0 Smartphone SDK, and Templates: Device Application
In Solution Explorer, Delete Form1, then add a new Class called "PhoneBrowser.vb"
Paste the code below.
In Project Properties, choose Application->Startup Object: PhoneBrowser
Hit F5 to build and run the program. A dialog comes up: choose either smart phone emulator. The first time, it takes a few seconds to deploy the .Net Framework 3.5.
Notes: use Tools->Device Emulation Manager to hook up the emulator to your network, so it can browse web sites. Right click on the emulator you chose and choose "Cradle" to attach the emulator electronically to your PC (like putting the phone into a cradle that's attached via cable). Otherwise you'll get "Your Internet connection is not configured properly. Please verify your settings in Data Connections"
Install Microsoft ActiveSync 4.5, run it, and choose File->Connection Settings->Connect to sync the emulator to the PC. This is not required, but will allow you to have your emulator connect to the web while it's cradled.
You can change the Output File folder where the application is stored: Project->Properties->Devices->Output File Folder. It defaults to %CSIDL_PROGRAM_FILES%\SmartDeviceProject1
The sample doesn't show History: if you figure that out, let me know. Otherwise, I may just create a SQL Server CE database and store the links in thereā¦.
If you're using VS 2005, you'll get several VB compile errors that you can fix due to new features in VB.Net 2008
- Change due to Type Inference:
- Old: Dim NewWindow = New PhoneBrowser
- New: Dim NewWindow As New PhoneBrowser
- Change For loops to declare vars
- Old: For i = 1 To oBrowserForms.Count
- New: For i As Integer = 1 To oBrowserForms.Count
- Relaxed Delegates in 2008 means the method signature doesn't have to exactly match. To fix this, change the event handler method parameters to match.
- Old: ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs
- New: ByVal o As MyMenuItem, ByVal e As System.Windows.Forms.KeyEventArgs
- Now that the method signatures match, you'll have to cast some of the Objects to things like "MyMenuItem" or "MyListViewItem" :
- Old: Select Case o.Text
- New: Select Case CType(o, MyMenuItem).Text
- The sample uses Linq in a few places: just replace with the commented code below if Linq is not available (as in VS 2005)
The F1,F2 buttons on your keyboard map to the left and right soft keys of the phone.
To deploy on a real SmartPhone, connect via ActiveSync and choose Project->Properties->Devices->Target Device: Windows Mobile 5.0 Smartphone Device, then hit F5. You'll get some confirmation dialogs on your phone.
You can use Linq on your SmartPhone in VS 2008: if you get an error creating a simple query, Project->Properties: add a reference to c:\Program Files\Microsoft.Net\SDK\CompactFramework\V3.5\WindowsCE\System.Core.dll, and import System.Core and System.Xml
You may have to unlock your phone: http://blogs.conchango.com/stuartpreston/archive/2005/11/10/2376.aspx
See also:
Customize your Windows Mobile SmartPhone home screen and Start Menu
How to: Use the WebBrowser Control in the .NET Compact Framework
Smartphone Development and the .NET Compact Framework
Windows Mobile 5.0 Application Security
Windows Mobile-based Smartphone Applications Deployment Demystified
Device Security Manager Powertoy for Windows Mobile 5.0
Remove double spaces from pasted code samples in blog
Visual Studio->Tools menu->Device Security Manager
Lots of goodies under Start Menu->Visual Studio 2008->Remote Tools, like Remote Registry Editor, Remote Process Viewer, Remote File Viewer
Start of Code
Public Class PhoneBrowser
Inherits Form
#If Smartphone = True Then 'auto set: Project->Properties->Compile->Advanced Compile Options->Custom Constants
Dim fSmartPhone = True
#Else
Dim fSmartPhone = False
#End If
Friend WithEvents WebBrowserCtrl As System.Windows.Forms.WebBrowser
Shared oBrowserForms As New System.Collections.Generic.List(Of PhoneBrowser)
Private Sub PhoneBrowser_Load() Handles MyBase.Load
Me.WebBrowserCtrl = New Windows.Forms.WebBrowser
Me.WebBrowserCtrl.Dock = DockStyle.Fill
Me.WebBrowserCtrl.DocumentText = "<html>Custom Web Browser for Smartphone by Calvin Hsia</html>" ' put your name here
Me.Controls.Add(Me.WebBrowserCtrl)
Me.Text = "PhoneBrowser"
If fSmartPhone Then
Me.WindowState = FormWindowState.Maximized
Else
Me.Size = New Size(1280, 1024)
End If
oBrowserForms.Add(Me) ' add ourselves to the collection
If oBrowserForms.Count = 1 Then ' if it's the first instance
Me.WebBrowserCtrl.Navigate(New System.Uri("http://www.msn.com"))
'ShowFavorites()
End If
If Not fSmartPhone Then
ShowMenu()
End If
End Sub
Private Sub PhoneBrowser_FormClosing(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closing
For i = 1 To oBrowserForms.Count
If oBrowserForms.Item(i - 1) Is Me Then ' remove ourselves from the list
oBrowserForms.RemoveAt(i - 1)
Exit For
End If
Next
End Sub
Private Sub PhoneBrowser_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = System.Windows.Forms.Keys.F1 OrElse _
e.KeyCode = System.Windows.Forms.Keys.F2 Then
ShowMenu()
Else
If e.KeyCode = Keys.Back Then
If WebBrowserCtrl.CanGoBack Then
WebBrowserCtrl.GoBack()
End If
End If
End If
End Sub
Sub ShowMenu()
Me.Menu = New MainMenu
Dim mItem As MyMenuItem
Dim mItemBar As MyMenuItem 'the 2 softkeys
Dim mItemNav As MyMenuItem
If Me.WebBrowserCtrl.ReadyState <> WebBrowserReadyState.Complete Then
mItemBar = New MyMenuItem("Stop Request", Me.Menu.MenuItems, AddressOf Me.BrowserMenuItem_Click)
Else
mItemBar = New MyMenuItem("Favorites", Me.Menu.MenuItems, AddressOf Me.BrowserMenuItem_Click)
End If
mItemBar = New MyMenuItem("Menu", Me.Menu.MenuItems)
mItem = New MyMenuItem("Address Bar", mItemBar.MenuItems, AddressOf Me.BrowserMenuItem_Click)
mItem = New MyMenuItem("Add to Favorites", mItemBar.MenuItems, AddressOf Me.BrowserMenuItem_Click)
mItemNav = New MyMenuItem("Navigate", mItemBar.MenuItems)
mItem = New MyMenuItem("Favorites", mItemNav.MenuItems, AddressOf Me.BrowserMenuItem_Click)
mItem = New MyMenuItem("Go Forward", mItemNav.MenuItems, AddressOf Me.BrowserMenuItem_Click)
mItem.Enabled = Me.WebBrowserCtrl.CanGoForward
mItem = New MyMenuItem("Go Back", mItemNav.MenuItems, AddressOf Me.BrowserMenuItem_Click)
mItem.Enabled = Me.WebBrowserCtrl.CanGoBack
mItem = New MyMenuItem("Cancel Menu", mItemBar.MenuItems, AddressOf Me.BrowserMenuItem_Click)
mItem = New MyMenuItem("Refresh", mItemBar.MenuItems, AddressOf Me.BrowserMenuItem_Click) ' keep in pos 5
mItem = New MyMenuItem("New Window", mItemBar.MenuItems, AddressOf Me.BrowserMenuItem_Click)
mItem = New MyMenuItem("Switch Windows", mItemBar.MenuItems)
If oBrowserForms.Count = 1 Then
mItem.Enabled = False
Else
For i = 1 To oBrowserForms.Count
If Not oBrowserForms(i - 1) Is Me Then ' don't add ourself
Dim mText As String
Dim oUri = oBrowserForms(i - 1).WebBrowserCtrl.Url
If oUri Is Nothing Then
mText = "Form " + i.ToString
Else
If oUri.ToString.ToLower.StartsWith("http://") Then
mText = oUri.ToString.Substring(7)
Else
mText = oUri.ToString
End If
End If
Dim MenuItemBrowserInstance = New MyMenuItem(mText, mItem.MenuItems, AddressOf MenuItemNewFormHandler_Click)
MenuItemBrowserInstance.oBrowser = oBrowserForms(i - 1)
End If
Next
End If
mItem = New MyMenuItem("Exit", mItemBar.MenuItems, AddressOf Me.BrowserMenuItem_Click)
End Sub
Sub BrowserMenuItem_Click(ByVal o As MyMenuItem, ByVal e As System.EventArgs)
If fSmartPhone Then
Me.Menu = Nothing ' make menu disappear
End If
Select Case o.Text
Case "Stop Request"
If Me.WebBrowserCtrl.ReadyState <> WebBrowserReadyState.Complete Then
Me.WebBrowserCtrl.Stop()
End If
ShowMenu() ' refresh menu
Case "Favorites"
ShowFavorites()
Case "Address Bar"
Dim oAddrBarForm = New FormAddressBar(Me)
If oAddrBarForm.ShowDialog() = Windows.Forms.DialogResult.OK Then
Try
Me.WebBrowserCtrl.Navigate(New System.Uri(oAddrBarForm.Result))
Catch ex As Exception
MsgBox(oAddrBarForm.Result, MsgBoxStyle.OkOnly, "Invalid URL")
End Try
End If
Case "Add to Favorites"
ShowAddFavoriteForm(Me, o.Text)
Case "Go Forward"
Me.WebBrowserCtrl.GoForward()
Case "Go Back"
Me.WebBrowserCtrl.GoBack()
Case "Refresh"
If Me.WebBrowserCtrl.ReadyState <> WebBrowserReadyState.Complete Then
Me.WebBrowserCtrl.Stop()
End If
Me.WebBrowserCtrl.Refresh()
Case "Cancel Menu"
'do nothing
Case "New Window"
Dim NewWindow = New PhoneBrowser
Dim cUrl = ""
If Not Me.WebBrowserCtrl.Url Is Nothing Then
cUrl = Me.WebBrowserCtrl.Url.ToString
End If
NewWindow.Show()
NewWindow.WebBrowserCtrl.Navigate(New System.Uri(cUrl)) ' new instance goes to same page
Case "Exit"
If Me.WebBrowserCtrl.ReadyState <> WebBrowserReadyState.Complete Then
Me.WebBrowserCtrl.Stop()
End If
Me.Close()
Case Else
MsgBox("Menu item " + o.Text + " not handled")
End Select
End Sub
Sub ShowFavorites()
If Me.WebBrowserCtrl.ReadyState <> WebBrowserReadyState.Complete Then
Me.WebBrowserCtrl.Stop()
End If
Dim oFavForms As New FormFavorites(Me)
If oFavForms.ShowDialog() = Windows.Forms.DialogResult.OK AndAlso oFavForms.Result.Length > 0 Then
Try
Me.WebBrowserCtrl.Navigate(New System.Uri(oFavForms.Result))
Catch ex As Exception
MsgBox(oFavForms.Result, MsgBoxStyle.OkOnly, "Error in URL")
End Try
End If
End Sub
Shared Sub ShowAddFavoriteForm(ByVal oPhoneBrowser As PhoneBrowser, _
ByVal cTitle As String, _
Optional ByVal cName As String = "", _
Optional ByVal cUrlSpecific As String = "", _
Optional ByVal cOrigFileName As String = "")
'SmartPhone webbrowser doesn't have Document property
Dim cHost = cName
Dim cUrl = cUrlSpecific
If Not oPhoneBrowser.WebBrowserCtrl.Url Is Nothing AndAlso cTitle <> "Edit" Then
cHost = oPhoneBrowser.WebBrowserCtrl.Url.Host
cUrl = oPhoneBrowser.WebBrowserCtrl.Url.ToString
End If
Dim oAddFav As New FormAddToFavorites(cHost, cUrl, cTitle, cOrigFileName)
If oAddFav.ShowDialog = Windows.Forms.DialogResult.OK Then
End If
End Sub
Sub MenuItemNewFormHandler_Click(ByVal o As MyMenuItem, ByVal e As System.EventArgs)
If fSmartPhone Then
Me.Menu = Nothing
End If
o.oBrowser.Show()
End Sub
Class MyMenuItem ' same as MenuItem, except a place to put a reference to the instance
Inherits MenuItem
Public oBrowser As PhoneBrowser
Sub New(ByVal cText As String, ByRef mi As MenuItemCollection, _
Optional ByVal handler As System.EventHandler = Nothing)
Me.Text = cText
mi.Add(Me)
If Not handler Is Nothing