HomeDigital EditionSearch Dotnet Cd
ASP.NET C# Certification Exams The CLI Data Access Editorials Extending .NET Fundamentals Interoperability Interviews Migrate Mobile .NET Mono .NET Interface Object-Oriented Programming Open Source Optimization Product/Book Reviews Security Source Code UML Visual Studio .NET

The introduction of the Tablet PC has given developers and end users an entirely new way to interact with their computers: pen and ink. The concept of pen-and-ink input is not new ­ pen-based computing has been around for some time ­ but what the Tablet PC does is combine the latest in technology with ink support embedded right in the operating system, raising the bar on user experience to a new level. Many developers are wondering what this means for them, as if the transition from ASP to ASP.NET or VB6 to VB.NET were not enough to learn already.

One inhibitor to the acceptance of pen-based computing is the tremendous effort a developer must undertake to work with ink. Think of all the complexities that need to be addressed: tracking of the pen tip, the drawing of ink, the size and style of the pen type, the ink color ­ the list could go on and on. Add to this list the advanced features the Tablet PC supports, such as handwriting recognition, a pressure-sensitive pen tip, and the ability to scale, move, and rotate strokes, just to name a few.

Thankfully, Microsoft has already addressed most of these issues and provides an extensive library of both managed and unmanaged classes for working with ink. This article will introduce you to exploring these libraries by walking you through the creation of a basic ink-enabled application.

The Tablet PC operating system does not require developers to learn a new environment to create applications. The Tablet PC OS is a superset of the Windows XP operating system. This means your entire set of Windows Forms and legacy applications will run natively on the Tablet PC.

Besides running all of your .NET and COM applications, you can continue to use the extensive collection of quality third-party controls ­ or your own libraries ­ to build Tablet PC applications. Look for top-tier component vendors to fully support ink in their components in the near future, extending basic ink controls provided by Microsoft.

To begin creating custom controls or applications for the Tablet PC, developers can download the Tablet PC SDK 1.5 from www.tabletpcdeveloper.com. The Tablet PC SDK requires Windows 2000, XP, or XP Tablet Edition to run. The SDK allows developers to create ink-enabled applications using either the mouse or a third-party digitizer on a computer not running XP Tablet Edition, but will throw an exception if the handwriting recognition is used on a non-XP Tablet Edition OS.

Once the Tablet PC SDK is installed you need only start up Visual Studio.NET and set a reference to the Microsoft.Ink assembly in a standard Windows Forms project. This assembly contains all of the ink-related libraries and classes.

One of the main objects is the InkCollector. This object ­ and its replacement, the InkOverlayform ­ is the foundation of an ink-enabled application. To enable an area to accept and process ink, a handle is passed to the InkCollector and the InkCollector's enable property is set to true.

Private WithEvents InkArea As InkOverlay

Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
InkArea = New InkOverlay(GroupBox1.Handle)
InkArea.Enabled = True
End Sub

The InkOverlay object, a superset of the InkCollector object, is used for providing editing support. It is useful for applications in which one wishes to work with ink that is neither handwriting nor gestures.

Once an InkCollector or InkOverlay is enabled it will handle most of the ink functionality on behalf of the developer; these objects' sole purpose is collecting ink input from the hardware and passing it on to the application.

Now that the area of the group box is ink enabled, place three buttons on the form to allow the input mode to be switched. These three buttons will be labeled erase, select, and write.

The code behind for each of these buttons sets the EditingMode property of the InkOverlay object to one of the values in the InkOverlayEditingMode enumeration.

In addition to setting the EditingMode value you need to set the EraseMode property for the delete button. Using the values in the InkOverlayEraseMode enumeration developers can select either stroke or point erasing. Stroke erase will delete the entire stroke when the eraser is passed over it. Point erase mode will delete only the section of the stroke below the eraser.

Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
InkArea.EraserMode = InkOverlayEraserMode.PointErase
InkArea.EditingMode = InkOverlayEditingMode.Delete
End Sub

Private Sub Button2_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button2.Click
InkArea.EditingMode = InkOverlayEditingMode.Select
End Sub

Private Sub Button3_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button3.Click
InkArea.EditingMode = InkOverlayEditingMode.Ink
End Sub

With the basic functionality taken care of with just a few lines of code, the application can be extended to handle some more advanced functionality, such as handwriting recognition. The robust nature of the Tablet PC SDK makes this simple, too.

While there are more advanced and robust ways to invoke various recognizers that may be installed on a Tablet PC, the easiest way to do this is to call the ToString method of the Ink object. The ToString method invokes the unit's default recognizer and returns the text value for the ink.

