戻る



マウスカーソルの位置を取得する方法(API関数GetCursorPosの使用例)
動作環境 Windows95/98/2000/XP 開発環境 Visual Basic 6.0 (+SP5)
説明 API関数GetCursorPosでマウスカーソルの座標を取得します。
用意するもの:標準フォーム(Form1)、タイマー(Timer1)、ラベル(Label1)


'変数の明示的な宣言を強制します。

Option Explicit

'マウスカーソルの位置がスクリーン座標で格納される構造体
Private Type POINTAPI
x As Long
y As Long
End Type

'API関数 GetCursorPos の使用を宣言します。
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

'変数を宣言する
Dim ret As Long
Dim myCurPos As POINTAPI

Private Sub Form_Load()

Form1.Caption = "マウスカーソルの位置を取得する方法(API関数GetCursorPosの使用例)"

Label1.Caption = ""

Timer1.Interval = 100

Timer1.Enabled = True

End Sub

Private Sub Timer1_Timer()

ret = GetCursorPos(myCurPos)

Label1.Caption = "X座標 " & myCurPos.x & " : " & "Y座標 " & myCurPos.y

End Sub