Animate with Pocket PC

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

  1. 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.

  2. 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.

  3. 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.

  4. 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

  5. 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

  6. 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!

  7. 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.

PocketPCAnimateImage1.jpg

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.

PocketPCAnimateImage2.jpg

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.

PocketPCAnimateImage3.jpg

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 04/26/02 by John O'Donnell - [email protected]
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
namespace Animate
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
public int[,] ballarray = new int[20,20];
public int g1=1;
public int g2=200;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MainMenu mainMenu1;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent all
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.timer1 = new System.Windows.Forms.Timer();
this.menuItem1 = new System.Windows.Forms.MenuItem();
//
// mainMenu1
//
this.mainMenu1.MenuItems.Add(this.menuItem1);
//
// timer1
//
this.timer1.Interval = 10;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// menuItem1
//
this.menuItem1.Text = "Start";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// Form1
//
this.ClientSize = new System.Drawing.Size(240, 70);
this.Menu = this.mainMenu1;
this.Text = "Form1";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run(
new Form1());
}
private void timer1_Tick(object sender, System.EventArgs e)
{
for (int i=1;i<=13;i++)
{
//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)
{
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)
{
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)
{
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)
{
ballarray[i,2]=ballarray[i,2]-ballarray[i,4];
ballarray[i,4]=-ballarray[i,4];
}
}
this.Refresh(); //force repaint of window
}
protected override void OnPaint(PaintEventArgs e)
{
Pen p=
new Pen(Color.Blue);
for (int i=1;i<=13;i++)
{
e.Graphics.DrawEllipse(p, ballarray[i,1],ballarray[i,2],50,50);
}
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
Random r =
new Random();
//set ball coords and vectors x,y,xv,yv
for (int i=1;i<=13;i++)
{
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;
}
this.timer1.Enabled=true;
}
}
}

Up Next
    Ebook Download
    View all
    Learn
    View all