Click here to Skip to main content
15,884,298 members
Articles / Multimedia / GDI+

C# Application to Create and Recognize Mouse Gestures (.NET)

Rate me:
Please Sign up or sign in to vote.
4.82/5 (39 votes)
17 Mar 2008CPOL5 min read 221.4K   8.1K   144  
This program can create and recognize mouse gestures.
using System;
using System.Drawing;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
using MouseGestures;
using MouseGestures.Utilities;

namespace GestureRecognizer
{
	public class GesturePanel : Panel
	{
		private PointF[] m_Points = new PointF[0];
		private Gesture m_Gesture;
		private PointF m_MinPoint;
		private PointF m_MaxPoint;
		private Color m_BaseColor;
		private int m_Transparency;
		private Pen m_Pen;
		private Pen m_BasePen;
		private SolidBrush m_StartBrush;
		private SolidBrush m_EndBrush;

		public Color BaseColor
		{
			get { return m_BaseColor; }
			set
			{ 
				m_BaseColor = value;
				BackColor = Color.FromArgb(m_Transparency, value);
				Invalidate();
				//Refresh();
			}
		}

		public int Transparency
		{
			get { return m_Transparency; }
			set
			{
				m_Transparency = value;
				BackColor = Color.FromArgb(value, m_BaseColor);
				Invalidate();
				//Refresh();
			}
		}

		public Color StartColor
		{
			get { return m_StartBrush.Color; }
			set
			{
				m_StartBrush.Color = value;
				Invalidate();
				//Refresh();
			}
		}

		public Color StandardColor
		{
			get { return m_Pen.Color; }
			set
			{
				//m_StandardBrush.Color = value;
				m_Pen = new Pen(value);
				Invalidate();
				//Refresh();
			}
		}

		public Color EndColor
		{
			get { return m_EndBrush.Color; }
			set
			{
				m_EndBrush.Color = value;
				Invalidate();
				//Refresh();
			}
		}
		
		public Gesture Gesture
		{
			get { return m_Gesture; }
			set
			{
				if (value == null)
				{
					m_Gesture = null;
					m_Points = new PointF[0];
				}
				else
				{

					m_Gesture = value;
					m_Points = value.ToArray();

					#region Performing geometric operations to fit gesture in the panel
					Mathematics.GetBoundingBox(m_Points, out m_MinPoint, out m_MaxPoint);
					Mathematics.TranslateToOrigin(m_Points, ref m_MinPoint, ref m_MaxPoint);
					Mathematics.ScaleToFit(m_Points, Math.Min(ClientSize.Width, ClientSize.Height) - 10, ref m_MinPoint, ref m_MaxPoint);
					Mathematics.TranslateToCenter(m_Points, ClientSize.Width, ClientSize.Height, ref m_MinPoint, ref m_MaxPoint);
					#endregion
				}
				//Refresh();
				Invalidate();
			}
		}

		public PointF[] Points
		{
			get { return m_Points; }
			set
			{
				m_Gesture = null;

				if (value == null)
					m_Points = new PointF[0];
				else
				{

					m_Points = value;

					if (m_Points.Length > 0)
					{
						#region Performing geometric operations to fit gesture in the panel
						Mathematics.GetBoundingBox(m_Points, out m_MinPoint, out m_MaxPoint);
						Mathematics.TranslateToOrigin(m_Points, ref m_MinPoint, ref m_MaxPoint);
						Mathematics.ScaleToFit(m_Points, Math.Min(ClientSize.Width, ClientSize.Height) - 10, ref m_MinPoint, ref m_MaxPoint);
						Mathematics.TranslateToCenter(m_Points, ClientSize.Width, ClientSize.Height, ref m_MinPoint, ref m_MaxPoint);
						#endregion
					}
				}
				//Refresh();
				Invalidate();
			}
		}

		public GesturePanel()
		{
			SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
			DoubleBuffered = true;

			m_BaseColor = Color.Gray;
			m_Transparency = 100;
			m_Pen = new Pen(Color.Black, 1);
			m_BasePen = new Pen(Color.Gray, 1);
			m_StartBrush = new SolidBrush(Color.Green);
			m_EndBrush = new SolidBrush(Color.Red);
			BackColor = Color.FromArgb(m_Transparency, m_BaseColor);
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			if (m_Points.Length > 1)
			{
				e.Graphics.DrawLines(m_Pen, m_Points);
				e.Graphics.FillRectangle(m_StartBrush, m_Points[0].X - 1.5f, m_Points[0].Y - 1.5f, 3, 3);
				e.Graphics.FillRectangle(m_EndBrush, m_Points[m_Points.Length - 1].X - 1.5f, m_Points[m_Points.Length - 1].Y - 1.5f, 3, 3);
			}

			e.Graphics.DrawRectangle(new Pen(m_BaseColor), 0, 0, Width - 1, Height - 1);
		}

		public void Clear()
		{
			m_Gesture = null;
			m_Points = new PointF[0];
			//Refresh();
			Invalidate();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior) Apex s.r.l.
Italy Italy
I got my Computer Science (Engineering) Master's Degree at the Siena University (Italy), but I'm from Rieti (a small town next to Rome).
My hobbies are RPG, MMORGP, programming and 3D graphics.
At the moment I'm employed at Apex s.r.l. (Modena, Italy) as a senior software developer, working for a WPF/WCF project in Rome.

Comments and Discussions