Click here to Skip to main content
15,884,836 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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MouseGestures;

namespace GestureRecognizer
{
	public partial class GestureManagementForm : Form
	{
		private GestureMainForm m_MainForm;
        private GestureSet m_GestureSet;

		public GestureManagementForm(GestureMainForm main_form)
		{
			InitializeComponent();
			m_MainForm = main_form;
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			TrainingSetCountLabel.Text = "TS Count:";
            TestSetCountLabel.Text = "VS Count: ";
			GesturePreviewPanel.BaseColor = Color.Gray;
			GesturePreviewPanel.Points = null;
			KnownGesturesList.Items.Clear();

			foreach (GestureRecognizerData g in m_MainForm.GesturesData.Values)
				KnownGesturesList.Items.Add(g.Name);
		}

		private void CreateGestureButton_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.OK;
			Close();
		}

		private void CreateTrainingSetButton_Click(object sender, EventArgs e)
		{
			if (KnownGesturesList.SelectedIndex != -1)
			{
				DialogResult = DialogResult.Yes;
                m_GestureSet = m_MainForm.GesturesData[(string)KnownGesturesList.SelectedItem].TrainingSet;
				Close();
			}
		}

        private void CreateTestSetButton_Click(object sender, EventArgs e)
        {
            if (KnownGesturesList.SelectedIndex != -1)
            {
                DialogResult = DialogResult.Yes;
                m_GestureSet = m_MainForm.GesturesData[(string)KnownGesturesList.SelectedItem].TestSet;
                Close();
            }
        }

		private void RemoveGestureButton_Click(object sender, EventArgs e)
		{
			if (KnownGesturesList.SelectedIndex != -1)
			{
				int index = KnownGesturesList.SelectedIndex;
				string item = (string)KnownGesturesList.SelectedItem;
				KnownGesturesList.SelectedIndex = -1;
				GestureRecognizerData data = m_MainForm.GesturesData[item];
				m_MainForm.GesturesData.Remove(item);
				KnownGesturesList.Items.RemoveAt(index);

				if (MessageBox.Show("You have exclued from current session '" + item + "' gesture.\nDo you want to remove files also?", "Remove files?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
					data.Remove();
			}
		}

		private void KnownGesturesList_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (KnownGesturesList.SelectedIndex != -1)
			{
				GesturePreviewPanel.Gesture = m_MainForm.GesturesData[(string)KnownGesturesList.SelectedItem].Gesture;
				if (m_MainForm.GesturesData[GesturePreviewPanel.Gesture.Name].TrainingSet.Gestures > 0)
				{
                    if (m_MainForm.GesturesData[GesturePreviewPanel.Gesture.Name].TestSet.Gestures > 0)
                    {
                        if (m_MainForm.GesturesData[GesturePreviewPanel.Gesture.Name].NeuralNetwork != null)
                            GesturePreviewPanel.BaseColor = Color.Green;
                        else
                            GesturePreviewPanel.BaseColor = Color.Blue;
                    }
                    else
                        GesturePreviewPanel.BaseColor = Color.Orange;
				}
				else
					GesturePreviewPanel.BaseColor = Color.Red;

				TrainingSetCountLabel.Text = "TS Count: " + m_MainForm.GesturesData[GesturePreviewPanel.Gesture.Name].TrainingSet.Gestures.ToString();
                TestSetCountLabel.Text = "VS Count: " + m_MainForm.GesturesData[GesturePreviewPanel.Gesture.Name].TestSet.Gestures.ToString();

				RemoveGestureButton.Enabled = true;
				CreateTrainingSetButton.Enabled = true;
                CreateTestSetButton.Enabled = true;
			}
			else
			{
				GesturePreviewPanel.BaseColor = Color.Gray;
				GesturePreviewPanel.Gesture = null;
				RemoveGestureButton.Enabled = false;
				CreateTrainingSetButton.Enabled = false;
                CreateTestSetButton.Enabled = false;
			}
		}

		protected override void OnClosing(CancelEventArgs e)
		{
            if (DialogResult == DialogResult.Yes)
            {
                GestureRecognizerData data = m_MainForm.GesturesData[(string)KnownGesturesList.SelectedItem];
                GestureSetCreationForm ts_form = new GestureSetCreationForm(m_MainForm, data, m_GestureSet);
                ts_form.ShowDialog();
                if (ts_form.DialogResult != DialogResult.OK)
                    e.Cancel = true;

                KnownGesturesList.SelectedIndex = -1;
            }
		}
	}
}

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