Master C# Logo banner
Welcome to MasterCSharp.com - Master C#, the easy way... - by Saurabh Nandu

 


Introduction to Tablet PC Development

Add Comment
 

 
Introduction
Microsoft has been on the forefront of new ways user interactivity with the computer. The Tablet PC is the result of one of those efforts to bring the computer much closer to the users comfort. Microsoft launched the Tablet PC OS along with hardware recommendations to Tablet PC manufacturers to insure end users get the best experience out of the platform.

Although its certainly a great platform and form factor, I believe that its not been successfully marketed. Even after 2 years of the tablets being in market, users still have the questions like Will Tablet PCs be mainstream?, Are Tablet PCs very expensive?, Tablet PCs are for a niche market, right?, I am sure the Tablet cant recognize my ****ed handwriting!. Last December (2004), I was in the US during Christmas time in San Francisco, California. I visited some local computer stores like Frys, Best Buys etc. around the Pleasanton-Freemont area and I did not find a single Tablet PC on display, making me wonder what the Tablet PC marketing has been doing!

So what's a Tablet PC?
Well there are dedicated websites like the Tablet PC Buzz  TheTabletPC.Net etc. that can fill you in with the gory details. To be brief, a Tablet PC is a platform on which one of the modes of input is a Pen. Remember the last time you took a Pen and wrote something long (I hope you are not recoiling way back to your college days like me); well the Tablet PC is a platform to give you the same experience with a geeky edge to it! Now it gives you the reason to walk into a meeting with your Tablet PC and use it to WRITE notes, draw (drawing stick figures of your boss :) ) and still not be rude! Other than that is an awesome platform for the Health Care, Insurance, ___ (Insert any of todays paper driven industry) to start adopting digital paper.

How do I get started to develop applications for the Tablet PC?
You could start by convincing your boss (which could be your wife), to get you a Tablet PC! Although thats not absolutely necessary to have a Tablet PC to get started! You can develop and test (to some extent) pen-enabled applications (i.e. applications for the Tablet PC) on your normal workstation/laptop.
All you really need is:
1) Windows 2000 (Sp4), Windows Server 2003, Windows XP Professional, Windows Tablet PC
2) .NET SDK v1.1 (VS 2003 will help, although not 100% necessary) considering you want to develop in C#.
3) Tablet PC SDK Latest is version 1.7 (15MB) [ http://www.microsoft.com/downloads/details.aspx?FamilyId=B46D4B83-A821-40BC-AA85-C9EE3D6E9699&displaylang=en ] Please read the notes on the download page.
4) Setup the paths to the Tablet PC SDK libraries as given at the following link
[ http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dntablet/html/tabdevfaq.asp?frame=true ]
Once you have the Tablet PC SDK installed, paths setup you are all set to start developing pen-enabled applications.

The first Pen Enabled application
1) Simple Windows Form
The first pen-enabled application I will build will show you how Ink (more on this a little later) can be used to draw/write on a Windows Form.
To start with fire-up your favorite editor (notepad/emacs) and punch in (copy/paste) the code below. As you will observe its simple code from FirstApp.cs that will display a Windows Form.
 

using System ;
using System.Windows.Forms ;

namespace MasterCSharp.TabletPC.Samples
{

  public class FirstApp : Form
  {
    public static void Main()
	{
	  Application.Run( new FirstApp() );
	}
  }
}

And then compile it with csc FirstApp.cs to produce FirstApp.exe application.

2) Adding Ink Collector object
In order for the sample application to be able to collect ink the Tablet PC SDK defines a control called the InkCollector (Microsoft.Ink namespace). This control can be attached to another control on which you want to capture ink. In our first sample I attach the InkCollector to the main Form so that I can collect ink from the form.
 

using System ;
using System.Windows.Forms ;
using Microsoft.Ink ;

namespace MasterCSharp.TabletPC.Samples
{

  public class FirstApp : Form
  {
    private InkCollector inkCollect ;

    public FirstApp()
    {
      inkCollect = new InkCollector(this);
      inkCollect.Enabled = true ;
    }
    public static void Main()
    {
	  Application.Run( new FirstApp() );
    }

    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if( inkCollect != null )
	      inkCollect.Dispose();			 
      }
      base.Dispose( disposing );
    }
  }
}

If you look at the code above the first thing I have done is referenced the Microsoft.Ink namespace. The assembly corresponding to this is Microsoft.ink.dll, which generally resides in the C:\Program Files\Microsoft Tablet PC Platform SDK\Include directory.
Next, within the class I have defined a member of type InkCollector. When the member inkCollect is instantiated, I pass this indicating that I want the InkCollector control to be attached to the Form. Finally, I set inkCollects Enabled property to true indicating that the control should start collecting ink.
One important point to note is that the InkCollector control internally depends upon unmanaged resources (COM objects) hence it is essential to manually free the resources once the control is no longer required. Hence I override the Dispose method on the Form and dispose the inkCollect object.

