This article
discusses programming for a Pocket PC in general complete with a code sample.
If you are lucky enough to get the Smart Devices Extensions you will finally be
able to target devices like Pocket PCs using C# however like all good things
there are a few snags.
The first of course is that Microsoft cannot squeeze the entire .NET framework
into perhaps a 32mb ROM or even 32MB Ram. Microsoft have made some cutbacks so
before jumping in and developing the worlds greatest application on Pocket PC
take care and have a think about these considerations.
-
Just because a
class exists on the full .NET framework does not mean it exists on the
Compact Framework. This means you need to research your Pocket PC project
and ensure that all the classes you need and even the methods you are used
to using have been implemented on the Compact Framework.
-
If one of your favorite
classes is not implemented, what do you do? Well its possible that Microsoft
has implemented a different Pocket PC class that gives you the functionality
you need or maybe they have not included the functionality as it can be
achieved with another class.
-
Remember that a Pocket PC
or mobile device, unlike a desktop PC has very limited performance and so
any duplication of functionality has to be removed.
-
Welcome to the Win32 API.
If you have used Microsofts embedded Visual Basic tool you may assume that
all its functionality is included in the Smart Device Extensions. This is a
mistake, a lot of the functionality of embedded visual basic was developed
over a few versions of the product and the same thing happens with Smart
Device Extensions. Some of the common functionality of a Pocket PC like
hiding and displaying the Pocket PC SIP or keyboard are not provided as
classes and such you need to call out to Win32 APIs.
-
Assuming the above, the
best book I can recommend for using API calls on Pocket PC is Programming
Microsoft Windows CE and you can see this on Amazon at
http://www.amazon.com/exec/obidos/ASIN/0735614431/qid=1019870793/sr=1-1/ref=sr_1_1
/002-8613147-9333661.
-
Smart Devices Extension
also includes an emulator to enable you to write code for a Pocket PC and
run it on your development system. Of course this is an excellent tool but
don't be fooled into thinking you can develop and complete a Pocket PC
project without actually having a real Pocket PC. Keep in mind that you
cannot guarantee that the emulator will show up all problems in your code.
With each release of these tools the emulator gets better but you cannot
beat the real thing!
- As I have just
found if you take C# code and try to run it on a Pocket PC project it will
probably fail. Perhaps you have used a class that the Pocket PC does not
support. Or more annoyingly there are inconsistencies between the Pocket PC
classes and the desktop versions. Even something as simple as the Timer
class exhibits this problem. On the desktop version of my animate example I
use Start and Stop methods to stop and start the timer. On a Pocket PC
project you set the Enabled property to true or false to achieve the same
thing. An annoying problem but not a major one. However it means you cannot
just copy and paste code between desktop and Pocket PC projects.
Finally, you may read
the above and think I am negative about coding C# on Pocket PC. This of course
is not the case. I can now use the same (more or less) code to write apps on
both a desktop and a mobile device. As long as you are aware of the limitations
the possibilities are great and once again thanks to Microsoft for producing
another great development system on Pocket PC.
Now onto some code!!
This first screenshot shows what happens when you create a new project when you
have Smart Device Extensions installed.
As you can see I
now have a new Smart Device Application Project type. Once entering the name etc
I get a new dialog asking me whether I want to create a Windows CE or a Pocket
PC project. Here is the Dialog.
Once you have entered your
code and try to run it you will be asked whether you want to run the code in
either a Pocket PC device or using the emulator. Because I am lazy and have not
unpacked my Pocket PC I will target the emulator as you can see from this
screen.
Here is the final code and the project has been included in the attached zip
file.
'Pocket
PC Animate
'Shows some very basic animation code on the Pocket PC platform
'Written 06/04/2004 by John O'Donnell - [email protected]
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.Windows.Forms
Imports System.Data
Public Class Form1
Inherits System.Windows.Forms.Form
Public ballarray(20,
20) As Integer
Public g1 As Integer =
1
Public g2 As Integer =
200
Private timer1 As System.Windows.Forms.Timer
Private menuItem1 As System.Windows.Forms.MenuItem
Private mainMenu1 As System.Windows.Forms.MainMenu
#Region "
Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This
call is required by the Windows Form Designer.
InitializeComponent()
'Add
any initialization after the InitializeComponent() call
End Sub
'Form
overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
MyBase.Dispose(disposing)
End Sub
'NOTE:
The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Private Sub InitializeComponent()
Me.mainMenu1
= New System.Windows.Forms.MainMenu
Me.menuItem1
= New System.Windows.Forms.MenuItem
Me.timer1
= New System.Windows.Forms.Timer
'
'mainMenu1
'
Me.mainMenu1.MenuItems.Add(Me.menuItem1)
'
'menuItem1
'
Me.menuItem1.Text
= "Start"
AddHandler menuItem1.Click, AddressOf menuItem1_Click
'
'timer1
'
Me.timer1.Interval
= 10
AddHandler timer1.Tick, AddressOf timer1_Tick
'
'Form1
'
Me.Menu
= Me.mainMenu1
Me.Text
= "Form1"
End Sub
#End Region
Private Sub timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim i As Integer
For i
= 1 To 13
'add
direction vectors to coordinates
ballarray(i, 1)
= ballarray(i, 1) + ballarray(i, 3)
ballarray(i, 2) = ballarray(i, 2) + ballarray(i, 4)
'if
ball goes of to right
If ballarray(i,
1) + 50 >= ClientSize.Width Then
ballarray(i, 1) = ballarray(i, 1) - ballarray(i, 3)
ballarray(i, 3) = -ballarray(i, 3)
'if
ball goes off bottom
Else
If ballarray(i,
2) + 50 >= ClientSize.Height Then
ballarray(i, 2) = ballarray(i, 2) - ballarray(i, 4)
ballarray(i, 4) = -ballarray(i, 4)
'if
ball goes off to left
Else
If ballarray(i,
1) <= 1 Then
ballarray(i, 1) = ballarray(i, 1) - ballarray(i, 3)
ballarray(i, 3) = -ballarray(i, 3)
'if
ball goes over top
Else
If ballarray(i,
2) <= 1 Then
ballarray(i, 2) = ballarray(i, 2) - ballarray(i, 4)
ballarray(i, 4) = -ballarray(i, 4)
End If
End If
End If 'force
repaint of window
End If
Next i
Me.Refresh()
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim p As New Pen(Color.Blue)
Dim i As Integer
For i
= 1 To 13
e.Graphics.DrawEllipse(p, ballarray(i, 1), ballarray(i, 2), 50, 50)
Next i
End Sub 'OnPaint
Private Sub menuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim r As New Random
'set
ball coords and vectors x,y,xv,yv
Dim i As Integer
For i
= 1 To 13
ballarray(i, 1) = +r.Next(10) + 1 '+1
means i lose zero values
ballarray(i, 2)
= +r.Next(10) + 1
ballarray(i, 3) = +r.Next(10) + 1
ballarray(i, 4) = +r.Next(10) + 1
Next i
Me.timer1.Enabled
= True
End Sub
End Class