Click here to Skip to main content
15,895,667 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 222K   8.1K   144  
This program can create and recognize mouse gestures.
using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;


namespace MouseGesture
{
	#region GesturesManagementForm class
	//This class allows to create/remove gestures and creating training set
	public class GesturesManagementForm : System.Windows.Forms.Form
	{
		#region Visual objects
		private System.Windows.Forms.ListBox KnownGesturesList;
		private System.Windows.Forms.GroupBox KnownGesturesGroup;
		private System.Windows.Forms.GroupBox GesturePreviewGroup;
		private System.Windows.Forms.Button RemoveGestureButton;
		private System.Windows.Forms.Button CreateGestureButton;
		private System.ComponentModel.Container components = null;
		private TransparentPanel m_PreviewPanel;
		private System.Windows.Forms.Button Cancel;
		private System.Windows.Forms.Button CreateTrainingSet;
		#endregion

		#region Private members
		private MainBoard m_MainBoard;
		#endregion

		#region Contructor
		public GesturesManagementForm(MainBoard board)
		{
			this.m_MainBoard=board;
			this.InitializeComponent();
			this.CustomInitialize();
			this.LoadKnownGesturesList();
			this.LinkHandlers();
		}
		#endregion

		#region Dispose function
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#endregion

		#region Linking handlers
		private void LinkHandlers()
		{
			this.KnownGesturesList.SelectedIndexChanged+=new EventHandler(this.OnListSelectionChanged);
			this.RemoveGestureButton.Click+=new EventHandler(this.OnRemoveGesture);
			this.CreateGestureButton.Click+=new EventHandler(this.OnCreateGesture);
			this.Cancel.Click+=new EventHandler(this.OnCancel);
			this.CreateTrainingSet.Click+=new EventHandler(this.OnCreateTrainingSet);
		}
		#endregion
		
		#region Event handlers

		#region OnListSelectionChanged handler
		//This function is a handler to selected index changing in the gestures ListBox
		private void OnListSelectionChanged(object sender, EventArgs e)
		{
			//Checking if index is valid
			if(this.KnownGesturesList.SelectedIndex!=-1)
			{
				//Chek training set created for gesture
				if(((Gesture)this.m_MainBoard.KnownGestures[this.KnownGesturesList.SelectedIndex]).TrainingSetCreated)
				{
					//Check if trained (if trained with training set: panel is green;
					//if not trained with training set panel is oprange
					if(((Gesture)this.m_MainBoard.KnownGestures[this.KnownGesturesList.SelectedIndex]).Trained)
						this.m_PreviewPanel.BaseColor=Color.Green;
					else
						this.m_PreviewPanel.BaseColor=Color.Orange;
				}
				else
				{
					//I gesture has no training set, check if it's trained, if so, panel is blue,
					//otherwise, panel is red
					if(((Gesture)this.m_MainBoard.KnownGestures[this.KnownGesturesList.SelectedIndex]).Trained)
						this.m_PreviewPanel.BaseColor=Color.Blue;
					else
						this.m_PreviewPanel.BaseColor=Color.Red;
				}

				//Showing preview and enabling buttons
				this.m_PreviewPanel.InsertNewGesture(((Gesture)this.m_MainBoard.KnownGestures[this.KnownGesturesList.SelectedIndex]).Points);
				this.RemoveGestureButton.Enabled=true;
				this.CreateTrainingSet.Enabled=true;
			}
			else
			{
				//If index is not valid, some buttones are disabled
				this.RemoveGestureButton.Enabled=false;
				this.CreateTrainingSet.Enabled=false;
			}
		}
		#endregion

