Announcement

Collapse
No announcement yet.

windows APIs to control mouse

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • windows APIs to control mouse

    Can someone post examples of powerbasic code that utilize windows APIs for the following:

    - retrieve the position of the mouse (presumably x and y coordinates) within the active window

    - move the mouse to specified x and y coordinates in the active window

    - perform a mouse left-click at the mouse's current position (he left click is sent to the active window's application)

  • #2
    >retrieve the position of the mouse (presumably x and y coordinates) within the active window

    GetCursorPos () + ScreenToClient()

    > move the mouse to specified x and y coordinates in the active window
    ClientToScreen() + SetCursorPos()

    >perform a mouse left-click at the mouse's current position (he left click is sent to the active window's application

    mouse_event()

    Should be examples of all here if you search on these functions.

    MCM
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Do a search for Mouse and Mouse movement and you will find the following.
      (Mine is slightly modified, but should show the basics)
      Sorry I do not remember off the top of my head where I found this, but should piece together what it takes to find mouse location and move a mouse to a location.
      Code:
          %tmr = 250 ' 0.25 sec
          %ii_min = 10 ' 0.5 sec
          %ii_max = 40 ' 2 sec
          %ii_ave = 20 ' 1 sec
      FUNCTION LeftButtonPressed() AS LONG
          ON ERROR GOTO ErrHandler
          DIM ButtonState AS LONG
          ButtonState = (GetAsyncKeyState(%vbKeyLButton) AND &H8000)
          SELECT CASE ButtonState
              CASE %False
                  ButtonState = %False
              CASE ELSE
                  ButtonState = %True
          END SELECT
          FUNCTION = ButtonState
          EXIT FUNCTION
      ErrHandler:
          StartTrace
          ErrorCheck ERR
          EndTrace
      END FUNCTION
      
      FUNCTION RightButtonPressed() AS LONG
          ON ERROR GOTO ErrHandler
          DIM ButtonState AS LONG
          ButtonState = (GetAsyncKeyState(%vbKeyRButton) AND &H8000)
          SELECT CASE ButtonState
              CASE %False
                  ButtonState = %False
              CASE ELSE
                  ButtonState = %True
          END SELECT
          FUNCTION = ButtonState
          EXIT FUNCTION
      ErrHandler:
          StartTrace
          ErrorCheck ERR
          EndTrace
      END FUNCTION
      
      FUNCTION MiddleButton() AS LONG
          ON ERROR GOTO ErrHandler
          DIM ButtonState AS LONG
          ButtonState = (GetAsyncKeyState(%vbKeyMButton) AND &H8000)
          SELECT CASE ButtonState
              CASE %False
                  ButtonState = %False
              CASE ELSE
                  ButtonState = %True
          END SELECT
          FUNCTION = ButtonState
          EXIT FUNCTION
      ErrHandler:
          StartTrace
          ErrorCheck ERR
          EndTrace
      END FUNCTION
      
      SUB MoveMouseToControl (hWnd AS LONG)
          ON ERROR GOTO ErrHandler
          EnableWindow GetParent(hWnd), %False
          LOCAL rc AS RECT, pt1 AS POINTAPI, pt2 AS PointApi, MovingPt AS PointApi
          LOCAL i AS LONG, ii AS LONG, dx AS LONG, dy AS LONG
          LOCAL p AS PointApi
          GetWindowRect hWnd, rc              'Get position of control
          GetCursorPos pt1                    'Get current position
      '    dx = 0.5 * (rc.nLeft + rc.nRight) - pt1.x                   'Get distance in x to go
      '    dy = 0.5 * (rc.nTop + rc.nBottom) - pt1.y                   'Get distance in y to go
          p.x = rc.nLeft
          p.y = rc.nTop
          ScreenToClient HwndMain, p
      '    MSGBOX STR$(p.x) + $CR + STR$(p.y)
          dx = 0.5 * (rc.nLeft + rc.nRight) - pt1.x                   'Get distance in x to go
          dy = 0.5 * (rc.nTop + rc.nBottom) - pt1.y                   'Get distance in y to go
      'msgbox str$(dy)
      '    dx = dx - (30)
      '    dy = dy - (85)
      'msgbox str$(dy)
      'MSGBOX "Top = " + STR$(rc.nTop) + $CR + "Bottom = " + STR$(rc.nBottom) + $CR + "From = " + STR$(pt1.y) + $cr + "Distance = " + str$(dy)
          ii = MAX(GetSystemMetrics(%SM_CXSCREEN) / %ii_ave / dx, _
          GetSystemMetrics(%SM_CYSCREEN) / %ii_ave / dy)
      'MSGBOX "Max = " + STR$(ii)
          ii = MIN(ii, %ii_max)
      'MSGBOX "Min = " + STR$(ii)
          ii = MAX(ii, %ii_min)
      'MSGBOX "Max = " + STR$(ii)
          FOR i = 1 TO ii
              pt2.x = pt1.x + i * dx / ii
              pt2.y = pt1.y + i * dy / ii
      'MSGBOX STR$(pt1.x) + "," + STR$(pt1.y) + $CR + STR$(pt2.x) + "," + STR$(pt2.y) + $CR + STR$(dx) + "," + STR$(dy) +$CR + STR$(ii)
              SetCursorPos pt2.x, pt2.y
      '        SLEEP 40
              SLEEP 100
          NEXT
      
      '    SendMessage hwnd, %BM_SETSTATE, 1, 0: SLEEP %tmr
      '    PostMessage hWnd, %BM_SETSTATE, 0, 0
      '    PostMessage hWnd, %BM_CLICK, 0, 0
      '    FOR i = ii TO 0 STEP -1
      '        pt2.x = pt1.x + i * dx / ii
      '        pt2.y = pt1.y + i * dy / ii
      '        SetCursorPos pt2.x, pt2.y
      '        SLEEP 40
      '    NEXT
      '    SetCursorPos pt1.x, pt1.y
          EnableWindow GetParent(hWnd), %True
          EXIT SUB
      ErrHandler:
          StartTrace
          ErrorCheck ERR
          EndTrace
      END SUB
      
      FUNCTION IsMouseOverControl(hWnd AS LONG)AS LONG
          ON ERROR GOTO ErrHandler
           LOCAL p AS PointApi
           LOCAL R AS Rect
           GetCursorPos(p)
           GetWindowRect hWnd, R
           SELECT CASE p.x
                CASE R.nLeft TO R.nRight
                     SELECT CASE p.y
                          CASE R.nTop TO R.nBottom
                               FUNCTION = %TRUE
                          CASE ELSE
                               FUNCTION = %FALSE
                     END SELECT
                CASE ELSE
                     FUNCTION = %FALSE
           END SELECT
          EXIT FUNCTION
      ErrHandler:
          StartTrace
          ErrorCheck ERR
          EndTrace
      END FUNCTION
      Take out the error handling routines to be functional
      Engineer's Motto: If it aint broke take it apart and fix it

      "If at 1st you don't succeed... call it version 1.0"

      "Half of Programming is coding"....."The other 90% is DEBUGGING"

      "Document my code????" .... "WHYYY??? do you think they call it CODE? "

      Comment


      • #4
        Thanks for your help.

        For future reference, here is the sample code I made for getting the mouse coordinates, moving mouse to new coordinates, and performing left click.

        The mouse coordinates are all absolute screen coordinates. This does not convert between screen coordinates and active window coordinates (it does not utilize ScreenToClient and ClientToScreen).

        Code:
        #COMPILE EXE
        #DIM ALL
        #INCLUDE "WIN32API.INC"
        
        FUNCTION PBMAIN () AS LONG
        
        LOCAL pt AS POINTAPI
        GetCursorPos pt              ' grab the mouse position on screen, and
        MSGBOX STR$(pt.x)+$CRLF+STR$(pt.y) ' display position
        
        pt.x=487 ' set new mouse coordinate x
        pt.y=122 ' set new mouse coordinate y
        
        SetCursorPos pt.x,pt.y ' move mouse to new coordinates
        
        SLEEP 1500
        
        mouse_event %MOUSEEVENTF_LEFTDOWN   ,0,0,0,0 ' left mouse button down
        
        SLEEP 200
        
        mouse_event %MOUSEEVENTF_LEFTUP   ,0,0,0,0 ' left mouse button up
        
        END FUNCTION

        Comment


        • #5
          Scott,

          As with keyboard input, you can also use SendInput (the API command) for mouse input. Use it with input type %INPUT_MOUSE.

          Kind regards
          Eddy

          Comment

          Working...
          X