VB:Tutorials:WINAPI:Determining the Cursor Position

From GPWiki
Jump to: navigation, search

Determining the position of the cursor is vitally important if you intend to enable mouse support in your game. There is a simple API call that will allow you do this with ease.

Private Declare Function GetCursorPos Lib "user32" (lpPoint As PointAPI) As Long

Type PointAPI

   x As Long
   y As Long

End Type

Ok, it's a little more than just an API call, you also need to declare a new type, PointAPI, to go along with it. If you pass a variable of type PointAPI to the GetCursorPos function, it will fill the variable with the coordinates of the cursor. You can then retrieve this data at your leisure!

Calling this function during a mouse click or double click (ie. the Click and DblClick events of a form) is quite useful, you can then check if the cursor was over a clickable element of your game, like a button, and respond accordingly.