This article discusses the function and use of the RealTimeStylus, which was introduced in the Tablet PC SDK 1.7. Being more of an advanced topic, I will not discuss the basics of the Tablet PC or introduce Tablet PC development with the SDK. Excellent articles are already available, the list of which you can find at the end of this article.
One of the issues developers have encountered in developing applications that utilize Ink is a degradation of performance that occurs when you trap the stylus actions in an event handler. The digitizer has a very high sampling rate of 133 packets per second. Also, some of the events are not switched on until an event handler is attachted. For example, the NewPackets event that fires as the stylus moves across the digitizer.
To solve this problem, the Tablet PC development team has added the RealTimeStylus (RTS) class to the SDK. The RTS allows you to programmatically trap the events produced from the stylus. The RealTimeStylus class provides a queue that holds the events generated from the stylus. This queue gets filled from a separate thread so it will not impact the UI performance.
Another use of the RealTimeStylus is intercepting the Ink to manipulate the way it's rendered.
Locating the RealTimeStylus
The RTS is located in a namespace of its own. While most Tablet PC classes reside in the Microsoft.Ink namespace, you need to use the Microsoft.Stylus namespace for the RealTimeStylus. You get access to both when you add a reference to the Microsoft Tablet PC API in your project.
In order to use the events in the queue, we need to create a class that implements IStylusAsyncPlugin.
Public Class Form1
Implements IStylusAsyncPluginEntering the Implements keyword with the desired interface in VB.NET conveniently inserts all the methods that are required by this interface. We’ll get to the implementation in a moment.
Now we need to create a RealTimeStylus object and attach it to a control on the form. Let’s use the Panel control to attach the RTS to. First the declaration is made in the Form class.
Private myRTS As RealTimeStylus
Then, in the Form_Load event, the class gets instantiated with a reference to the form.
myRTS = New RealTimeStylus(PanelRTS.Handle)
In the DataInterest property of the form, we need to add the notifications for StylusDown, Packets and StylusUp.
Public ReadOnly Property DataInterest() As Microsoft.StylusInput.DataInterestMask Implements _
Return DataInterestMask.StylusDown + DataInterestMask.StylusUp + DataInterestMask.Packets
In order to display the Ink on the screen, we need to implement the methods for StylusDown, Packets and StylusUp.
Remember, the form in which all this code resides implements IStylusAsyncPlugin. To allow the plug-in to interact with the data stream from the tablet pen, the RealTimeStylus object maintains two plug-in collecton, which are accessible through the SyncPluginCollection and AsyncPluginCollection properties. You can add a plug-in by calling either StylusSyncPluginCollection.Add or the StylusAsyncPluginCollection.Add method of the collection within the appropriate property.