I don’t know about you, but I’ve been really looking forward to the touch features built into Windows 7 (also known as Windows Touch). Now that we’ve hit general availability for Windows 7, I took a look at how I might be able to start using the Windows Touch in my .NET applications.
After a little review, it turns out that it is not as simple as you’d think. Windows Touch is not a .NET native, and a platform invoke (p-invoke) is needed to really reap the full benefits. I say full benefits, because Windows Touch sends some events to applications, without doing anything at all. It’s pretty much what you’d expect: a single touch sends a Click event to the form/control in question, holding a touch to the screen will send a RightClick to the form. But in order to do the really neat stuff, you need to use p-invoke.
A basic example of how to perform a platform invoke is creating a simple form which determines the touch capabilities of the client running your application. While this doesn’t use the touch API, it does provide a basic example of how access the user32.dll.
Start off by creating a form in .NET. Be sure to include an imports / using statement which accesses System.RunTime.InteropServices. Next, in the form you’ve created, be sure to add a DLL Import of user32.dll for the GetSystemMetrics() function. This is what we’ll use to determine what touch features are available. It will look something like this:
Next, we’ll need to create some constants to handle the value returned from the GetSystemMetrics function.
These constants are AND-wise compared with returned value to see all of the capabilities available. This is needed, because a computer might well have an integrated pen and an external pen. The AND-wise compare looks like this.
In case you are wondering, the 94 submitted to the GetSystemMetrics() function is a ‘magic number’ that tells the function to bring back just the Windows Touch-related capabilities. You can reference the Windows API and see other kinds of metrics (How many buttons are on the mouse, whether or not the call is being performed in a Terminal Services environment, whether you’re running a “Starter” edition, etc..)
In conclusion, this is a good start for getting your feet wet in using platform invoke and to determine how to what touch capabilities your computer is capable of. In another post, I’ll describe how to register a touch window in .NET with p-invoke and how to get information from the Windows Touch API.


0 comments:
Post a Comment