Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[TUT] The Mouse [VB]
#1
I leeched this from here!


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:

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


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

Code:
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:

Code:
Cursor.Position = New Point(250, 250)







Get which button was used

When using the MouseDown event:


Code:
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:
Code:
e.Button.ToString

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

Code:
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:

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

.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:



Code:
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:

Code:
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:


Code:
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:

Code:
Mouse_Click(2, "down")
Mouse_Click(2, "up")
[Image: 20r9vh4.jpg]
Reply
#2
ur code /code did not wrk
If you are willing to join SF Webmasters.
Reply
#3
He probably ripped this.
Reply
#4
This is ripped, jah?
Reply
#5
This is nice Big Grin Thank you Big Grin
Returning to SF / HF. Long story Tongue
Reply
#6
(04-15-2010, 12:46 PM)Mr.Kewl Wrote: ur code /code did not wrk

Its working now,Sry
[Image: 20r9vh4.jpg]
Reply
#7
Ripped from --> http://forum.codecall.net/vb-tutorials/2...c-net.html
[Image: IKILLUNEW.jpg]
Reply
#8
Nice tutorial, thanks !
Keep it up
Reply
#9
Nice, But I have seen tutorial like this on another forums.
[Image: ShuTdown.png]
Reply
#10
(04-24-2010, 07:14 AM)ShuTdown Wrote: Nice, But I have seen tutorial like this on another forums.

Yes. He leeched his tutorials without giving credits, which is why he is banned Oui
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [TUT] How to make a simple WebBrowser in VB 2010! [TUT] - [ Pictures] Statics 95 62,010 10-07-2012, 06:56 AM
Last Post: a99
  [TUT]Auto-Update System[TUT] HB Virus 3 2,432 01-07-2012, 02:21 PM
Last Post: Mastermrz
  [TUT]Enable and Disable TaskManger in vb.net [TUT] HB Virus 4 3,180 12-19-2011, 10:10 AM
Last Post: euverve
  [TuT] VB6 Message Box Spammer [TuT] HakkuR* 30 7,572 06-06-2011, 04:43 PM
Last Post: Black Demon
  ~[VB.NET]~ [TUT+SOURCE] Computer To Cell Phone Messenger [TUT] ThePrinCe 11 3,748 04-13-2011, 10:15 AM
Last Post: Mammoth

Forum Jump:


Users browsing this thread: 2 Guest(s)