+ Reply to Thread
Results 1 to 6 of 6

Thread: The Mouse in VB.NET

  1. #1
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    16
    Posts
    8,819
    Blog Entries
    5

    The Mouse in VB.NET

    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:

    [highlight=VB.NET] MessageBox.Show("X: " & MousePosition.X & " Y: " & MousePosition.Y)[/highlight]


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

    [highlight=VB.NET] MessageBox.Show("X: " & Cursor.Position.X & " Y: " & Cursor.Position.Y)
    [/highlight]

    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:

    [highlight=VB.NET] Cursor.Position = New Point(250, 250)[/highlight]







    Get which button was used

    When using the MouseDown event:


    [highlight=VB.NET] Private Sub frmMain_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick

    End Sub[/highlight]


    on something we can check which button that was press by using:
    [highlight=VB.NET]e.Button.ToString[/highlight]

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

    [highlight=VB.NET] 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[/highlight]







    Scrolling

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

    [highlight=VB.NET] Private Sub frmMain_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
    MessageBox.Show(e.Delta)
    End Sub[/highlight]

    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:



    [highlight=VB.NET] 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[/highlight]






    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:

    [highlight=VB.NET] 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)[/highlight]

    Then we create another sub to easier use it:


    [highlight=VB.NET] 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[/highlight]

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

    [highlight=VB.NET]Mouse_Click(2, "down")
    Mouse_Click(2, "up")[/highlight]


    And that was everything for this tutorial, I hope you learned something

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: The Mouse in VB.NET

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

  3. #3
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,702
    Blog Entries
    57

    Re: The Mouse in VB.NET

    Nice +rep
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #4
    Newbie Zerkei is an unknown quantity at this point
    Join Date
    Dec 2009
    Posts
    8

    Re: The Mouse in VB.NET

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

  5. #5
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    16
    Posts
    8,819
    Blog Entries
    5

    Re: The Mouse in VB.NET

    Yes, or any controls on the form if you change keypreview to True.

  6. #6
    Newbie arkanion is an unknown quantity at this point
    Join Date
    Jan 2010
    Posts
    10

    Re: The Mouse in VB.NET

    thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Replies: 3
    Last Post: 11-02-2009, 07:40 PM
  2. Replies: 1
    Last Post: 11-02-2009, 07:38 AM
  3. Flash: Keyboard and Mouse Movement
    By chili5 in forum Tutorials
    Replies: 4
    Last Post: 08-27-2009, 02:46 PM
  4. Rechargeable Cordless Mouse
    By eric in forum Computer Hardware
    Replies: 2
    Last Post: 11-24-2007, 11:39 AM
  5. Mouse made for Gaming ??
    By xtraze in forum Technology Ramble
    Replies: 10
    Last Post: 01-25-2007, 07:28 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts