Results 1 to 9 of 9

Thread: [RESOLVED] location of the mouse

  1. #1

    Thread Starter
    Addicted Member Mojtaba's Avatar
    Join Date
    Dec 2020
    Posts
    153

    Resolved [RESOLVED] location of the mouse

    Hello friends
    I get the location of the mouse
    Both inside the form and outside the form
    The position is not exact
    For example to draw a line
    X1 , Y1 I set it equal to the location of the mouse

    -------------------
    Scale mode on pixel
    -------------------

    Click on the form to find the position of the mouse

    HTML Code:
    Option Explicit
    Private Type POINTAPI
    X As Long
    Y As Long
    End Type
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private Sub Form_Load()
    Timer1.Interval = 100
    Timer1.Enabled = True
    End Sub
    Private Sub Timer1_Timer()
    Dim pt As POINTAPI
    If GetAsyncKeyState(1) <> 0 Then
    GetCursorPos pt
    Textx.Text = pt.X * Screen.TwipsPerPixelX
    Texty.Text = pt.Y * Screen.TwipsPerPixelY
    Line1.X1 = pt.X * Screen.TwipsPerPixelX
    Line1.Y1 = pt.Y * Screen.TwipsPerPixelY
    End If
    End Sub
    Download Attachments Mouse Position : Mouse Position.zip

  2. #2

  3. #3

    Thread Starter
    Addicted Member Mojtaba's Avatar
    Join Date
    Dec 2020
    Posts
    153

    Re: location of the mouse

    Yes, the question is about the exact location of the mouse
    When drawing a line from the location of the mouse is not accurate

  4. #4
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,064

    Re: location of the mouse

    You need ScreenToClient:

    Code:
    Option Explicit
    
    Private Type POINTAPI
        X As Long
        Y As Long
    End Type
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
    
    Private Sub Form_Load()
        Timer1.Interval = 100
        Timer1.Enabled = True
    End Sub
    Private Sub Timer1_Timer()
        Dim pt As POINTAPI
        If GetAsyncKeyState(1) <> 0 Then
        GetCursorPos pt
        ScreenToClient hWnd, pt
        Textx.Text = pt.X * Screen.TwipsPerPixelX
        Texty.Text = pt.Y * Screen.TwipsPerPixelY
        Line1.X1 = pt.X * Screen.TwipsPerPixelX
        Line1.Y1 = pt.Y * Screen.TwipsPerPixelY
        End If
    End Sub

  5. #5
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,730

    Re: location of the mouse

    also, he could use:

    Code:
    Private Declare Function WindowFromPoint Lib "user32" (ByVal xpoint As Long, ByVal ypoint As Long) As Long
    to know that he is in the right form/picturebox/whatever

  6. #6

    Thread Starter
    Addicted Member Mojtaba's Avatar
    Join Date
    Dec 2020
    Posts
    153

    Re: location of the mouse

    Thankful Eduardo
    Another problem in mode
    Form Scale mode on pixel
    There is no problem in Twip mode

  7. #7
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,064

    Re: location of the mouse

    Quote Originally Posted by Mojtaba View Post
    Thankful Eduardo
    Another problem in mode
    Form Scale mode on pixel
    There is no problem in Twip mode
    If you want to work in pixels, then remove the * Screen.TwipsPerPixelX and * Screen.TwipsPerPixelY from the code.

  8. #8

    Thread Starter
    Addicted Member Mojtaba's Avatar
    Join Date
    Dec 2020
    Posts
    153

    Resolved Re: location of the mouse

    The problem was solved thanks to Eduardo

  9. #9
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: location of the mouse

    You didn't mark the thread as resolved.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

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