Search code examples
.netwinformsuser-interfacewindows-8touch

Adding Windows 8 touch support to existing WinForms application


I have an existing Windows Forms desktop application targeting .NET Framework 4 and would like to add Windows 8 touch support to it.

Currently the program works fine in Windows 8, and I can potentially just resize some of the elements to make it more user-friendly on touch devices. However, adding gestures such as pinch-to-zoom on datagrids, and swipe support for other elements would go a long way to making the application more modern in a touch-only environment.

I'm investing in Visual Studio 2012, which will let me target .NET 4.5 and the new Windows 8 features, but does anyone know of any resources which would help me with updating my application? I'm specifically concerned about the following:

  • Inability to directly test the touch features of the application on my non-touch development machine. Microsoft's simulator only seems to support Metro apps. I've heard that tablet apps such as Splashtop can help (I have an Android tablet), but haven't seen anything concrete for this particular scenario
  • Whether gestures are even supported on WinForms applications. Am I going to have to upgrade the entire UI to WPF to get this working? (If I did go this route, I believe I could also target Windows 7, as multi-touch is supported on WPF 4)
  • Detecting the device's touch support at runtime and scaling/changing the UI appropriately, similar to the Touch mode setting on Microsoft's Windows RT Office apps. I don't want to fork the project just to add the new features
  • Automated testing of touch interactions

This isn't an exhaustive list by any means, but I'd really appreciate any advice from those who may have approached a similar upgrade in the past.


Solution

  • Detecting the device's touch support at run-time is problematic since the user can connect a touch device anytime. If you resize the form after detecting touch device connection and the device's connection is not stable you may end up with a form that keep resizing itself. Better have one steady interface for all inputs (e.g. using ribbon instead of a small menu bar). If you really want to detect touch devices, you can call GetSystemMetrics with SM_DIGITIZER.

    Gestures are not supported in Windows Forms (feature frozen in 2005). However form controls can still use touch input since the default touch handler translate touches to mouse clicks. If you want to have your own touch handler, since touch inputs are sent in the form of windows Messages, you can override your form or control's WndProc function to handle the gesture messages. For sample code check Windows Touch Samples in the the Windows 7 SDK.

    For writing test code for your touch capability, you can call InjectTouchInput to simulate touch input. A complete sample can be found at Input: Touch injection sample.