Reputation: 908
I am making a project in VB.NET in which we track the cursor's location anywhere on the screen.
In Timer 1_ Tick Event, I have set frequency as 1 millisecond so that it can track the location more frequently. The following is the code I have inserted in Timer1_Tick event
Me.Cursor = New Cursor(Cursor.Current.Handle)
Label4.Text = Cursor.Position.X
Label5.Text = Cursor.Position.Y
I want the user to click and drag outside the form to get dimensions of the selected area in pixels. Can anyone please help how to control mouse clicking outside a form?
Thanks in Advance!
Upvotes: 0
Views: 2140
Reputation: 54532
It sounds like you are trying to get an area from outside of your application, you could try using a Global Hook, such as is available from this CodePlex Project.
This is a simple example that is responding to the MouseDown and MouseUp events creating a Rectangle then printing the results to a TextBox it should give you an idea how to proceed.
Imports MouseKeyboardActivityMonitor
Imports MouseKeyboardActivityMonitor.WinApi
Public Class Form1
Private m_MouseHookManager As MouseHookListener
Dim tempPoint As Point
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
m_MouseHookManager = New MouseHookListener(New GlobalHooker)
m_MouseHookManager.Enabled = True
AddHandler m_MouseHookManager.MouseUp, AddressOf HookManager_MouseUp
AddHandler m_MouseHookManager.MouseDown, AddressOf HookManager_MouseDown
TextBox1.ScrollBars = ScrollBars.Vertical
End Sub
Private Sub HookManager_MouseUp(sender As Object, e As MouseEventArgs)
Dim r As Rectangle = New Rectangle(tempPoint.X, tempPoint.Y, e.X - tempPoint.X, e.Y - tempPoint.Y)
TextBox1.Text += "Left: " & r.Left & vbCrLf
TextBox1.Text += "Right: " & r.Top & vbCrLf
TextBox1.Text += "Width: " & r.Width & vbCrLf
TextBox1.Text += "Height: " & r.Height & vbCrLf
tempPoint = Point.Empty
End Sub
Private Sub HookManager_MouseDown(sender As Object, e As MouseEventArgs)
tempPoint = e.Location
End Sub
End Class
Upvotes: 2