-
1 Attachment(s)
[RESOLVED] Mouseposistion gone weird?
OK i have a label that updates every 20 milliseconds with the coordinates of the mouse using this
Code:
Label1.Text = MousePosition.Tostring
Then i have a global click hook that when the mouse is clicked it puts the X & Y of the mouse position into textboxes.
When i click with the mouse it puts different numbers into the textboxs than whats in the label, ill screenshoot it.
Why is it doing this?
And how can i fix it?
THE LABEL IS IN TOP RIGHT CORNER AND IT DOES WORK just so you know :)
-
Re: Mouseposistion gone weird?
because, you're using a windows api to get the coords that are relative to the whole desktop, then in the app you're probably setting the textbox's x and y = to the x,y of the click relative to the form.
Justin
-
Re: Mouseposistion gone weird?
How do i get them both to be relative to the whole screen?
-
Re: Mouseposistion gone weird?
What code are you using to get the cursor position (524,189) ?
Justin
-
Re: Mouseposistion gone weird?
I am using the
Code:
ON TIMER TICK
CursorP.Text = MousePosition.ToString
EDIT: you can see it in the screenshot
-
Re: Mouseposistion gone weird?
So if they click, update the label, and the textboxes at the same time.. do you have different threads for handling the mouseclick and then one for the position?
so in your threadfunc sub do:
vb Code:
public sub threadfunc()
while running
'if left button clicked
'get mouseposition
'create delegate to update the label with that mousposition
'create delegate to update the textboxes with that moueposition
end while
end sub
Can't remember the exact code, that's why i put comments.
Justin
-
Re: Mouseposistion gone weird?
If your wanting the co-ods of the whole screen you need to use MousePosition again. You could still use the click event but use MousePosition.X.ToString() & MousePosition.Y.ToString() instead of whatever you did use.
-
Re: Mouseposistion gone weird?
Code:
Private Sub threadfunc()
For i As Integer = 1 To 15
CollectingStarted = True
Me.Invoke(updatebox, New Object() {"X" & i.ToString, "Y" & i.ToString, MousePosition.X, MousePosition.Y})
Dim obj As Object = Me.Invoke(d)
Dim p As Point = CType(obj, Point)
While running
Dim Rclick As Boolean = KeyStatus(Keys.RButton)
Dim Lclick As Boolean = KeyStatus(Keys.LButton)
If CollectingStarted = True Then
If Lclick Then
If cAmount = 1 Then
X1.Text = p.X.ToString
Y1.Text = p.Y.ToString
Threading.Thread.Sleep(500)
cAmount = 2
ElseIf cAmount = 2 Then
X2.Text = p.X
Y2.Text = p.Y
Threading.Thread.Sleep(500)
cAmount = 3
End If
End If
If Rclick Then
End If
End If
End While
Next
End Sub
Public Function Getcoords() As Point
Return New Point(MousePosition.X.ToString, MousePosition.Y.ToString)
End Function
-
Re: Mouseposistion gone weird?
Mouseposition.Y and Mouseposition.Y.ToString Are exactly the same, you dont need the .tostring part and also i was already using that.
Also MonkOFox Nope, i have a Timer1.Tick with interval 20miliseconds and the CursorP.Text = MousePosition.ToString is on that. So it automaticly updates every 20 miliseconds.
-
Re: Mouseposistion gone weird?
bump, anyone have any clue to why its doing this?
-
Re: Mouseposistion gone weird?
If you're getting the mouseposition every 20 milliseconds and setting the label in a timer. Then when they click the left button, a totally different thread updates the textbox's to the position their mouse was clicked at, why would you expect them to be the same?
Justin
-
Re: Mouseposistion gone weird?
Inside the UpdateTB method, set the label's text there, that way their always the same.
Justin
get rid of the timer...
-
Re: Mouseposistion gone weird?
because the label is the mouseposition and when u click it should be that mouseposition shouldn't it, correct me if im wrong
-
Re: Mouseposistion gone weird?
I dont understand now... You basically just answered your original question...
If the label represents where the mouse currently is (regardless of click) and the click updates the textboxes to the point it was clicked, what exactly is the problem? The label and the textbox values with almost always have different values (unless you click and then don't move your mouse).
Justin
-
Re: Mouseposistion gone weird?
Thats what i did in the screenshot, i clicked and didn't move mouse and the values where different, so the label had different X and Y to the textbox, Which is what my question was :)
-
Re: Mouseposistion gone weird?
Ok this is some sample code I wrote, that works just fine.
vb Code:
Public Class Form1
Public Delegate Sub Hello(ByVal a As String)
Public Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Int32) As UShort
Private running As Boolean = True
Private t As System.Threading.Thread
Private t2 As System.Threading.Thread
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
t = New Threading.Thread(New Threading.ThreadStart(AddressOf threadfunc))
t2 = New Threading.Thread(New Threading.ThreadStart(AddressOf threadfunc2))
t.Start()
t2.Start()
End Sub
Public Sub threadfunc()
While running
Dim lbpushed As Boolean = KeyStatus(Keys.LButton)
If lbpushed Then
Dim d As New Hello(AddressOf Me.updateTB)
Me.Invoke(d, New Object() {MousePosition.ToString})
End If
End While
End Sub
Public Sub threadfunc2()
While running
Threading.Thread.Sleep(20)
Dim d As New Hello(AddressOf Me.updateLbl)
Me.Invoke(d, New Object() {MousePosition.ToString})
End While
End Sub
Public Sub updateTB(ByVal value As String)
Me.TextBox1.Text = value
End Sub
Public Sub updateLbl(ByVal value As String)
Me.LinkLabel1.Text = value
End Sub
Public Shared ReadOnly Property KeyStatus(ByVal Key As Keys) As Boolean
Get
If Key = Keys.LButton AndAlso My.Computer.Mouse.ButtonsSwapped Then
Key = Keys.LButton
MessageBox.Show("left button pushed")
ElseIf Key = Keys.RButton AndAlso Not My.Computer.Mouse.ButtonsSwapped Then
Key = Keys.RButton
MessageBox.Show("right button pushed")
End If
Return GetAsyncKeyState(Key) And &H8000US
End Get
End Property
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Try
t.Abort()
t2.Abort()
Catch ex As Exception
If t.IsAlive Then
t.Abort()
End If
If t2.IsAlive Then
t.Abort()
End If
End Try
t.Join()
t2.Join()
End Sub
End Class
when i move it updates the linklabel, if i hold my mouse still and click, then at that point the linklabel and the textbox's values are the exact same... but if i move the mouse after I click they will of course differ.
Justin
-
Re: Mouseposistion gone weird?
Thankyou, but
I dont see why i need all that, why arn't they the same when i keep mouse still and click? with my method of the timer and MousePosition.ToString???
-
Re: Mouseposistion gone weird?
Post the entire code, so I can see what you do in your timer_tick event.
Justin
-
Re: Mouseposistion gone weird?
Sure,
There is only one line to it. But as you wish. :)
Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If IsConnectionAvailable("http://www.google.co.uk") = True Then
ProgressBar1.Value = 100
End If
If ProgressBar1.Value = 100 Then
Label9.Text = "Load Done"
End If
CursorP.Text = MousePosition.ToString <--- THIS IS ALL THERE IS TOO IT
If CTimer.Enabled = True Then
started = True
Else
started = False
End If
If started2 = True Then
PlaybackB.Enabled = False
StopP.Enabled = True
Status2TB.Text = "Started"
ElseIf started2 = False Then
If X1.Text = "" Or Y1.Text = "" Then
PlaybackB.Enabled = False
Else
PlaybackB.Enabled = True
End If
StopP.Enabled = False
Status2TB.Text = "Stopped"
End If
If started = True Then
StartB.Enabled = False
StopB.Enabled = True
StatusTB.Text = "Started"
NumUpDown.Enabled = False
ElseIf started = False Then
StartB.Enabled = True
StopB.Enabled = False
StatusTB.Text = "Stopped"
NumUpDown.Enabled = True
End If
If CollectingStarted = True Then
Collect.Enabled = False
Collect.Text = "Collecting"
ElseIf CollectingStarted = False Then
Collect.Enabled = True
Collect.Text = "Start Collecting"
End If
End Sub
-
Re: Mouseposistion gone weird?
I've come to a point where I have to say, I just don't know. I've given an example of using two thread to accomplish what you want. If you're timer is ticking every 20ms just like my thread is, then everything should be fine. I'm not totally sure the System.Windows.Forms.Timer supports a multiple threaded environment though.
Just not something I'm familiar about. If I wanted something to happen in the background, I would use a BackgroundWorker, not a Timer. The only reason I'm saying anything about the Timer, is because it seems to be the only difference in you approach and my test application.
Justin
If I'm wrong, someone will hopefully help me out
-
Re: Mouseposistion gone weird?
Emcrank, is there anyway you can upload your full project. I'm sure people will find it easier to help if they have something whole to look at rather than one method.
-
Re: Mouseposistion gone weird?
Well thankyou for all your help :)
Ill try your way :)
-
Re: Mouseposistion gone weird?
Ok i used the thread2() method and for some weird reason it worked? I have come to a conclusion timers are officially not my friend. Thankyou for your help :)
-
Re: [RESOLVED] Mouseposistion gone weird?
Sweet! I'm glad you got it working : )
Justin