Browse DevX
Sign up for e-mail newsletters from DevX


Creating Tablet PC Applications with VS .NET : Page 4

The Tablet PC platform supports applications with special tablet features enabled, such as ink input and pen-based operation. In order for this platform to become truly popular, third-party vendors will also have to ink-enable their applications. Find out how.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Interacting with the Ink
At this point, you have a fully functional ink notepad that can collect ink in different colors. Users also expect to be able to interact with the ink as they can with any other type of data in Windows. This includes selecting and modifying ink, as well as erasing ink. The InkOverlay class supports all these operations natively by supporting three main modes of interaction: Ink, Select, and Delete. The current interaction mode is set through the EditingMode property. You can simply add a drop-down list to the form that allows the user to switch between the three different modes. To do this, I dropped a ComboBox on the form, changed its DropDownStyle to DropDownList, and added three entries to its Items collection called Edit, Select, and Erase. Whenever the user picks one of those items, switch the mode using the following code:

private void lstEditMode_SelectedIndexChanged; ( object sender, System.EventArgs e) { switch (lstEditMode.SelectedIndex) { case 0: this.oInk.EditingMode = InkOverlayEditingMode.Ink; break; case 1: this.oInk.EditingMode = InkOverlayEditingMode.Select; break; case 2: this.oInk.EditingMode = InkOverlayEditingMode.Delete; break; } }

At this point, you have run into one of those tablet-specific ways of interaction: many pens feature an end-of-pen eraser. This allows the user to turn the pen around and use the end to erase ink. This is what the user expects to happen, and it is quite straightforward to implement; but it doesn't come for free. Basically, your application needs to listen for pen-related events fired by the InkOverlay, and whenever you notice that the pen is inverted (turned around), your application must automatically switch the controls' edit mode to delete. The key to implementing this lies in the CursorInRangeEvent. This event fires every time the cursor (pen) moves into the range of the digitizer. The event provides a number of parameters, one of which is a Cursor object. This object contains all kinds of information about the pen, including whether or not it is inverted.

The combination of great handwriting recognition and a fault-tolerant interface design generate amazingly good user experiences, even in database applications.
The first step in the implementation process is to hook up an event handler right after you instantiate the InkOverlay object. Here is the code that does that:

this.oInk.CursorInRange += new ; InkCollectorCursorInRangeEventHandler ( this.oInk_CursorInRange )

This wires the CursorInRange event to an event handler method called oInk_CursorInRange. The code in this event handler method is straightforward:

private void oInk_CursorInRange ( object sender, InkCollectorCursorInRangeEventArgs e) { if (e.Cursor.Inverted == true) { if (oInk.EditingMode != InkOverlayEditingMode.Delete) { oInk.EditingMode = InkOverlayEditingMode.Delete; } } else { if (oInk.EditingMode == InkOverlayEditingMode.Delete) { oInk.EditingMode = InkOverlayEditingMode.Ink; } } }

The code inspects the Inverted property of the Cursor object. If the pen is inverted and the InkOverlay is not in delete mode, switch it. The opposite is true as well. If the pen is not inverted, but the InkOverlay is in delete mode, set it back to ink (edit) mode. This solution is not entirely perfect. The user could start out in selection mode, then, turn the pen around to erase ink, and then turn it around again to continue selecting ink. This would not work as written because it switches to edit mode instead. This problem can be fixed easily by memorizing the previous state in a field on the form.

This works slightly differently in Visual Basic .NET. There are two options to consider here. One is to hook up an event handler manually, similar to how it is done in C#. The other option is to declare the reference to the InkCollector object "with events." This allows you to define a method that handles the fired event without having to worry about the underlying plumbing. To do so, you have to change the definition of the reference to the InkOverlay object:

Private WithEvents oInk _ As Microsoft.Ink.InkOverlay

Now, you can simply define a method than handles this event:

Private Sub oInk_CursorInRange( _ ByVal sender As Object, _ ByVal e As _ InkCollectorCursorInRangeEventArgs) _ Handles oInk.CursorInRange ' Event code goes here End Sub

This demonstrates the mechanics of hooking up the event handler code. The actual implementation of the end-of-pen erasing algorithm is nearly identical to the C# version (and you can download the complete VB.NET code sample from the CoDe Magazine Web site).

There is one more little detail about erasing ink that is interesting to know. By default, deletion occurs on a stroke-by-stroke basis. This means that if the user writes a whole word in one stroke, the whole world will be deleted as soon as it is touched by an eraser anywhere. This may or may not be what the user expects. Certainly, in the simulation of real pen and paper, one could argue that only the sections (pixels) of ink that are touched by the eraser should be removed, and the rest of the stroke should stay intact.

The Ink SDK supports both ways of erasing. The behavior you desire is set using the EraserMode property, using InkOverlayEraserMode.PointErase and InkOverlayEraserMode.StrokeErase respectively. The mode you choose may depend on the needs of your application. In the ink notepad example, setting it to PointErase may be the better idea, but other needs (such as in the inventory management application described below) may be better off with the default StrokeErase. In some applications, the decision may be hard to make and should be left to the user.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date