Results 1 to 24 of 24

Thread: [RESOLVED] Mouseposistion gone weird?

  1. #1

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    Resolved [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
    Attached Images Attached Images  

  2. #2
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    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
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  3. #3

  4. #4
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Mouseposistion gone weird?

    What code are you using to get the cursor position (524,189) ?

    Justin
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  5. #5

  6. #6
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    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:
    1. public sub threadfunc()
    2.  
    3. while running
    4.  
    5. 'if left button clicked
    6. 'get mouseposition  
    7. 'create delegate to update the label with that mousposition
    8. 'create delegate to update the textboxes with that moueposition
    9.  
    10. end while
    11.  
    12. end sub

    Can't remember the exact code, that's why i put comments.

    Justin
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  7. #7
    Member
    Join Date
    Jun 2010
    Posts
    55

    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.

  8. #8

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    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

  9. #9

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    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.

  10. #10

  11. #11
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    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
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  12. #12
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    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...
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  13. #13

  14. #14
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    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
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  15. #15

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    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

  16. #16
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Mouseposistion gone weird?

    Ok this is some sample code I wrote, that works just fine.

    vb Code:
    1. Public Class Form1
    2.  
    3.     Public Delegate Sub Hello(ByVal a As String)
    4.  
    5.     Public Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Int32) As UShort
    6.  
    7.     Private running As Boolean = True
    8.     Private t As System.Threading.Thread
    9.     Private t2 As System.Threading.Thread
    10.  
    11.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    12.         t = New Threading.Thread(New Threading.ThreadStart(AddressOf threadfunc))
    13.         t2 = New Threading.Thread(New Threading.ThreadStart(AddressOf threadfunc2))
    14.         t.Start()
    15.         t2.Start()
    16.  
    17.     End Sub
    18.  
    19.      Public Sub threadfunc()
    20.         While running
    21.  
    22.             Dim lbpushed As Boolean = KeyStatus(Keys.LButton)
    23.  
    24.             If lbpushed Then
    25.                 Dim d As New Hello(AddressOf Me.updateTB)
    26.                 Me.Invoke(d, New Object() {MousePosition.ToString})
    27.             End If
    28.  
    29.  
    30.         End While
    31.     End Sub
    32.  
    33.     Public Sub threadfunc2()
    34.  
    35.         While running
    36.  
    37.             Threading.Thread.Sleep(20)
    38.             Dim d As New Hello(AddressOf Me.updateLbl)
    39.             Me.Invoke(d, New Object() {MousePosition.ToString})
    40.  
    41.         End While
    42.  
    43.     End Sub
    44.  
    45.     Public Sub updateTB(ByVal value As String)
    46.  
    47.         Me.TextBox1.Text = value
    48.  
    49.     End Sub
    50.  
    51.     Public Sub updateLbl(ByVal value As String)
    52.  
    53.         Me.LinkLabel1.Text = value
    54.     End Sub
    55.  
    56.     Public Shared ReadOnly Property KeyStatus(ByVal Key As Keys) As Boolean
    57.         Get
    58.             If Key = Keys.LButton AndAlso My.Computer.Mouse.ButtonsSwapped Then
    59.                 Key = Keys.LButton
    60.                 MessageBox.Show("left button pushed")
    61.             ElseIf Key = Keys.RButton AndAlso Not My.Computer.Mouse.ButtonsSwapped Then
    62.                 Key = Keys.RButton
    63.                 MessageBox.Show("right button pushed")
    64.             End If
    65.             Return GetAsyncKeyState(Key) And &H8000US
    66.         End Get
    67.     End Property
    68.  
    69.     Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    70.         Try
    71.             t.Abort()
    72.             t2.Abort()
    73.         Catch ex As Exception
    74.             If t.IsAlive Then
    75.                 t.Abort()
    76.             End If
    77.             If t2.IsAlive Then
    78.                 t.Abort()
    79.             End If
    80.         End Try
    81.         t.Join()
    82.         t2.Join()
    83.     End Sub
    84.  
    85.  
    86. 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
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  17. #17

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    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???

  18. #18
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Mouseposistion gone weird?

    Post the entire code, so I can see what you do in your timer_tick event.

    Justin
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  19. #19

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    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

  20. #20
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    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
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  21. #21
    Member
    Join Date
    Jun 2010
    Posts
    55

    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.

  22. #22

  23. #23

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    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

  24. #24
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: [RESOLVED] Mouseposistion gone weird?

    Sweet! I'm glad you got it working : )

    Justin
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

Posting Permissions

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



Click Here to Expand Forum to Full Width