Gestures multitouch in windows forms

0

Hello I am developing an application in Visual Studio it is a windowos form application in C # , the application makes use of a monitor multitouch in win10 and I wanted to know if someone can give me some guidance to see if it is possible to zoom in on an image by pinching as in a < strong> iphone , all the information I have found is for WPF applications, I do not know if in windows forms it is possible.

Thanks

    
asked by Sergio Carvajal Alonso 09.12.2016 в 19:54
source

1 answer

-1

Try using this: link

You have an example here: link

#region Using Directives

using System;

using System.Diagnostics;

using System.Drawing;

using System.Windows.Forms;

#endregion

namespace Alteridem.WinTouch.Demo

{

public partial class GestureControl : UserControl

{

// Our touch listener

private readonly GestureListener _gesture;

// Size, location and rotation of the image

private Point _location;

private int _size;

private double _rotation;

// An image to draw

private Image m_image;

// Brushes

private readonly Brush[] _backBrushes = { Brushes.White, Brushes.AntiqueWhite, Brushes.Bisque, Brushes.Wheat, Brushes.Bisque, Brushes.AntiqueWhite };

private int _backBrush;

public GestureControl()

{

InitializeComponent();

SetStyle( ControlStyles.AllPaintingInWmPaint |

ControlStyles.UserPaint |

ControlStyles.ResizeRedraw |

ControlStyles.OptimizedDoubleBuffer, true );

// Subscribe to all the touch events

_gesture = new GestureListener( this );

_gesture.Pan += OnPan;

_gesture.PressAndTap += OnPressAndTap;

_gesture.Rotate += OnRotate;

_gesture.TwoFingerTap += OnTwoFingerTap;

_gesture.Zoom += OnZoom;

m_image = Properties.Resources.icon;

}

private void OnLoad( object sender, EventArgs e )

{

ResetImage();

}

private void ResetImage()

{

// Center the image on the form and reset its size

_size = Math.Min( m_image.Width, m_image.Height ) / 2;

_location = new Point( Width / 2, Height / 2 );

_rotation = 0;

}

void OnPan( object sender, PanEventArgs e )

{

string msg = string.Format( "Pan Loc:({0},{1}) InertiaVector:({2},{3})", e.Location.X, e.Location.Y,

e.InertiaVector.X, e.InertiaVector.Y );

Debug.WriteLine( msg );

if ( !e.Begin )

{

_location.Offset( e.PanOffset );

// Make sure it doesn't leave the screen

if ( _location.X < 0 )

_location.X = 0;

else if ( _location.X > Width )

_location.X = Width;

if ( _location.Y < 0 )

_location.Y = 0;

else if ( _location.Y > Height )

_location.Y = Height;

Invalidate();

}

}

void OnPressAndTap( object sender, PressAndTapEventArgs e )

{

string msg = string.Format( "PressAndTap Loc:({0},{1}) Distance:{2})", e.Location.X, e.Location.Y, e.Distance );

Debug.WriteLine( msg );

if ( e.Begin )

{

if ( ++_backBrush >= _backBrushes.Length )

{

_backBrush = 0;

}

Invalidate();

}

}

void OnRotate( object sender, RotateEventArgs e )

{

string msg = string.Format( "Rotate Loc:({0},{1}) Angle:{2}", e.Location.X, e.Location.Y, e.TotalDegrees );

Debug.WriteLine( msg );

if ( !e.Begin && !e.End )

{

_rotation -= e.Degrees;

Invalidate();

}

}

void OnTwoFingerTap( object sender, TwoFingerTapEventArgs e )

{

string msg = string.Format( "TwoFingerTap Loc:({0},{1}) Distance:{2}", e.Location.X, e.Location.Y, e.Distance );

Debug.WriteLine( msg );

if ( e.Begin )

{

ResetImage();

Invalidate();

}

}

void OnZoom( object sender, ZoomEventArgs e )

{

string msg = string.Format( "Zoom Loc:({0},{1}) Distance:{2}", e.Location.X, e.Location.Y, e.Distance );

Debug.WriteLine( msg );

if ( !e.Begin )

{

_size = (int)(_size * e.PercentChange);

Invalidate();

}

}

protected override void OnPaint( PaintEventArgs e )

{

// Draw Background

e.Graphics.FillRectangle( _backBrushes[_backBrush], 0, 0, Width, Height );

// Draw Info

string info = string.Format( "Pos:{0} Size:{1} Rotation:{2}", _location, _size, _rotation );

e.Graphics.DrawString( info, SystemFonts.DefaultFont, Brushes.Black, 5, 5 );

// Draw Square

e.Graphics.TranslateTransform( _location.X, _location.Y );

e.Graphics.RotateTransform( (float)_rotation );

e.Graphics.TranslateTransform( -_location.X, -_location.Y );

e.Graphics.DrawImage( m_image, _location.X - _size / 2, _location.Y - _size / 2, _size, _size );

}

}

}