		#region OnRemoveGesture handler
		private void OnRemoveGesture(object sender, EventArgs e)
		{
			//Checks index
			if(this.KnownGesturesList.SelectedIndex!=-1)
			{
				//Store file paths
				int Index=this.KnownGesturesList.SelectedIndex;
				string Path=((Gesture)this.m_MainBoard.KnownGestures[Index]).FileFullName;
				string NetPath=((Gesture)this.m_MainBoard.KnownGestures[Index]).NetFullName;
				string TrainingSetPath=((Gesture)this.m_MainBoard.KnownGestures[Index]).TrainingSetFullName;

				//Showing a confirm message box
				if(MessageBox.Show("Do you want to delete \'"+Path+"\' and all related files?","Attention!",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2)==DialogResult.Yes)
				{
					if(File.Exists(Path))
					{
						try
						{
							File.Delete(Path);
						}
						catch
						{
							MessageBox.Show("Could not delete \'"+Path+"\'!","Error!",MessageBoxButtons.OK,MessageBoxIcon.Error);
						}
					}

					if(((Gesture)this.m_MainBoard.KnownGestures[Index]).Trained)
					{
						if(File.Exists(NetPath))
						{
							try
							{
								File.Delete(NetPath);
							}
							catch
							{
								MessageBox.Show("Could not delete \'"+NetPath+"\'!","Error!",MessageBoxButtons.OK,MessageBoxIcon.Error);
							}
						}
					}

					if(((Gesture)this.m_MainBoard.KnownGestures[Index]).TrainingSetCreated)
					{
						if(File.Exists(TrainingSetPath))
						{
							try
							{
								File.Delete(TrainingSetPath);
							}
							catch
							{
								MessageBox.Show("Could not delete \'"+TrainingSetPath+"\'!","Error!",MessageBoxButtons.OK,MessageBoxIcon.Error);
							}
						}
					}
				}

				//Remove from list
				this.m_MainBoard.KnownGestures.RemoveAt(Index);
				//Remove gesture
				this.KnownGesturesList.Items.RemoveAt(Index);
				//Change selected index
				this.KnownGesturesList.SelectedIndex=-1;
				//Reset preview panel color
				this.m_PreviewPanel.BaseColor=Color.Gray;
				//Clear preview panel
				this.m_PreviewPanel.ClearPanel();
			}
		}
		#endregion

		#region OnCreateGesture handler
		//Starts gesture creation
		private void OnCreateGesture(object sender, EventArgs e)
		{
			//Clear all panels
			this.m_MainBoard.ClearAll();
			//Enable main form to start acquiring
			this.m_MainBoard.Parent.Enabled=true;
			this.Visible=false;
			this.Enabled=false;
			this.m_MainBoard.CreateGesture();
		}
		#endregion

		#region OnCreateTrainingSet handler
		private void OnCreateTrainingSet(object sender, EventArgs e)
		{
			//Check index
			if(this.KnownGesturesList.SelectedIndex!=-1)
			{
				//Shows training set form
				this.Visible=false;
				this.Enabled=false;
				CreateTrainingSetForm TrainingForm=new CreateTrainingSetForm(this.m_MainBoard, this.KnownGesturesList.SelectedIndex);
				TrainingForm.Closed+= new EventHandler(this.OnCloseCreateTrainingSetForm);
				TrainingForm.TrainingSetCreatedEvent+=new MouseGesture.CreateTrainingSetForm.TrainingSetCreatedDelegate(this.OnTrainingSetCreated);
				TrainingForm.ShowDialog();
			}
		}
		#endregion

		#region OnCloseCreateTrainingSetForm handler
		private void OnCloseCreateTrainingSetForm(object sender, EventArgs e)
		{
			//This form is re-enabled when training set is created
			this.Visible=true;
			this.Enabled=true;			
		}
		#endregion

		#region OnCancel handler
		private void OnCancel(object sender, EventArgs e)
		{
			this.Close();
		}
		#endregion

		#region OnTrainingSetCreated handler
		private void OnTrainingSetCreated()
		{
			//If training set created for selected gesture, refresh preview panel
			if(this.KnownGesturesList.SelectedIndex!=-1)
			{
				if(((Gesture)this.m_MainBoard.KnownGestures[this.KnownGesturesList.SelectedIndex]).Trained)
					this.m_PreviewPanel.BaseColor=Color.Green;
				else
					this.m_PreviewPanel.BaseColor=Color.Orange;
			}
		}
		#endregion