Now lets compile this application using csc /r:Microsoft.ink.dll FirstApp.cs

Note: If compilation fails make sure you have setup the correct paths as mentioned above. You can also you the following command to compile the application:
csc /r:"C:\Program Files\Microsoft Tablet PC Platform SDK\Include\microsoft.ink.dll" FirstApp.cs


Now run the application calling FirstApp at the command prompt. You will see the normal blank Form, but observe the cursor it will be displayed as a dot. You can use you mouse to write/draw on the Form using Ink!! Thats how simple it is to build your first pen-enabled application.

Power of the PEN!


Figure 1: Hello World in Ink (on Workstation)

The screen shot above shows Hello World!!! written in ink using a Mouse on a standard desktop machine. You will notice couple of things about this (besides my handwriting looking like a childs scribble).
1) Its difficult to write text using mouse as input
2) Mouse does not give me good writing control hence everything I write appears jagged.
 


Figure 2: Hello World in Ink (on Tablet PC)

If you look at the screen shot above (figure 2), this has been taken from my Tablet PC using a pen. If you compare the text on two screen shots you will see that not only does the ink from the pen look more readable but its smoother. This magic is done because of the following reasons:

1) While using a mouse, the rate at which packets are sent to the PC is very low like 30-35 samples per second. This is enough for general tasks, but not good enough for writing text. On the tablet pc the digitizer sends samples at the rate above 133 samples per second. Essentially sending the tablet enough information to make your handwriting appear life like.

2) The Tablet PC OS has been built in a way to accept this high frequency of packets on a high priority thread hence the ink flows as your pen moves rather than having some delay making it a natural writing experience. On top of this, in real time the Ink from the pen is smoothened and anti-aliased to give a natural feel.

Ink Ink Ink What is Ink?
I have been referring to text written in the sample above as Ink. But you could very well open MS Paint (or any drawing tool for that matter) and write/draw similarly since Windows 3.0, would that also be considered as Ink? Whats so special about Ink?

Ink on the Tablet PC platform, is a collection of strokes made by the users pen. It not only handles the visualization of strokes made by the pen but it is also a data type. Each stroke contains other valuable information which allows us to build rich applications that can recognize text, perform various actions based upon the sequence/type of strokes etc.
Also by virtue of being a data type you can now store ink (serialize the ink to a file/database), copy ink into other applications (like Word), perform analysis on it etc.

And what is a stroke?
A stroke is the set of data captured in a single pen/mouse down, up, move sequence. It would consist of the set of points consisting of the stroke as well as other packet data.

In the listing below I have enhanced the sample to show the count of strokes made on the InkCollector. So after you make stroke/strokes you can click on the Count Strokes button to give you the count of strokes made on the InkCollector object.

using System ;
using System.Windows.Forms ;
using Microsoft.Ink ;
using System.Drawing ;

namespace MasterCSharp.TabletPC.Samples
{

  public class FirstApp : Form
  {
    private InkCollector inkCollect ;
    private Button countStrokes;

    public FirstApp()
    {
      // Create and add a new Button to the form
      countStrokes = new Button();
      countStrokes.Text = "Count Strokes" ;
      countStrokes.Size = new Size(90,25);
      countStrokes.Click += new EventHandler(countStrokes_Click);
      this.Controls.Add(countStrokes);

      inkCollect = new InkCollector(this);
      inkCollect.Enabled = true ;

    }
	
    public static void Main()
    {
      Application.Run( new FirstApp() );
    }

    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if( inkCollect != null )
          inkCollect.Dispose();			 
      }
      base.Dispose( disposing );
    }

    private void countStrokes_Click(object sender, EventArgs e)
    {
      // Display the count of strokes
      MessageBox.Show( inkCollect.Ink.Strokes.Count.ToString() );
    }
  
  }
}

In the sample above, I use the Count property of the Strokes collection of the Ink object associated with the InkCollector to get the count of the strokes made.
Recompile the sample with the same switch given above and run it.


Figure 3: Stroke Count (on Workstation)

In the screen shot above the number 7 indicates that 7 strokes were used to write the word Hello. 3 for H and one each for e,l,l and o.
As you can see from the example above each stroke made is stored separately, this allows us to build interesting applications where we could process each of the strokes coming from the pen.

Conclusion
Hopefully this article gave you a quick start on developing applications on the Tablet PC Platform. In future articles we will start to explore different aspects of the platform deeply.

Comments

Add Comment