n Using Directives

using System;

using System.Diagnostics;

using System.Drawing;

using System.Windows.Forms;

#endregion

namespace Alteridem.WinTouch.Demo

{

public partial class GestureControl : UserControl

{

// Our touch listener

private readonly GestureListener _gesture;

// Size, location and rotation of the image

private Point _location;

private int _size;

private double _rotation;

// An image to draw

private Image m_image;

// Brushes

private readonly Brush[] _backBrushes = { Brushes.White, Brushes.AntiqueWhite, Brushes.Bisque, Brushes.Wheat, Brushes.Bisque, Brushes.AntiqueWhite };

private int _backBrush;

public GestureControl()

{

InitializeComponent();

SetStyle( ControlStyles.AllPaintingInWmPaint |

ControlStyles.UserPaint |

ControlStyles.ResizeRedraw |

ControlStyles.OptimizedDoubleBuffer, true );

// Subscribe to all the touch events

_gesture = new GestureListener( this );

_gesture.Pan += OnPan;

_gesture.PressAndTap += OnPressAndTap;

_gesture.Rotate += OnRotate;

_gesture.TwoFingerTap += OnTwoFingerTap;

_gesture.Zoom += OnZoom;

m_image = Properties.Resources.icon;

}

private void OnLoad( object sender, EventArgs e )

{

ResetImage();

}

private void ResetImage()

{

// Center the image on the form and reset its size

_size = Math.Min( m_image.Width, m_image.Height ) / 2;

_location = new Point( Width / 2, Height / 2 );

_rotation = 0;

}

void OnPan( object sender, PanEventArgs e )

{

string msg = string.Format( "Pan Loc:({0},{1}) InertiaVector:({2},{3})", e.Location.X, e.Location.Y,

e.InertiaVector.X, e.InertiaVector.Y );

Debug.WriteLine( msg );

if ( !e.Begin )

{

_location.Offset( e.PanOffset );

// Make sure it doesn't leave the screen

if ( _location.X < 0 )

_location.X = 0;

else if ( _location.X > Width )

_location.X = Width;

if ( _location.Y < 0 )

_location.Y = 0;

else if ( _location.Y > Height )

_location.Y = Height;

Invalidate();

}

}

void OnPressAndTap( object sender, PressAndTapEventArgs e )

{

string msg = string.Format( "PressAndTap Loc:({0},{1}) Distance:{2})", e.Location.X, e.Location.Y, e.Distance );

Debug.WriteLine( msg );

if ( e.Begin )

{

if ( ++_backBrush >= _backBrushes.Length )

{

_backBrush = 0;

}

Invalidate();

}

}

void OnRotate( object sender, RotateEventArgs e )

{

string msg = string.Format( "Rotate Loc:({0},{1}) Angle:{2}", e.Location.X, e.Location.Y, e.TotalDegrees );

Debug.WriteLine( msg );

if ( !e.Begin && !e.End )

{

_rotation -= e.Degrees;

Invalidate();

}

}

void OnTwoFingerTap( object sender, TwoFingerTapEventArgs e )

{

string msg = string.Format( "TwoFingerTap Loc:({0},{1}) Distance:{2}", e.Location.X, e.Location.Y, e.Distance );

Debug.WriteLine( msg );

if ( e.Begin )

{

ResetImage();

Invalidate();

}

}

void OnZoom( object sender, ZoomEventArgs e )

{

string msg = string.Format( "Zoom Loc:({0},{1}) Distance:{2}", e.Location.X, e.Location.Y, e.Distance );

Debug.WriteLine( msg );

if ( !e.Begin )

{

_size = (int)(_size * e.PercentChange);

Invalidate();

}

}

protected override void OnPaint( PaintEventArgs e )

{

// Draw Background

e.Graphics.FillRectangle( _backBrushes[_backBrush], 0, 0, Width, Height );

// Draw Info

string info = string.Format( "Pos:{0} Size:{1} Rotation:{2}", _location, _size, _rotation );

e.Graphics.DrawString( info, SystemFonts.DefaultFont, Brushes.Black, 5, 5 );

// Draw Square

e.Graphics.TranslateTransform( _location.X, _location.Y );

e.Graphics.RotateTransform( (float)_rotation );

e.Graphics.TranslateTransform( -_location.X, -_location.Y );

e.Graphics.DrawImage( m_image, _location.X - _size / 2, _location.Y - _size / 2, _size, _size );

}

}

}
    
answered by 16.10.2017 в 05:19