マウス状態の取得

<マウスユーティリティソフトを作ろう>

宣言(準備としてまず次のMciSendStringの宣言を追加します)

(Private) Type POINTAPI'マウス取得用
x As Long
y As Long
End Type

(Private) Declare Function GetAsyncKeyState% Lib "user32" (ByVal vKey As Long)'マウスのボタンの状態取得用
(Private) Declare Function GetCursorPos& Lib "user32" (Point As POINTAPI)'マウス位置取得用

※Privateは必要に応じて削除またはPublicに変更

引 数 説 明


Button

Long

vbKeyRButton(右ボタン状態)またはvbKeyLButton(左ボタン状態)。戻り値が0未満ならボタンが押されている状態。

Point POINTAPI構造体 POINTAPI構造体を指定。要素xにマウスのx位置(単位:ピクセル)要素yにマウスのy位置が格納される。


サ ン プ ル プ ロ グ ラ ム

まず、VisualBasicを起動してプロジェクトを新しく作成してください。そしてフォームに大きめのラベルを張りつけて、次のコードを貼り付けてください。実行するとラベルにマウスの状態を表示します。

動作確認:Visual Basic 5.0(Visual Basic 4.0でも問題なく動作するはずです),Windows98

Private Type POINTAPI
x As Long
y As Long
End Type

Private Declare Function GetAsyncKeyState% Lib "user32"
(ByVal vKey As Long)'マウスのボタンの状態取得用
Private Declare Function GetCursorPos& Lib "user32" (Point As POINTAPI)'マウス位置取得用

Private Sub Form_Load()
Dim i As Integer'変数の宣言
Dim LButton As String
Dim RButton As String

Dim Point As POINTAPI
Form1.Show

Do
DoEvents
If GetAsyncKeyState(vbKeyLButton) < 0 Then'マウス左ボタン状態取得
LButton = "押されています"
Else
LButton = "押されていません"
End If


If GetAsyncKeyState(vbKeyRButton) < 0 Then'マウス右ボタン状態取得
RButton = "押されています"
Else
RButton = "押されていません"
End If

GetCursorPos Point'マウス位置取得
Label1.Caption = "マウスx方向位置:" + Str(Point.x) + _
Chr(13) + "マウスy方向位置:" + Str(Point.y) + _
Chr(13) + "マウス左ボタン状態:" + LButton + _
Chr(13) + "マウス右ボタン状態:" + RButton
Loop Until i <> 0
End
End Sub

Private Sub Form_Unload(Cancel As Integer)
End

End Sub

トップに戻る
Visual Basicワンポイントテクニック目次に戻る

Gポイントポイ活 Amazon Yahoo 楽天

無料ホームページ 楽天モバイル[UNLIMITが今なら1円] 海外格安航空券 海外旅行保険が無料!