Last update Jul 13, 2007 up top

Mouse gesture code by VB.NET


Public Class Form1

	Dim mg_enable As Boolean '// Is gesturing?
	Dim mg_x, mg_y, mg_dist As Integer '// original x, y location of mouse, gesture distance
	Dim mg_direction As String '// Result of gestures

	'// Mouse button was downed.
	Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
		If e.Button = Windows.Forms.MouseButtons.Right Then '// If it's right.
			mg_enable = True '// Start mouse gesture!
			mg_direction = "" '// Init gestures
			mg_dist = 20 '// 1 gesture per 20 dot moving
			mg_x = e.X '// Keep X location of mouse when right button was down.
			mg_y = e.Y '// Keep Y location of mouse when right button was down.
		End If
	End Sub

	'// Mouse button was uped.
	Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
		If e.Button = Windows.Forms.MouseButtons.Right Then '// If it's right.
			mg_enable = False '// Stop mouse gesture.

			Select Case mg_direction '// Execute gesture.
				Case "DR" '// Exit
					End
				Case "RU" '// Maximize window / Restore Window Size
					If Me.WindowState = FormWindowState.Normal Then
						Me.WindowState = FormWindowState.Maximized
					Else
						Me.WindowState = FormWindowState.Normal
					End If
			End Select

		End If
	End Sub

	'// Mouse moving
	Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
		'// if in gesturing
		If mg_enable Then
			'// 
			If Math.Abs(mg_x - e.X) > mg_dist Or Math.Abs(mg_y - e.Y) > mg_dist Then
				'// direction decision by moving distance of x y
				If Math.Abs(mg_x - e.X) > Math.Abs(mg_y - e.Y) Then
					'// if direction is left
					If mg_x > e.X Then
						'// set new location of mouse
						mg_x = e.X
						'// if it's not same last gesture then add gesture
						If Microsoft.VisualBasic.Right(mg_direction, 1) <> "L" Then
							mg_direction = mg_direction & "L"
						End If
					Else
						mg_x = e.X
						If Microsoft.VisualBasic.Right(mg_direction, 1) <> "R" Then
							mg_direction = mg_direction & "R"
						End If
					End If
				Else
					If mg_y > e.Y Then
						mg_y = e.Y
						If Microsoft.VisualBasic.Right(mg_direction, 1) <> "U" Then
							mg_direction = mg_direction & "U"
						End If
					Else
						mg_y = e.Y
						If Microsoft.VisualBasic.Right(mg_direction, 1) <> "D" Then
							mg_direction = mg_direction & "D"
						End If
					End If
				End If
			End If
		End If
	End Sub

End Class