CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2001
    Posts
    17

    Maybe to hard to do?

    OK so how can i get VB6 to determine where the mouse is in windows so that i can use MoveMouse to move it back to that exact spot to fire off mouse events?
    How can i make it so that i can determine the exact locatation of the mouse any time i want.?
    Is there a way to set it up in VB6 so that it can record the x and y coords of the mouse with a push of a button?

    I want to do this in a form, click the button to start recoding then move the mouse in windows98(or higher) and when i click at the spot i want to it records those coords for me or shows them to me so i can use them to send the mouse back to that exact spot later.



  2. #2
    Join Date
    Jun 2001
    Location
    Memphis, TN
    Posts
    146

    Re: Maybe to hard to do?

    I hope this example helps!

    put the following code in your main module...


    public Declare Function GetCursorPos Lib "user32" (lpPoint as POINTAPI) as Long

    'point type returned by GetCursorPos API
    public Type POINTAPI
    x as Long
    y as Long
    End Type




    then call the API in a command button like this...


    'create instance of POINTAPI
    Dim pt as POINTAPI
    'locate the cursor
    GetCursorPos pt
    msgbox "Your mouse is at X=" & pt.x & " and Y=" & pt.y





  3. #3
    Join Date
    Aug 2001
    Posts
    17

    Re: Maybe to hard to do?

    THANK YOU!!!!!!!!!!!!!!
    works good but i was wondering sice i was able to modifie it a litte so it puts the x and the y in feilds for me is there a way to get it to get the coords outside of the form?

    When i did it your way it got the coords right where the button was and when i do it my way the only way i can get it is inside the form.

    Can you assist me again?



  4. #4
    Join Date
    Jun 2001
    Location
    Memphis, TN
    Posts
    146

    Re: Maybe to hard to do?

    See if this is closer to what you are looking for...

    create a form with a timer control (Timer1) on it and two labels (Label1, Label2). put the API declaration and the POINTAPI Type definition in a module in the project just as before. set the timer's invertval property to 10 (this makes it run its event 100 times per second). inside the timer event, add the following code:


    'create instance of POINTAPI
    Dim pt as POINTAPI
    'locate the cursor
    GetCursorPos pt

    Label1.Caption = "X = " & pt.X
    Label2.Caption = "Y = " & pt.Y




    I haven't tested this but i think it will work okay. Let me know if ya need anything more. Good luck!

    Jeff


  5. #5
    Join Date
    Aug 2001
    Location
    PA
    Posts
    150

    Re: Maybe to hard to do?

    in you module pu this code

    Option Explicit

    Type POINTAPI
    X As Long
    Y As Long
    End Type
    Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
    End Type

    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
    Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long
    Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
    Private Declare Function ClientToScreen Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
    Private Declare Function ClipCursor Lib "user32" (lpRect As Any) As Long
    Private Declare Function GetClientRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
    Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function ReleaseCapture Lib "user32" () As Long
    Private Declare Function GetCapture Lib "user32" () As Long
    Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long

    ' Return the state of the Left button.

    Function LeftButton() As Boolean
    LeftButton = (GetAsyncKeyState(vbKeyLButton) And &H8000)
    End Function

    ' Return the state of the Right button.

    Function RightButton() As Boolean
    RightButton = (GetAsyncKeyState(vbKeyRButton) And &H8000)
    End Function

    ' Return the state of the Middle button.

    Function MiddleButton() As Boolean
    MiddleButton = (GetAsyncKeyState(vbKeyMButton) And &H8000)
    End Function

    ' Return the state of the mouse buttons as a bit-coded value.
    ' (same format as the Button argument of a Mousexxxx event procedure.)

    Function MouseButton() As Integer
    If GetAsyncKeyState(vbKeyLButton) < 0 Then
    MouseButton = 1
    End If
    If GetAsyncKeyState(vbKeyRButton) < 0 Then
    MouseButton = MouseButton Or 2
    End If
    If GetAsyncKeyState(vbKeyMButton) < 0 Then
    MouseButton = MouseButton Or 4
    End If
    End Function

    ' Return the number of mouse buttons.

    Function MouseButtonsNumber() As Integer
    Const SM_CMOUSEBUTTONS = 43
    MouseButtonsNumber = GetSystemMetrics(SM_CMOUSEBUTTONS)
    End Function

    ' Show and Hide the mouse cursor.
    ' (These calls must be balanced.)

    Sub MouseHide()
    ShowCursor False
    End Sub

    Sub MouseShow()
    ShowCursor True
    End Sub

    ' Get mouse X coordinates (screen coordinates).
    ' If a window handle is passed, the result are client area coordinates
    ' relative to that window.

    Function MouseX(Optional ByVal hWnd As Long) As Long
    Dim lpPoint As POINTAPI
    GetCursorPos lpPoint
    If hWnd Then ScreenToClient hWnd, lpPoint
    MouseX = lpPoint.X
    End Function

    Function MouseY(Optional ByVal hWnd As Long) As Long
    Dim lpPoint As POINTAPI
    GetCursorPos lpPoint
    If hWnd Then ScreenToClient hWnd, lpPoint
    MouseY = lpPoint.Y
    End Function

    ' Mouse mouse cursor. If a window handle is passed, the coordinates
    ' are taken as relative to that window's client area.

    Sub MoveMouseCursor(ByVal X As Long, ByVal Y As Long, Optional ByVal hWnd As Long)
    If hWnd = 0 Then
    SetCursorPos X, Y
    Else
    Dim lpPoint As POINTAPI
    lpPoint.X = X
    lpPoint.Y = Y
    ClientToScreen hWnd, lpPoint
    SetCursorPos lpPoint.X, lpPoint.Y
    End If
    End Sub

    ' Clip the mouse cursor to a given rectangle.

    Sub ClipMouse(ByVal Left As Long, ByVal Top As Long, ByVal Right As Long, ByVal Bottom As Long)
    Dim lpRect As RECT
    lpRect.Left = Left
    lpRect.Top = Top
    lpRect.Right = Right
    lpRect.Bottom = Bottom
    ClipCursor lpRect
    End Sub

    ' Clip mouse to a given window.

    Sub ClipMouseToWindow(ByVal hWnd As Long)
    Dim lpPoint As POINTAPI, lpRect As RECT
    ' Retrieve the coordinates of the upper-left corner of the window.
    ClientToScreen hWnd, lpPoint
    ' Get the client screen rectangle.
    GetClientRect hWnd, lpRect
    ' Manually convert the rectangle's elements to screen coordinates.
    lpRect.Left = lpRect.Left + lpPoint.X
    lpRect.Top = lpRect.Top + lpPoint.Y
    lpRect.Right = lpRect.Right + lpPoint.X
    lpRect.Bottom = lpRect.Bottom + lpPoint.Y
    ' Enforce the clipping.
    ClipCursor lpRect
    End Sub

    ' Clear the mouse clip area.

    Sub ClearClipMouse()
    ClipCursor ByVal 0&
    End Sub

    ' Set mouse capture.

    Sub SetMouseCapture(ByVal hWnd As Long)
    If hWnd Then
    SetCapture hWnd
    Else
    ReleaseCapture
    End If
    End Sub

    ' Get the handle of the window that has the mouse capture.

    Function GetMouseCapture() As Long
    GetMouseCapture = GetCapture()
    End Function

    ' Return the handle of the window under the mouse cursor.

    Function WindowFromMouse() As Long
    Dim lpPoint As POINTAPI
    GetCursorPos lpPoint
    WindowFromMouse = WindowFromPoint(lpPoint.X, lpPoint.Y)
    End Function


    in your timer put this code

    ' Show mouse button state
    msg = "X = " & MouseX & " Y = " & MouseY & " "

    If LeftButton Then msg = msg & "LEFT "
    If MiddleButton Then msg = msg & "MIDDLE "
    If RightButton Then msg = msg & "RIGHT"
    msg = msg & "[ " & ObjectFromMouse & " ]"
    lblMouse = msg

    your interval set to 200

    Thanks
    Hisham
    Thank You, Hisham

  6. #6
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Maybe to hard to do?

    Don't bother with a timer - just subclass the form and when you get a WM_MOUSEMOVE message you will have x in th ehiword of lParam and y in the loword of lParam.

    See the ImageMapVB.zip with code on how to do this at http://www.merrioncomputing.com/Download/index.htm

    HTH,
    Duncan

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    Check out the new downloads - ImageMap.ocx is the VB control that emulates an HTML image map, EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  7. #7
    Join Date
    Aug 2001
    Posts
    17

    Re: Maybe to hard to do?

    *sigh*

    I cant get this to work for me
    I want to be able to click the button and then be able to click in windows any where i want and have it give me the windows Coords X and Y ad place them in feilds for me on the form that the button is on.
    I cat get this to work




  8. #8
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Maybe to hard to do?

    Aha - to get clicks *outside* your form you need to "Capture" the mouse.

    To do this you use the SetCapture API call. Once you have done this then every mouse message in the same thread as your application will be sent to the window that you have set the capture to.

    (There is a good example of setcapture on this site so I won't plagiarise it)

    HTH,
    Duncan

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    Check out the new downloads - ImageMap.ocx is the VB control that emulates an HTML image map, EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  9. #9
    Join Date
    Aug 2001
    Posts
    17

    Re: Maybe to hard to do?

    can you show me where it is o how to set mine up please?


  10. #10
    Join Date
    Aug 2001
    Posts
    17

    Re: Maybe to hard to do?

    No one has any advice on how to get this to work ??

    So far i cant get it to work


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

Featured