		#endregion

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(GesturesManagementForm));
			this.KnownGesturesGroup = new System.Windows.Forms.GroupBox();
			this.KnownGesturesList = new System.Windows.Forms.ListBox();
			this.RemoveGestureButton = new System.Windows.Forms.Button();
			this.GesturePreviewGroup = new System.Windows.Forms.GroupBox();
			this.CreateGestureButton = new System.Windows.Forms.Button();
			this.Cancel = new System.Windows.Forms.Button();
			this.CreateTrainingSet = new System.Windows.Forms.Button();
			this.KnownGesturesGroup.SuspendLayout();
			this.SuspendLayout();
			// 
			// KnownGesturesGroup
			// 
			this.KnownGesturesGroup.Controls.Add(this.KnownGesturesList);
			this.KnownGesturesGroup.Location = new System.Drawing.Point(16, 8);
			this.KnownGesturesGroup.Name = "KnownGesturesGroup";
			this.KnownGesturesGroup.Size = new System.Drawing.Size(200, 273);
			this.KnownGesturesGroup.TabIndex = 1;
			this.KnownGesturesGroup.TabStop = false;
			this.KnownGesturesGroup.Text = "Known gestures:";
			// 
			// KnownGesturesList
			// 
			this.KnownGesturesList.DisplayMember = "string";
			this.KnownGesturesList.Location = new System.Drawing.Point(8, 24);
			this.KnownGesturesList.Name = "KnownGesturesList";
			this.KnownGesturesList.Size = new System.Drawing.Size(184, 238);
			this.KnownGesturesList.TabIndex = 4;
			this.KnownGesturesList.ValueMember = "string";
			// 
			// RemoveGestureButton
			// 
			this.RemoveGestureButton.Enabled = false;
			this.RemoveGestureButton.Location = new System.Drawing.Point(224, 224);
			this.RemoveGestureButton.Name = "RemoveGestureButton";
			this.RemoveGestureButton.Size = new System.Drawing.Size(120, 24);
			this.RemoveGestureButton.TabIndex = 2;
			this.RemoveGestureButton.Text = "&Remove gesture";
			// 
			// GesturePreviewGroup
			// 
			this.GesturePreviewGroup.Location = new System.Drawing.Point(224, 8);
			this.GesturePreviewGroup.Name = "GesturePreviewGroup";
			this.GesturePreviewGroup.Size = new System.Drawing.Size(120, 136);
			this.GesturePreviewGroup.TabIndex = 4;
			this.GesturePreviewGroup.TabStop = false;
			this.GesturePreviewGroup.Text = "Gesture preview:";
			// 
			// CreateGestureButton
			// 
			this.CreateGestureButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.CreateGestureButton.Location = new System.Drawing.Point(224, 160);
			this.CreateGestureButton.Name = "CreateGestureButton";
			this.CreateGestureButton.Size = new System.Drawing.Size(120, 24);
			this.CreateGestureButton.TabIndex = 0;
			this.CreateGestureButton.Text = "Create &gesture";
			// 
			// Cancel
			// 
			this.Cancel.Location = new System.Drawing.Point(224, 256);
			this.Cancel.Name = "Cancel";
			this.Cancel.Size = new System.Drawing.Size(120, 24);
			this.Cancel.TabIndex = 3;
			this.Cancel.Text = "&Cancel";
			// 
			// CreateTrainingSet
			// 
			this.CreateTrainingSet.Enabled = false;
			this.CreateTrainingSet.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.CreateTrainingSet.Location = new System.Drawing.Point(224, 192);
			this.CreateTrainingSet.Name = "CreateTrainingSet";
			this.CreateTrainingSet.Size = new System.Drawing.Size(120, 24);
			this.CreateTrainingSet.TabIndex = 1;
			this.CreateTrainingSet.Text = "Create &training set";
			// 
			// GesturesManagementForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(354, 288);
			this.Controls.Add(this.CreateTrainingSet);
			this.Controls.Add(this.Cancel);
			this.Controls.Add(this.CreateGestureButton);
			this.Controls.Add(this.GesturePreviewGroup);
			this.Controls.Add(this.RemoveGestureButton);
			this.Controls.Add(this.KnownGesturesGroup);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "GesturesManagementForm";
			this.ShowInTaskbar = false;
			this.Text = "Gesture management";
			this.TopMost = true;
			this.KnownGesturesGroup.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		#region Custom Inizialization code
		private void CustomInitialize()
		{
			this.m_PreviewPanel=new TransparentPanel();
			this.GesturePreviewGroup.Controls.Add(this.m_PreviewPanel);
			this.m_PreviewPanel.SetBounds(8,24,100,100);
			this.m_PreviewPanel.Parent=this.GesturePreviewGroup;
			this.m_PreviewPanel.Show();
		}
		#endregion

		#region Gesture list management
		//Loads gestures adding names to ListBox
		private void LoadKnownGesturesList()
		{
			for(int i=0; i<this.m_MainBoard.KnownGestures.Count; i++)
			{
				this.KnownGesturesList.Items.Add(((Gesture)this.m_MainBoard.KnownGestures[i]).Name);
			}		
		}

		//Refresh items in the ListBox
		public void RefreshGestureList()
		{
			this.KnownGesturesList.Items.Clear();
			this.LoadKnownGesturesList();
			this.Invalidate();
		}
		#endregion
	}
	#endregion
}

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