Click here to Skip to main content
15,896,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how do i do a mouse swipe gesture in win forms? i have found some routine but i have not found something that is like the mobile world swipe behavior.

what i wanted if i swipe slow the swiping transition will be slow, but if i flick it, it will have its momentum. i would use it to change a picture from inside a panel.

can anyone point me to an article or a link :D

thanks :D
Posted

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-May-11 15:56pm    
This is interesting project, good to know, a 5.
--SA
RaviRanjanKr 1-May-11 23:10pm    
Thanks SA.
First, add a property to your page/control that keeps track of the Touch that the user has initiated to start the swipe event:

C#
protected TouchPoint TouchStart;

In the codebehind of the Page or Control you're building, add the following handlers:
C#
public BasePage()
{
	this.TouchDown += new EventHandler<toucheventargs>(BasePage_TouchDown);
	this.TouchMove += new EventHandler<toucheventargs>(BasePage_TouchMove);            
}</toucheventargs></toucheventargs>

These handlers help detect when a user has pressed down and moved his/her finger over your page or control.

Next, handle the initial Touch event that triggers the swipe:
C#
void BasePage_TouchDown(object sender, TouchEventArgs e)
{
	TouchStart = e.GetTouchPoint(this);
}
Finally, handle the movement aspect of the touch. If the movement exceeds some threshold; then we consider it a swipe and execute whatever code we want to do (Navigation, animation, etc)

Here, 'AlreadySwiped' is just a flag property so we don't execute the same task multiple times if the swipe exceeds our threshold more than once. You are responsible for resetting it after you do your on-swiped code. Also, I used 200 pixels as the swipe threshold, but you may want a bigger/smaller value. You may also want to consider percentages of X here instead of actual pixels.
C#
void BasePage_TouchMove(object sender, TouchEventArgs e)
{
	if (!AlreadySwiped)
	{                
		var Touch = e.GetTouchPoint(this);

                //right now a swipe is 200 pixels 

                //Swipe Left

                if (TouchStart != null && Touch.Position.X > (TouchStart.Position.X + 200))
                {
                    RunMyCustomCode();
                    AlreadySwiped = true;
                }

                //Swipe Right
                if (TouchStart != null && Touch.Position.X < (TouchStart.Position.X - 200))
                {
                     RunMyCustomCodeSwipeRight();
                    AlreadySwiped = true;
                }
            }

            e.Handled = true;
}

This should be straightforward but send me a message if you have any questions.
 
Share this answer
 
v2
Comments
Member 8753121 28-Feb-14 14:59pm    
Will Solution 2 only work for a WPF/XAML application? If also for Winforms how do I get visibility to TouchDown and TouchMove? Thanks
Member 8753121 13-Nov-14 16:59pm    
Same for me. I do not see TouchDown/TouchMove in my Winforms app. Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900