Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

The Mouse in VB.NET

VB.NET form

  • Please log in to reply
8 replies to this topic

#1 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 08 November 2009 - 05:07 AM

In this tutorial I will show how to get the actions made by the mouse but also show you how to control the mouse.





Its Location

To get the position where the mouse is on the screen you can use MousePosition, you will then get the mouse's position of the screen as a point, so to get the X and Y values you just do something like this:

        MessageBox.Show("X: " & MousePosition.X & " Y: " & MousePosition.Y)



But you can do the same thing with Cursor.Position, like so:

        MessageBox.Show("X: " & Cursor.Position.X & " Y: " & Cursor.Position.Y)


And Cursor.Position is not readonly as MousePosition is, this means you can move the mouse cursor to a special point of the screen like this:

        Cursor.Position = New Point(250, 250)








Get which button was used

When using the MouseDown event:


    Private Sub frmMain_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick

End Sub



on something we can check which button that was press by using:
e.Button.ToString


Since e.Button is an Enum with all the different buttons you can also do like this:

    Private Sub frmMain_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
Select (e.Button)
Case Windows.Forms.MouseButtons.Left
MessageBox.Show("You pressed the left button")
Case Windows.Forms.MouseButtons.Right
MessageBox.Show("Don't use the Right button, use the left instead")
Case Windows.Forms.MouseButtons.Middle
MessageBox.Show("Scroll with it instead")
Case Windows.Forms.MouseButtons.XButton1
MessageBox.Show("Extra button 1 was pressed")
Case Windows.Forms.MouseButtons.XButton2
MessageBox.Show("Extra button 2 was pressed")
Case Windows.Forms.MouseButtons.None
MessageBox.Show("No button was pressed :S")
End Select
End Sub








Scrolling

You can also get the direction and how much you scrolled with the mouse wheel by using the MouseWheel event:

    Private Sub frmMain_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
MessageBox.Show(e.Delta)
End Sub


e.Delta is how much you scrolled, this is depending on the options in the control panel about mouse wheel scrolling. So we can't be sure what these values will be, but the good thing is that scrolling ups is always positive and scrolling down is negative. So therefor we can do like this:



    Private Sub frmMain_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
If e.Delta > 0 Then
MessageBox.Show("You scrolled up")
ElseIf e.Delta < 0 Then
MessageBox.Show("You scrolled down")
End If
End Sub







Simulating mouse clicks

It's also possible to simulate mouse clicks from button 1,2 and 3 (Left, Right and Middle). To do this we first need to declare the mouse clicking sub:

    Private Declare Sub mouse_event Lib "user32.dll" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)


Then we create another sub to easier use it:


     Private Sub Mouse_Click(ByVal button As Integer, ByVal state As String)
Select Case button
Case 1
If state = "down" Then
mouse_event(2, 100, 100, 0, 0)
Else
mouse_event(4, 100, 100, 0, 0)
End If
Case 2
If state = "down" Then
mouse_event(8, 100, 100, 0, 0)
Else
mouse_event(16, 100, 100, 0, 0)
End If
Case 3
If state = "down" Then
mouse_event(32, 100, 100, 0, 0)
Else
mouse_event(64, 100, 100, 0, 0)
End If
End Select

End Sub


So if we now want to simulate a click from button 2(Right mouse button) we'll do like this:

Mouse_Click(2, "down")
Mouse_Click(2, "up")



And that was everything for this tutorial, I hope you learned something :)
  • 2

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 08 November 2009 - 03:22 PM

These type of mouse events are very handy when creating a GUI. Nicely done, +rep!
  • 0

#3 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 13 November 2009 - 08:17 AM

Nice +rep
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#4 Zerkei

Zerkei

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 26 December 2009 - 10:45 AM

Nice tutorial, though those mouse movements can only be recorded if the user is on the form right?
  • 0

#5 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 26 December 2009 - 01:00 PM

Yes

This answer is wrong, what were I thinking lol...

Edited by Vswe, 30 September 2010 - 12:37 PM.

  • 0

#6 arkanion

arkanion

    CC Newcomer

  • Just Joined
  • PipPip
  • 10 posts

Posted 13 January 2010 - 12:01 PM

thanks
  • -1

#7 pyaephyokyaw1992

pyaephyokyaw1992

    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 23 September 2010 - 09:44 PM

thanks
  • 0

#8 Jarryd

Jarryd

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 63 posts
  • Location:Australia
  • Programming Language:C, Java, C++, C#

Posted 24 September 2010 - 12:14 AM

Nice tutorial mate, this will come of good use in the complicated GUI
  • 0

#9 Nikey646

Nikey646

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts
  • Programming Language:PHP
  • Learning:PHP, Visual Basic .NET

Posted 26 November 2012 - 06:16 AM

It's possible to expand the simulation element of this, by doing the following:
Dim x As Integer = Cursor.Position.X
Dim y As Integer = Cursor.Position.Y
Mouse_Click(1, "down")
Cursor.Position = New Point(x + 50, y + 50)
Mouse_Click(1, "up")
Sorry if this is classified as grave digging, just thought i would contribute to the post (:
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download