Click here to Skip to main content
15,893,588 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.9K   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
	//La classe realizza la form per gestire le gesture (eliminazione, creazione e generazione del 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
		//La funziona collega gli eventi ai relativi handlers
		private void LinkHandlers()
		{
			//Selezione di una gesture nella lista
			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
		//La funzione gestisce la selezione della gesture
		private void OnListSelectionChanged(object sender, EventArgs e)
		{
			//Controllo che l'indice della lista sia valido
			if(this.KnownGesturesList.SelectedIndex!=-1)
			{
				//Controllo se la gesture ha gi� un traiinng set
				if(((Gesture)this.m_MainBoard.KnownGestures[this.KnownGesturesList.SelectedIndex]).TrainingSetCreated)
				{
					//Controllo se la gesture � addestrata, in tal caso il colore del pannello diventa verde,
					//altrimenti il pannello diventa di colore arancione
					if(((Gesture)this.m_MainBoard.KnownGestures[this.KnownGesturesList.SelectedIndex]).Trained)
						this.m_PreviewPanel.BaseColor=Color.Green;
					else
						this.m_PreviewPanel.BaseColor=Color.Orange;
				}
				else
				{
					//Se la gesture non ha un training set, controllo se � addestrata, in tal caso il colore
					//del pannello diventa blue; in caso contrario il colore � rosso
					if(((Gesture)this.m_MainBoard.KnownGestures[this.KnownGesturesList.SelectedIndex]).Trained)
						this.m_PreviewPanel.BaseColor=Color.Blue;
					else
						this.m_PreviewPanel.BaseColor=Color.Red;
				}

				//Visualizzo nella preview la gesture e abilito i bottoni per rimuovere la gesture e creare il training set
				this.m_PreviewPanel.InsertNewGesture(((Gesture)this.m_MainBoard.KnownGestures[this.KnownGesturesList.SelectedIndex]).Points);
				this.RemoveGestureButton.Enabled=true;
				this.CreateTrainingSet.Enabled=true;
			}
			else
			{
				//Se l'indice non � valido disattivo i tasti di eliminazione e di creazione
				this.RemoveGestureButton.Enabled=false;
				this.CreateTrainingSet.Enabled=false;
			}
		}
		#endregion

		#region OnRemoveGesture handler
		private void OnRemoveGesture(object sender, EventArgs e)
		{
			//Controllo che l'indice sia valido
			if(this.KnownGesturesList.SelectedIndex!=-1)
			{
				//Memorizzo le path di tutti i files
				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;

				//Faccio apparire un messaggio chiedendo Se l'utente vuole eliminare anche i files
				//o vuole solo rimuovere la gesture dalla sessione attuale
				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);
							}
						}
					}
				}

				//Rimuovo dalla lista il nome della gesture rimossa
				this.m_MainBoard.KnownGestures.RemoveAt(Index);
				//Rimuovo dalle gestures conosciute quella specificata
				this.KnownGesturesList.Items.RemoveAt(Index);
				//Rimuovo la selezione dalla lista
				this.KnownGesturesList.SelectedIndex=-1;
				//Reimposto il colore del preview panel al valore base
				this.m_PreviewPanel.BaseColor=Color.Gray;
				//Elimino la gesture dal pannello della preview
				this.m_PreviewPanel.ClearPanel();
			}
		}
		#endregion

		#region OnCreateGesture handler
		//La funzione si incarica di preparare la finestra iniziale per la memorizzazione di una nuova gesture
		private void OnCreateGesture(object sender, EventArgs e)
		{
			//Pulisco tutti i pannelli e i messaggi della finestra principale
			this.m_MainBoard.ClearAll();
			//Riattivo la finestra iniziale iniziando la fase di creazione
			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)
		{
			//Controllo che sia valido l'indice della lista
			if(this.KnownGesturesList.SelectedIndex!=-1)
			{
				//Mostro La finestra dei settaggi di creazione del training set
				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)
		{
			//Alla chiusura della finestra di creazione del training set riabilito questa form
			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()
		{
			//Una volta creato il training set aggiorno il 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
		//La funzione carica dalle gestures della finestra principale i nomi delle gestures
		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);
			}		
		}

		//La funzione si occupa di aggiornare il contenuto della lista eliminando e ricaricando tutte le gestures
		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