Windows Mobile 6.5 Gestures using managed .NET code
I've been working with bar code scanners lately for a project I'm doing with a company that needs to track their inventory. Unfortunately there isn't any bar code scanners with Windows 7 yet, but there is the last gasp of the failed Microsoft mobile OS, Windows Mobile 6.5. I wanted to take advantage of the gesture ability that comes with 6.5 and discovered that some kind soul created a managed dll for it. That can be picked up here
, go grab it now.
The only reason I decided to write to post this is that I found almost no help with how to use this component without delving into C++ code examples. Here are the 3 pages I found that helped --
* A great overview article on gestures at MSDN
* An article
that briefly introduces you to the managed component and how to use it
* Finally Maarten Struys created a video
on how to use the component. In fact I believe he is the one who wrote the component. Now Maarten does a good job explaining how to use the pan and scroll function he really doesn't provide all of the code. In a few frames in the video I had to pause it to see the code he didn't cover that made the scroll function work.
So here's my setup. Using VS 2008 I created a device project for .NET CE. Added a form. Navigation is using the simple design pattern of adding a user control to the form, press a button on control to navigate to the next screen, which raises an event to the form, which in turn removes the control and replaces it with a new user control. Simple, effective.
First thing you need to do is add the Microsoft.WindowsMobile.Gestures.dll to your toolbox. This will give you two UI design components, GestureRecognizer and PhysicsEngine. Drag and drop these components on to your form. Click on the PhysicsEngine component and set your ViewportControl to your form. Also if you're set up to only move vertically, like me, then set AnimateXAxis to false. With a map you'll want to scroll in both x and y directions, so leave it alone. Next up, for the GestureRecognizer component add the scroll and pan events. Sorry I didn't need to handle the other events select and hold. I'll leave that as a exercise for the reader :) Just a recap on what these events are. Scrolling is basically when you flick your finger and the screen moves fast then slows down, this is where the PhysicsEngine comes into play, since it speeds up and then slows down. Panning on the other hand is for precise movement, when you press and drag the screen with your finger.
Ok now on to the code.
In the event handler that handles navigation (when the control raises an event saying, user just requested that she wants to navigate to screen xyz), you'll need to add this code
protected void userControlA_NavigationRequested( object sender, EventArgs e )
this.Controls.RemoveAt( 0 );
var userControlB = new UserControlB();
... // your setup code this.Controls.Add( userControlB );}
physicsEngine.ExtentControl = userControlB;
gestureRecognizer.TargetControl = userControlB;
So we're just assigning the Extentcontrol and TargetControl properties. That's kind of self explanatory. Your user control is larger than your screen or viewport so it's the one that needs to be moved, not your form. It's just a place for your control to sit in.
Next up is filling in the two gesture events --
private Point _last;
private Point _offset;
private void gestureRecognizer_Pan( object sender,
Microsoft.WindowsMobile.Gestures.GestureEventArgs e )
if ( ( e.State & GestureState.Begin ) == GestureState.Begin )
_last = e.Location;
_offset = this.Controls[ 0 ].Location.Negate();
Point delta = e.Location.Subtract( _last );
delta.X = 0;
_offset = _offset.Subtract( delta );
_offset.Y = _offset.Y < 0 ? 0 : _offset.Y;
_offset.Y = ( _offset.Y > this.Controls[ 0 ].Height - this.Height ) ? this.Controls[ 0 ].Height - this.Height : _offset.Y;
this.Controls[ 0 ].Location = _offset.Negate();
_last = e.Location;
private void gestureRecognizer_Scroll( object sender,
Microsoft.WindowsMobile.Gestures.GestureScrollEventArgs e )
physicsEngine.Velocity = e.Velocity;
physicsEngine.Angle = e.Angle;
Make sure you add using Microsoft.WindowsMobile.Gestures with your using statements. This namespace includes the Negate() function for the Point class. I'm not going to explain this code to you, walk through it on your own. Maaten's video explains most of it, although he never explains setting up the Scroll event handler.
Good luck!UPDATE 4/10/2011I discovered the source code for the dll hereAlso I had some issues with the library handling the gestures fine but sometimes it would invariably fail and not work at all. Looks like this was a common problem that the author updated with this code
Read the complete post at http://therationalpath.blogspot.com/2011/03/windows-mobile-65-gestures-using.html