The last two posts discussing Windows Touch gave a high level view of using the Windows Touch SDK in Windows 7 with the .NET Framework. In this post, I’d like to go into more detail on using Gestures. Remember from the first post – Gestures cannot be used when the RegisterTouchWindow() has been called.
Configuration – Where to Configure
In the Windows Touch SDK, Gesture messages are sent by default to all windows, without any kind of configuration settings. However, configuration is needed to fine-tune what gestures are sent and which ones are ignored. This is done via a p/invoke of the SetGestureConfig() function in user32.dll in one of two places: in the Load event or when the WM_GESTURENOTIFY message has been sent. Ideally, the Load event is the place to set the configuration, unless the configuration parameters will be changed multiple times.
Structures and P/Invoke Setup
When setting up the configuration for using the Windows Touch SDK with .NET, you’ll need to declare the structure needed to hold the configuration. You’ll also need to declare the external functions (p/invoke) for setting the configuration, along with the values needed for each configuration option.
The Configuration Structure (GESTURECONFIG) is pretty straightforward, with only three ints in the structure. It is good to note that more than one GESTURECONFIG structures can be sent, as this allows specific settings for each individual gesture.
The GESTURECONFIG can be declared as follows:
private struct GESTURECONFIG
{
public int id;
public int want;
public int block;
}
The id field determines which gesture for which you are setting the configuration. (See Here For Gestures And Their IDs). The Want and Block fields determine which features (specified by a flag) within a specific gesture are wanted or blocked. Most Gestures only have a feature flag of ID 0x0000001, as the gesture has only one feature (like Zoom). Pan has five gesture flags: 0x0000001 (All gestures), 0x00000002 (vertical pans with one finger), 0x00000004 (horizontal pans with one finger), 0x00000008 (Pan with a ‘gutter’), and 0x00000010 (Pan with Inertia).
So, a new GESTURECONFIG for activating just a vertical pan would be:
GESTURECONFIG config = new GESTURECONFIG(){
id = 0x00000004, // This is the ID for PAN
want = 0x00000002, // We want to have vertical PAN
block = 0x00000004 // We want to block the horizontal PAN
};
The p/invoke declaration is set up in the class you are using by doing a DllImport to the user32.dll. It will look like this:
[DllImport("user32")]
private static extern bool SetGestureConfig(IntPtr window, int reserved, int ids, ref GESTURECONFIG config, int size);
Remember to include the using / import statement for System.Runtime.InteropServices.
Setting the Configuration
At this point, we have set up all the prerequisite information for setting the Gesture Configuration. The SetGestureConfig() function takes in five different parameters:
- hWnd – The Handle to the Window that we’re using to capture gestures.
- Reserved – This is reserved for future use and needs to be set to 0.
- IDs – This is how many GESTURECONFIG structures that are being in passed into SetGestureConfig().
- config – An array of GESTURECONFIG structures containing the configuration information.
- size – the size of the array passed in.
Once called, SetGestureConfig() return a zero if it is unsuccessful in setting the gesture configuration and a nonzero value if successful.
bool result = SetGestureConfig(this.Handle, 0, 1, ref config, Marshal.SizeOf(config));
Putting It All Together
If we were to set the PAN Gesture to only accept vertical gestures, we’d put the previous code snippets together into a single form. It would look something like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace VerticalPanOnly
{
public partial class frmGestures : Form
{
private struct GESTURECONFIG
{
public int id;
public int want;
public int block;
}
[DllImport("user32")]
private static extern bool SetGestureConfig(IntPtr window, int reserved, int ids, ref GESTURECONFIG config, int size);
public frmGestures()
{
InitializeComponent();
}
private void frmGestures_Load(object sender, EventArgs e)
{
GESTURECONFIG config = new GESTURECONFIG(){
id = 0x00000004, // This is the ID for PAN
want = 0x00000002, // We want to have vertical PAN
block = 0x00000004 // We want to block the horizontal pan
};
int size = Marshal.SizeOf(config);
bool result = SetGestureConfig(this.Handle, 0, 1, ref config, Marshal.SizeOf(config));
}
}
}
In another post, I’ll demonstrate how to obtain gesture information from the Windows Touch API in .NET once a gesture has occurred.


