Windows 7 Multi-touch Panning with ScrollViewer in WPF 3.5

UPDATE: You should be using the Window to receive events. See my updated post for details.

Yes, it can be done easily, including inertia. It does require the Windows 7 Multitouch .NET Interop Sample Library

First, create a class that inherits from scrollviewer.

Code Snippet
  1. Public Class MultiTouchScollViewer
  2.     Inherits ScrollViewer
  3.  
  4. End Class

Then you add references to Windows7.Multitouch.dll and Windows7.Multitouch.Wpf.dll.

Then modify the MultiTouchScrollViewer like so:

Code Snippet
  1. Imports System.Windows
  2. Imports System.Windows.Controls
  3.  
  4. Public Class MultiTouchScrollViewer
  5.     Inherits ScrollViewer
  6.  
  7.     Private GestureHandler As Windows7.Multitouch.GestureHandler
  8.  
  9.     Public Sub New()
  10.         AddHandler MyBase.Loaded, New RoutedEventHandler(AddressOf Me.MultiTouchScollViewer_Loaded)
  11.         Me.GestureHandler = Nothing
  12.     End Sub
  13.  
  14.     Private Sub MultiTouchScollViewer_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
  15.         If Windows7.Multitouch.TouchHandler.DigitizerCapabilities.IsMultiTouchReady Then
  16.             GestureHandler = Windows7.Multitouch.WPF.Factory.CreateGestureHandler(System.Windows.Window.GetWindow(Me))
  17.             AddHandler GestureHandler.Pan, AddressOf Me.GestureHandler_Pan
  18.         End If
  19.     End Sub
  20.  
  21.     Private Sub GestureHandler_Pan(ByVal sender As Object, ByVal e As Windows7.Multitouch.GestureEventArgs)
  22.         Dim s As System.Drawing.Size = e.PanTranslation
  23.  
  24.         Me.ScrollToVerticalOffset(Me.VerticalOffset - s.Height)
  25.     End Sub
  26.  
  27. End Class

Download the VB.NET code here

Download the C# code here

NOTE: It does require hit testing if using multiple scrollviewers in the same window.

9. November 2009 05:04 by Rick | Comments (2) | Permalink

Comments

About Rick

Rick lives in North Las Vegas. He loves his wife, kids, dog, motorcycle, music and programming. There ain't nothing else. Oh yeah, mountain dew!



Programming interests are geared towards multimedia. Platforms are asp.net, windows forms, and WPF.

Calendar

<<  July 2010  >>
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar

RecentComments

Comment RSS