Docstoc

Getting Started with the Tablet PC

Document Sample
Getting Started with the Tablet PC Powered By Docstoc
					Hands-On Lab
Lab Manual
Getting Started with the Tablet PC (C#)




Release Date: May 2009
This is a preliminary document and may be changed substantially prior to final commercial release of the software described
herein.
The information contained in this document represents the current view of Microsoft Corporation on the issues discussed as of
the date of publication. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a
commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after the date
of publication.
This White Paper is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR
STATUTORY, AS TO THE INFORMATION IN THIS DOCUMENT.
Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no
part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any
means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written
permission of Microsoft Corporation.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject
matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this
document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.
Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people,
places and events depicted herein are fictitious, and no association with any real company, organization, product, domain
name, email address, logo, person, place or event is intended or should be inferred.
 2006 Microsoft Corporation. All rights reserved.
Microsoft, are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries.
The names of actual companies and products mentioned herein may be the trademarks of their respective owners.




                                                            Page i
Contents

LAB 1: OVERVIEW OF TABLET PC'S ..................................................................................................................... 1
  Lab Objective ......................................................................................................................................................... 1
  Overview of Tablet PC Technology........................................................................................................................ 1
  Exercise 1: Quick Tour of the Tablet PC ................................................................................................................ 1
    Task 1: Practice using Tablet PC Input Panel ................................................................................................... 1
    Task 2: Become familiar with Tablet PC Platform ............................................................................................. 3
  Lab Summary ......................................................................................................................................................... 5

LAB 2: CREATING AN INK-ENABLED FORM ........................................................................................................ 6
  Lab Objective ......................................................................................................................................................... 6
  Exercise 1: Set Up a Project .................................................................................................................................. 6
    Task 1: Start the Lab .......................................................................................................................................... 6
    Task 2: Add a reference to the Tablet PC SDK Platform libraries ..................................................................... 6
    Task 3: Add ink controls to the Visual Studio Toolbox ...................................................................................... 7
    Task 4: Review Controls on the Form ............................................................................................................... 7
    Task 5: Add an InkPicture Control to the form ................................................................................................... 7
    Task 6: Set the InkPicture properties ................................................................................................................. 7
  Enable Ink Collection on the Form ......................................................................................................................... 8
    Task 7: Create and attach an InkOverlay object................................................................................................ 8
    Task 8: Run the project ...................................................................................................................................... 9
  Lab Summary ......................................................................................................................................................... 9

LAB 3: WORKING WITH THE INK DATA............................................................................................................... 10
  Lab Objective ....................................................................................................................................................... 10
  Exercise 1: Working with Ink Data ....................................................................................................................... 10
    Task 1: Create drawing attributes for the pen and highlighter ......................................................................... 10
    Task 2: Assign the default pen drawing attributes ........................................................................................... 11
  Enable Button Functionality to the Toolbar .......................................................................................................... 11
    Task 3: Review the ToolBar on the Form ........................................................................................................ 11
    Task 4: Review the ToolBar’s ButtonClick EventHandler ................................................................................ 12
    Task 5: Add functionality for Highlighter button ............................................................................................... 12
    Task 6: Add functionality for Medium Point button .......................................................................................... 13
  Enable Different Editing Modes ............................................................................................................................ 14
    Task 7: Write the Select mode code ................................................................................................................ 14
  Enable Ink Persistence......................................................................................................................................... 14
    Task 8: Add functionality for the Save button .................................................................................................. 14
  Lab Summary ....................................................................................................................................................... 15

LAB 4: USING HANDWRITING RECOGNITION .................................................................................................... 16
  Lab Objective ....................................................................................................................................................... 16
  Create a RecognizerContext object and a Strokes collection .............................................................................. 16
    Task 1: Add a TextBox control to the form ...................................................................................................... 16
    Task 2: Create variables for the RecognizerContext object and the Strokes collection .................................. 16
    Task 3: Create a RecognizerContext object and associate the Strokes collection with it ............................... 17
    Task 4: Register delegates to handle stroke and recognition events .............................................................. 17
    Task 5: Implement event handlers for Stroke and Recognition events ........................................................... 17
  Lab Summary ....................................................................................................................................................... 18



                                                                                Page ii
Lab 1: Overview of Tablet PC's
Lab Objective
15 minutes

There are two objectives of this lab. The first objective is to introduce you to the features and benefits of
Tablet PC's and to describe the benefits of developing applications for Tablet PC form factor. The
second objective is to introduce basic concepts of working with the Tablet PC platform.

           Note If you are familiar with the functionality and input mechanisms of the Tablet PC, you may
           skip Lab 1 and go directly to Lab 2, where you will begin to develop a Tablet PC application. If
           you are not familiar with Tablet PC functionality or input mechanisms of the Tablet PC, we
           strongly urge you to complete Lab 1 before moving on.

    •    Overview of Tablet PC's
    •    Introduction to the Tablet PC platform

Overview of Tablet PC Technology

The following items introduce you to Tablet PC's.

•       The Tablet PC is the evolution of notebook computing. If you want the reliability and power of
        Windows with a natural interface and powerful mobility capabilities, a Tablet PC is the solution for
        you.
•       A Tablet PC provides the full capabilities of Windows, plus additional features for pen-based
        computing on a Tablet PC. And, because it uses the Windows operating system, any application
        that runs on a computer running Windows also runs on a Tablet PC.
•       The Tablet PC generally comes in two basic hardware formats: the convertible format, which
        includes an integrated keyboard, and the ultra-slim slate, which has innovative docking solutions for
        easy access to a keyboard while you are at your desk. Windows supports grab-and-go docking and
        has a fast resume-from-standby time. These capabilities, combined with wireless network support,
        give your applications greater mobility and immediate access to the full power of a computer.
•       The Tablet PC provides a powerful platform for developers and the foundation for a new generation
        of applications with pen and ink capabilities. For example, there are powerful yet simple ink controls
        and application programming interfaces (APIs). You can use these APIs to add pen and ink
        capabilities to existing applications and to develop new applications that take advantage of this
        technology.


Exercise 1: Quick Tour of the Tablet PC
Task 1: Practice using Tablet PC Input Panel



                                                      Page 1
Tablet PC Input Panel enables you to interact with applications that are not designed for the Tablet PC.
Think of Input Panel as an on-screen keyboard that lets you use your tablet pen to enter text and
control your computer. Using a tablet pen and Input Panel, you can write directly on the screen and
convert your handwriting to typed text for use in other applications. The following steps are an example
of using Input Panel to enter text and to control your computer.




            Figure 1.1: Tablet PC Input Panel Icon to the right and below the insertion point.
1. Using the pen, tap the Start menu, tap All Programs, tap Accessories, and tap Notepad.
2. Bring the pen within range of the insertion point. The Tablet PC Input Panel icon appears.
3. Tap the Input Panel icon. Input Panel appears, as in figure 1.2




                                    Figure 1.2 Tablet PC Input Panel.

4. If it is not already selected, select the Writing Pad      in Input Panel. Note that you can also
   choose a keyboard or a letter comb to input text.
5. Use the pen to write some words on the guide line on Input Panel. Notice that if you need more
   room, Input Panel automatically expands.
6. Input Panel previews the recognized text, as shown in figure 1.3




                                 Figure 1.3 Preview of recognized text.



                                                  Page 2
7. Tap the preview text for one of the words. Input Panel's correction interface appears, as shown in
   figure 1.4.




                             Figure 1.4 Input Panel's correction interface.
8. You can change the recognized string by tapping one of the suggestions, rewriting individual letters,
   or rewriting the entire word. When you are done exploring, click OK on the correction interface.
9. Tap Insert. The recognized text appears in Notepad.
10. Close Notepad.

Task 2: Become familiar with Tablet PC Platform

With the Tablet and Touch Technologies in the Windows SDK, you can create applications that are
designed to run on a Tablet PC. The Tablet and Touch Technologies includes libraries, headers, and
redistributable merge modules for setup projects, sample code, and documentation. In addition to
Windows Vista versions and Windows XP Tablet PC Edition, you can install the Tablet PC SDK on a
computer running Windows XP Professional, Windows Server™ 2003, and Windows 2000.
   Note: You may use the handwriting recognition features on a supported computer, other than one
   running Windows XP Tablet PC Edition, however you must first install the Windows XP Tablet PC
   Edition 2005 Recognition Pack, available as a free download.
   1. To open the Mobile PC and Tablet PC Development documentation, tap the Start menu, tap All
      Programs, tap Microsoft Windows SDK, and then tap Windows SDK Documentation.
   2. Expand the Win32 and COM Development node, expand the Mobile PC node, and then
      expand the Mobile PC node.




                                                Page 3
                            Figure 2.1 Tablet PC table of contents.


3. Browse the documentation to get familiar with the layout of the information. The documentation
   describes how to create Tablet PC applications that are easy to use. It also includes reference
   information and information about how to access the sample code, how to use the APIs, and
   more.
4. Tablet and Touch Technologies have managed code in the following namespaces within the
   .NET Framework 3.0:
        • Microsoft.Ink
        • Microsoft.Ink.TextInput
        • System.Windows.Controls (InkCanvas and InkPresenter)
        • System.Windows.Ink
        • System.Windows.Ink.AnalysisCore
        • System.Windows.Input.StylusPlugIns
5. Close the documentation.




                                            Page 4
Lab Summary
In this lab, you performed the following tasks:

 •   Learned about the Tablet PC
 •   Practiced using Tablet PC Input Panel
 •   Became familiar with the Tablet PC SDK

In this lab, you learned that all of your Windows-based applications can run on a Tablet PC. You also
discovered the power of ink-enabled applications. You saw how you use Tablet PC Input Panel to
enable pen input in applications that are not ink-enabled. Finally, you became familiar with the Tablet
and Touch Technologies in the Windows Vista SDK, which enables you to write your own applications
for the Tablet PC.




                                                  Page 5
Lab 2: Creating an Ink-Enabled Form
In this and the following Lab you will create a simple application that a doctor might use to enter
prescriptions on a Tablet PC.

Lab Objective
This lab will take approximately 15 minutes to complete.
The objectives of this lab are:
•       To learn how to add a reference to the Tablet PC SDK libraries in a Microsoft Visual Studio® project.
•       To create ink-enabled regions on a form in a Microsoft Visual C#® project by using controls and objects from the
        Tablet PC Platform SDK.
•       To improve recognition results by using factoids.

You will create a prescription form for a doctor that includes a date field, a prescription field, and a
signature field.

    •    Set up the project
    •    Add controls to a form
    •    Enable controls
    •    Run the ink-enabled application

Exercise 1: Set Up a Project

In this exercise, you will open an existing Visual Studio project and add a reference to the Tablet PC
SDK managed library.


Task 1: Start the Lab

        1. Open Visual Studio 2005.
        2. Click the File menu, point to Open, and then click Project/Solution.
        3. Navigate to Lab 2 in C:\HOL\VISTA_TABLET_CS\LAB2.
        4. Click Lab2.sln, and then click Open.
        5. The solution opens.

Task 2: Add a reference to the Tablet PC SDK Platform libraries

        1. To add a reference to the Tablet PC libraries, on the Project menu, click Add Reference.
        2. In the Add Reference dialog, click the Browse tab, navigate to C:\Program Files\Common
           Files\Microsoft Shared\ink, select Microsoft.Ink.dll, and then click OK. (If the reference is already
           selected, click Cancel.)
           Note For computers with Windows XP or earlier versions of the Windows SDK, the correct file
           path is C:\Program Files\Common Files\Microsoft Shared\ink\1.7.

                                                           Page 6
   3. Make sure the code for Form1.cs is visible. If it is not, right-click Form1.cs, and then click View
      Code.
   4. Add the following line below the other using statements at the top of the file to reference the Ink
      namespace.
      using Microsoft.Ink;




Task 3: Add ink controls to the Visual Studio Toolbox

   1. Click the Form1.cs [Design] tab to switch back to Design view. If the tab is not available, on the
      View menu, click Designer.
   2. To add the ink controls to the Toolbox, right-click within the Toolbox window and then click
      Choose Items.
   3. On the .NET Framework Components tab, click Browse, and then select Microsoft.ink.dll
      from C:\Program Files\Common Files\Microsoft Shared\ink. Click OK.
   4. Select the check boxes for the InkEdit and InkPicture controls, and then click OK.
   5. Save the project and leave it open.

Task 4: Review Controls on the Form
   1. This project has been pre-loaded with several controls. These controls are all windows
      standard controls and include a GroupBox and a TextBox.

Task 5: Add an InkPicture Control to the form
   1. Drag an InkPicture control from the ToolBox (the newly added items will be at the bottom of the
      list) to the form’s lower right corner, outside of the boundaries of the GroupBox.
   2. Set the size of the InkPicture (now named InkPicture1) to 368, 80
   3. Set the Location of the InkPicture to 256,352.
   4. Save the project and leave it open.

Task 6: Set the InkPicture properties

   1. Set the Image property of the InkPicture control. Select the InkPicture control on the form,
      click Image in the Properties pane, and then click the ellipses button (...) to browse for the
      image.
   2. In the Select Resource dialog, select Local Resource, click Import, select
      InkPictureBackground.gif (located in C:\HOL\ VISTA_TABLET_CS), and then click Open.
      Your form should now look like the following figure:




                                                 Page 7
                      Figure 2.2: The form with InkEdit and InkPicture controls


Enable Ink Collection on the Form
In this task, you will create an InkOverlay object and attach it to the GroupBox control window to
enable inking on the control. You can attach an InkOverlay object to any control's Window handle.

Task 7: Create and attach an InkOverlay object

   1. On the View menu, click Code.
   2. Add a private member variable for the InkOverlay object near the top of the form class. Insert
      the following code immediately after the declarations for the TextBox, Label, InkPicture and
      GroupBox controls.

       private Microsoft.Ink.InkOverlay inkOverlay1;




                                                Page 8
     3. Scroll to the point in Form1's constructor where you see the comment:

        // Add InkOverlay Code Here


     4. Add the following code inside the constructor after the comment. This will create a new
        InkOverlay object and assign it to the groupBox1 control's Window handle.

        inkOverlay1 = new Microsoft.Ink.InkOverlay(groupBox1.Handle);
        inkOverlay1.Enabled = true;


Task 8: Run the project

     1. Save the project.
     2. To compile and run the project, click Start Debugging on the Debug menu.
     3. Using the pen, tap the date TextBox to ensure that it has input focus.

     4. Hover over the date TextBox. The Input Panel icon appears          . Tap the icon to launch Input
        Panel next to the TextBox. Write today’s date in Input Panel's writing pad, and then tap Insert.
        Input Panel inserts the recognized text into the TextBox. To dismiss Input Panel, explicitly close
        Input Panel or move the pen outside the Input Panel window.
     5. Scribble in the GroupBox control.
     6. In the Signature box, write a signature.
     7. Close the application.
     8. Click the File menu, and then click Close Solution. You need not save any changes.

        Note: You will add selection and other features in the next lab.


Lab Summary
In this lab, you performed the following tasks.

 •    Added a reference to the Tablet PC SDK libraries to a Visual Studio
      project
 •    Made your application ink-enabled by adding an InkPicture control to a
      form and setting properties on this control
 •    Attached an InkOverlay object to a control to make the control ink-
      enabled
 •    Ran the ink-enabled application

In this lab, you have created a Windows Forms application in C# that uses basic handwriting
recognition and ink data. In the next labs, you will work with the ink data and perform more advanced
handwriting recognition tasks.




                                                   Page 9
Lab 3: Working with the Ink Data
Lab Objective
This lab will take approximately 15 minutes to complete.
The Tablet PC APIs expose the data that is collected from the pen with a rich object model. The
objective of this lab is to teach you how to work with ink data that is collected in your form. You will also
learn how to make use of two of the three editing modes of the InkOverlay object.

 •    Set up the drawing attributes for a medium-width pen and a highlighter
      pen
 •    Create and enable a ToolBar to manipulate ink data
 •    Change the editing mode of the InkOverlay object to enable selection
 •    Save ink data to a GIF file

Exercise 1: Working with Ink Data
In this exercise, you will set up the drawing attributes for a medium-width pen and a highlighter pen.

Task 1: Create drawing attributes for the pen and highlighter

     1. In Visual Studio, click the File menu, point to Open, and then click Project/Solution.
     2. Navigate to Lab 3 in C:\HOL\VISTA_TABLET_CS\LAB3. Click Lab3.sln, and then click Open.
     3. The solution opens.

To simplify changing from one type of pen to another, you will create a pair of DrawingAttributes class
instances to hold the attributes for each pen type.
     1. Right-click Form1.cs, and then click View Code. Just before the constructor for Form1, add the
        following private variables to hold the drawing attributes.
        private Microsoft.Ink.DrawingAttributes penDrawingAttributes;
        private Microsoft.Ink.DrawingAttributes highlighterDrawingAttributes;

     2. Scroll to within the form’s constructor, and just after the line that contains the
        InitializeComponent() call, insert the following code. This sets the attributes for the normal pen
        to black and the width to 50. This value is expressed in HIMETRIC units. Ink space coordinates
        is the virtual coordinate system used by ink, in which one HIMETRIC unit equals .01 mm.
        penDrawingAttributes = new Microsoft.Ink.DrawingAttributes();
        penDrawingAttributes.Color = Color.Black;
        penDrawingAttributes.Width = 50;

     3. Add the following code just after the code in Step 2. These lines set the attributes for a
        highlighter pen that will render nicely when drawn over other ink on the form by setting the
        Transparency and RasterOperation properties correctly. RasterOperation returns or sets
        how the pen color interacts with the existing background colors on the display when ink is
        drawn. The MaskPen value performs subtractive transparency. The value of 200 for

                                                  Page 10
        transparency is out of a possible 255. The Width and Height properties are specified in
        HIMETRIC units.
        highlighterDrawingAttributes = new Microsoft.Ink.DrawingAttributes();
        highlighterDrawingAttributes.Color = Color.Yellow;
        highlighterDrawingAttributes.PenTip = PenTip.Rectangle;
        highlighterDrawingAttributes.Width = 75;
        highlighterDrawingAttributes.Height = 1000;
        highlighterDrawingAttributes.Transparency = 200;
        highlighterDrawingAttributes.RasterOperation = RasterOperation.MaskPen;

Task 2: Assign the default pen drawing attributes

   1. To make the medium point pen the default attribute, add the following code to set the drawing
      attributes in the constructor for Form1, just after the code that sets inkOverlay1.enabled equal
      to true.
        inkOverlay1.DefaultDrawingAttributes = penDrawingAttributes;


Enable Button Functionality to the Toolbar
In this exercise, you will add a ToolBar and enable functionality so that a doctor can switch between a
medium and fine point pen, select, highlight, and erase ink. This will provide the user interface for a
doctor to ink on the prescription form.

Task 3: Review the ToolBar on the Form
   1.   Please notice that the form has an existing toolbar control that contains four buttons: Save, Highlighter, Medium
        Point, and Select. If you do not see the buttons, please make sure you have selected “lab 3” in the toolbar.




                                                       Page 11
                            Figure 3.1: The form with ToolBar control

Task 4: Review the ToolBar’s ButtonClick EventHandler
   1. Please review the ButtonClick event hander for toolBar1. The handler contains a Select
      statement that handles all of the possible button clicks from toolBar1.

Task 5: Add functionality for Highlighter button

   1. Edit the code in the Highlighter case of the switch statement in the toolBar1_ButtonClick
      method as follows:
      case "Highlighter":
             inkOverlay1.DefaultDrawingAttributes = this.highlighterDrawingAttributes;
             this.toolBarButton3.Enabled = false;
             e.Button.Text = "Pen";


                                             Page 12
              break;

   2. Edit the code in the Pen case of the switch statement in the toolBar1_ButtonClick method as
      follows:
      case "Pen":
             inkOverlay1.DefaultDrawingAttributes = this.penDrawingAttributes;
             this.toolBarButton3.Enabled = true;
             e.Button.Text = "Highlighter";
             break;

   3. To compile and run the project, on the Debug menu, click Start.
   4. Write some ink on the prescription form.
   5. Tap Highlighter to switch to the highlighter pen.
   6. Highlight some ink. Notice how the black ink still shows through the highlighter color. This is
      because of the Transparency property we set earlier:
      highlighterDrawingAttributes.Transparency = 200;

   7. Tap Pen to switch back to a regular pen.
   8. Write some more ink.
   9. Close the application.

Task 6: Add functionality for Medium Point button

   1. Edit the code in the Medium Point case of the switch statement in the toolBar1_ButtonClick
      method as follows:
      case "Medium Point":
             inkOverlay1.DefaultDrawingAttributes.Width = 200;
             e.Button.Text = "Fine Point";
             break;

   2. Edit the code in the Fine Point case of the switch statement in the toolBar1_ButtonClick
      method as follows:
      case "Fine Point":
             inkOverlay1.DefaultDrawingAttributes.Width = 50;
             e.Button.Text = "Medium Point";
             break;

   3. To compile and run the project, on the Debug menu, click Start.
   4. Write some ink on the prescription form.
   5. Tap Medium Point to change the pen width. Note that the text in the button changes to Fine
      Point.
   6. Write more ink and notice the difference in the pen width.
   7. Tap Fine Point to change the pen width back to a finer width. Note that the text in the button
      changes to Medium Point.
   8. Write more ink with the fine-width pen.


                                                 Page 13
   9. Close the application.


Enable Different Editing Modes
In this exercise, you will enable the Select editing mode so that the prescription writer can select ink
and move it around on the form.

Task 7: Write the Select mode code

       1. Edit the code in the Select case of the switch statement in the toolBar1_ButtonClick
          method as follows:
       case "Select":
              inkOverlay1.EditingMode = InkOverlayEditingMode.Select;
              e.Button.Text = "Ink";
              break;

       2. Edit the code in the Ink case of the switch statement in the toolBar1_ButtonClick method
          as follows:
       case "Ink":
              inkOverlay1.EditingMode = InkOverlayEditingMode.Ink;
              e.Button.Text = "Select";
              break;

       3. To compile and run the project, on the Debug menu, click Start Debugging.
       4. Write some ink on the prescription form.
       5. Tap Select to switch to select mode.
       6. Draw a circle around some ink to select it.
       7. Move the selected ink.
       8. Close the application.



Enable Ink Persistence
In this exercise, you will enable the Save button on the ToolBar. The application will save the contents
of the prescription field to GIF format.

Task 8: Add functionality for the Save button

       1. Edit the code in the Save case of the switch statement in the toolBar1_ButtonClick
          method as follows:
       case "Save":
              // Check to see if there is any ink to save
              if (inkOverlay1.Ink.Strokes.Count > 0)
              {
                     // Save the ink data to a GIF
                     byte[] inkData = inkOverlay1.Ink.Save(PersistenceFormat.Gif);
                     // Open up a file to write the GIF data into


                                                  Page 14
                        System.IO.FileStream fs = System.IO.File.Create(
                        "C:\\HOL\\tempink.gif");
                        // Write the data and close the file;
                        fs.Write(inkData, 0, inkData.Length);
                        fs.Close();
               }
               break;


       2. To compile and run the project, on the Debug menu, click Start.
       3. Write some ink on the prescription form.
       4. Tap Save.
       5. Close the application.
       6. View the saved file by using Windows Explorer. On the Start menu, select Run and type
          C:\HOL\tempink.gif to view the persisted ink.
       7. To return to Visual Studio, close the image viewer and Windows Explorer.
       8. In Visual Studio, click the File menu, and then click Close Solution. You need not save any
          changes.



Lab Summary
In this lab, you performed the following tasks:

 •   Added a ToolBar to a form
 •   Created drawing attributes for a medium pen, a fine pen, and a
     highlighter pen
 •   Assigned the default pen drawing attributes
 •   Added functionality for the medium point pen and the highlighter pen
 •   Enabled different editing modes
 •   Learned how to save ink as a GIF file


In this lab, you learned how to work with ink data that is collected in your form. You also learned how to
enable the select editing mode of the InkOverlay object and save ink data to GIF format.




                                                  Page 15
Lab 4: Using Handwriting Recognition
Lab Objective
This lab will take approximately 15 minutes to complete.
You have already learned how to collect ink data and change drawing attributes of ink. In this lab, you
will pass ink data to the Microsoft English recognizer, get the recognized result, and then display the
result in a text box on the form.

 •    Create a RecognizerContext object and a Strokes collection
 •    Pass the ink strokes to the RecognizerContext object
 •    Display the recognition results in a TextBox control

Create a RecognizerContext object and a Strokes collection
In this exercise, you will create an instance of the RecognizerContext class to represent the
handwriting recognition engine. You will then create an instance of the Strokes class, hook up the
strokes from your Ink object to the Strokes collection, and then associate the collection with the
RecognizerContext object.

Task 1: Add a TextBox control to the form

        1. Click the File menu, point to Open, and then click Project/Solution.
        2. Navigate to Lab 4 in C:\HOL\VISTA_TABLET_CS \LAB4. Click Lab4.sln, and then click
           Open.
        3. The solution opens.
        4. Right-click Form1.cs and then click View Designer.
        5. Drag a TextBox control from the Toolbox to the lower left-hand corner of the form.
        6. Clear the Text property of the TextBox control.
        7. Set the Multiline property of the TextBox control equal to True.
        8. Set the Size property of the TextBox control equal to 240,80.

Task 2: Create variables for the RecognizerContext object and the Strokes collection
     1. To switch to the Code Editor window, click Code on the View menu.
     2. Add the following code to the Form1 class, before the Form1 constructor, to create the variables
        for the RecognizerContext object and the Strokes collection.
        private Microsoft.Ink.Strokes strokesToRecognize;
        private Microsoft.Ink.RecognizerContext recognizerContext;

     3. Add the following code to the end of the Form1() constructor (inside the constructor). This
        creates an empty Strokes collection that is filled and passed to the recognizer as new strokes
        are added.

                                                 Page 16
      strokesToRecognize = inkOverlay1.Ink.CreateStrokes();

Task 3: Create a RecognizerContext object and associate the Strokes collection with it
   1. Add the following code to the end of the Form1() constructor (inside the constructor). This
      iterates through the recognizers that are installed on the computer by using the Recognizers
      collection and creates a RecognizerContext object that is associated with the US English
      recognizer. It also associates the Strokes collection from above with the new
      RecognizerContext.
      foreach (Recognizer recognizer in new Microsoft.Ink.Recognizers())
      {
             if (recognizer.Name == "Microsoft English (US) Handwriting Recognizer")
             {
                    recognizerContext = recognizer.CreateRecognizerContext();
                    recognizerContext.Strokes = strokesToRecognize;
             }
      }

      Note This does not work on a computer that is not running Windows Vista or Windows XP
      Tablet PC Edition. No recognizers are installed unless you use a computer running Windows
      Vista, Windows XP Tablet PC Edition, or another supported operating system with the Windows
      XP Tablet PC Edition 2005 Recognition Pack installed.

Task 4: Register delegates to handle stroke and recognition events
   To keep the Strokes collections synchronized, you must update the Strokes collection that is
   associated with recognizerContext to match strokesToRecognize, and you must do so each time
   a new stroke is added to the InkOverlay object on your form.
   1. Inside the Form1 constructor, create an instance of the InkCollectorStrokeEventHandler
      delegate and add it to the Stroke event on the inkOverlay1 object. The event handler will be
      implemented in Task 5.
      inkOverlay1.Stroke += new InkCollectorStrokeEventHandler(inkOverlay1_Stroke);

   2. While still within the Form1 constructor, create a new
      RecognizerContextRecognitionEventHandler delegate and add it to handle the Recognition
      event on the RecognizerContext object. Set interest for the event notification and use it to
      update the text box that displays the recognition results as they are returned.
      if (recognizerContext != null)
      {
        recognizerContext.Recognition +=
          new RecognizerContextRecognitionEventHandler(recognizerContext_Recognize);
      }

Task 5: Implement event handlers for Stroke and Recognition events
   1. Add the following implementation to handle the Stroke event when it is fired by the InkOverlay
      object instance. Place the code within the Form1 class. This code includes logic to determine if
      the stroke is made by a pen or a highlighter. If it is a pen, the Stroke is added to the
      strokesToRecognize collection and the RecognizerContext is forced to perform
      asynchronous recognition. Asynchronous recognition causes a Recognition event to fire from
      the RecognizerContext.
      private void inkOverlay1_Stroke(object sender,
             Microsoft.Ink.InkCollectorStrokeEventArgs e)


                                               Page 17
        {
               // Get the Stroke from the Event Args
               Stroke stroke = e.Stroke;

               if ((stroke.DrawingAttributes.RasterOperation != RasterOperation.MaskPen) &&
                      (inkOverlay1.EditingMode == InkOverlayEditingMode.Ink))
               {
                      strokesToRecognize.Add(stroke);
                      recognizerContext.BackgroundRecognize();
               }
        }

     2. Add the following implementation (also within the Form1 class) to handle the Recognition
        event when the RecognizerContext object fires it. This event handler takes the recognized text
        and places it in the text box you created on the form.
        private void recognizerContext_Recognize(object sender,
               Microsoft.Ink.RecognizerContextRecognitionEventArgs e)
        {
               // Check to see if the method has been called on the UI thread
               // If the method is not on the UI thread, then force the call to
               // the UI thread (using BeginInvoke)
               if (this.InvokeRequired)
               {
                      this.BeginInvoke(new RecognizerContextRecognitionEventHandler(
                      recognizerContext_Recognize), new object[] { sender, e });
                      return;
               }



               // When the recognition occurs, take the recognized text
               // and place it in the text box.
               textBox2.Text = e.Text;
        }

     3. To compile and run the project, click Start on the Debug menu. Write some words in the
        prescription form and notice how the recognition results are added to the text box.
     4. When you are through, please close Visual Studio.



Lab Summary
In this lab, you performed the following tasks:

 •    Created a RecognizerContext object and a Strokes collection to
      perform handwriting recognition
 •    Added a TextBox control to the form.
 •    Created variables for the RecognizerContext object and the Strokes
      collection
 •    Created a RecognizerContext object and associated the Strokes
      collection with it
 •    Registered delegates to handle Stroke and Recognition events
 •    Implemented event handlers for Stroke and Recognition events


                                                  Page 18
In this lab, you learned the basics of how to take strokes from an Ink object and perform handwriting
recognition on them. You also learned how to iterate through the recognition engines that are installed
on a computer and how to listen for the Stroke and Recognition events.




                                                Page 19

				
DOCUMENT INFO
Shared By:
Tags: Tablet
Stats:
views:6
posted:5/29/2012
language:English
pages:22
Description: Getting Started with the Tablet PC