Saturday, March 1, 2008

Determining the Cursor Position in Visual Basic

This tutorial show how to get the mouse cursor position on the screen. 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.

About this Tutorial
This tutorial is from The Game Progamming Wiki which is published under the GNU Free Documentation License 1.2.

0 comments: