January 1, 2005

WinTask - MousePos

'
' MousePos - Gives the mouse position on the screen.
'
' Author: Joe Strazzere
'

'It calls the Windows API GetCursorPos
'External, structure, allocate, peekinteger, pokeinteger are used.

'When you call Windows API functions, data are often written in
'structures (memory zones). A structure can contain various data (integer,
'string, etc ...). The only way to access those data through WinTask is to use
'System functions Read/Write in memory.

'Windows API GetCursorPos function gives the mouse cursor position on screen. It needs as parameter
'an address within a structure "point". Such a structure is composed of
' 2 integers of 4 bytes each (LONG) with consecutive addresses. At call,
'first one gives X coordinates, second one Y coordinates. Return code
'is 0 if not OK.

'Define memory address
dim addx as unsigned
dim addy as unsigned


'allocate memory address starting at addx (X position address)
addx=allocate(8)

'allocate memory address (Y position address)
addy=addx+4

'Call function through a loop
Repeat
    'address for structure addx is a parameter for GetCursorPos
    a=External("user32.dll","GetCursorPos",addx)
    If a <>0 Then
        'Read memory at address addx on 4 bytes (x position)
        pos_x=PeekInteger(addx,4)
        'Read memory at addy address on 4 bytes (y position)
        pos_y=PeekInteger(addy,4)
        'Display result
        mes$="Cursor X is at : "+str$(pos_x)+" Cursor Y is at : "+str$(pos_y)
        msgframe(mes$,1)
        pause 1
    EndIf
Until i=30
'the function is active for 100 seconds