To further explore the managed ink libraries with this application, the InkOverlay's SelectionChanged event is used to trigger the handwriting recognition. The SelectionChanged event is fired after a user clicks on the selection button, which was added earlier and highlights a section of text. At this point the event fires, and the circled text is converted to text.

Private Sub Ink_SelectionChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles InkArea.SelectionChanged
Label1.Text = InkArea.Selection.Ink.Strokes.ToString
End Sub

The ink objects provide developers with a robust collection of events. In addition to the SelectionChanged event, this application will use the NewPackets event.

The InkOverlay raises two main packet-based events, NewPackets and NewInAirPackets. A packet is the collection of properties of a stroke as it is being collected. Packet-based events can occur in rapid succession, so developers must be careful with the performance of the code that is placed here. Poor-performing code in a packet event can slow the entire application to a crawl.

The NewPackets event is raised as packets are collected as a result of a user writing or drawing in the ink area. The NewInAirPackets event is similar but is raised when the pen is hovering or passing over the ink area.

In the sample application the NewPacket event is used to add a bit of color and fun; each pen stroke captures and paints a random color.

Using the stroke collection that is passed as an argument to the NewPacket event, a developer can access most of the stroke's properties and methods. For this example the DrawingAttributes property is used. With DrawingAttributes, developers can set a wide range of settings, such as color, pen tip, or pressure sensitivity.

During the NewPacket event in this application we set the color of the stroke to a randomly generated color.

Private Sub InkArea_NewPackets(ByVal sender As Object,
ByVal e As Microsoft.Ink.InkCollectorNewPacketsEventArgs)
Handles InkArea.NewPackets
Dim Random As New Random

e.Stroke.DrawingAttributes.Color =
Color.FromArgb(Random.Next(0, 255),
Random.Next(0, 255), Random.Next(0, 255))
End Sub

The true power of the Tablet PC lies not in its ability to transform ink into text but rather in its ability to understand ink as a native data type. Handwriting recognition is a powerful feature, but the truth is that most people relate to ink naturally. From early on in our lives, handwriting is taught ­ to the point that it becomes second nature. The Tablet PC SDK allows developers to do some interesting things with ink, such as zooming, rotation, and hit tests.

With the addition of two more command buttons, this sample application can be expanded to support some advanced ink features. Label the buttons "move" and "rotate".

Like most of the other features of the Tablet PC SDK, basic movement and rotation are made to be simple. The Stroke object exposes a move and rotate method.

With a click event of the move button the application will move every stroke on the screen down and to the right. While this is a very basic movement, this concept can be applied to more advanced and real-world examples.

Private Sub Button4_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button4.Click
Dim Stroke As Stroke

For Each Stroke In
InkArea.Ink.Strokes
Stroke.Move(100, 100)
Next

Refresh()
End Sub

The rotate button will be used to enable and disable a timer that is placed on the form. The timer's event will be used to rotate the entire stroke on the page.

Private Sub RotateTimer_Tick(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles RotateTimer.Tick
If Not InkArea.CollectingInk Then
RotateTimer.Enabled = False

Dim Stroke As Stroke

For Each Stroke In InkArea.Ink.Strokes
Stroke.Rotate(10,
Stroke.GetPoint(CType(Stroke.GetPoints().Length / 2, Int32)))
Next

Refresh()

RotateTimer.Enabled = True
End If
End Sub

The rotate method takes two parameters, the number of degrees to rotate the stroke in a clockwise direction, and a point to rotate the stroke around. The timer event will rotate the stroke around the middle point of the stroke. This point is figured by using the GetPoints method to return all of the points of the stroke, the count of which is divided in half. After the index of the middle point is computed this value is passed to the GetPoint method.

Conclusion
While most developers will not be creating applications to rotate multicolor text, this basic application does highlight some of the powerful features of the Microsoft ink libraries. In just a few simple lines of code this application has taken a basic group box and turned it into an ink-enabled writing area with advanced features like handwriting recognition, movement, and rotation.

This ease of use highlights the work that Microsoft has completed, allowing developers to easily leverage their existing skill sets, presentation layer controls, and existing applications while extending into the next generation of applications. Even developers without direct access to a Tablet PC can download the Tablet PC SDK and experiment with and learn ink technology today.

About The Author
Brad McCabe is a technology evangelist for .NET, ASP.NET, and .NET CF for Infragistics (www.infragistics.com), a leader in providing a broad infrastructure of reusable presentation-layer components essential for the creation of next-generation Web-based applications and XML Web services utilizing .NET, COM, and Java. brad@infragistics.com

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.

  E-mail: info@sys-con.com