MouseGetPos

提供: AutoHotkey Wiki
移動: 案内検索

実行制御 | GUI表示 | 演算・変数 | メモリ・DLL操作 | 文字列操作 | キーボード | マウス | シェル | ウィンドウ | ウィンドウグループ
ステータスバー | コントロール | サウンド | ファイル | INIファイル | レジストリ | 環境変数 | AutoHotkey | その他 | 設定関係 | オブジェクト

MouseGetPos[編集]

マウスカーソルの位置、マウスの下のウィンドウやコントロールを取得。

MouseGetPos, [OutputVarX, OutputVarY, OutputVarWin, OutputVarControl, AltMethod]

Parameters[編集]

引数名 説明
OutputVarX/Y マウスカーソルの横座標とたて座標を格納する変数名。
OutputVarWin マウスの下のウィンドウのウィンドウハンドルを格納する変数名。
ウィンドウ関連コマンドのTitle引数に ahk_id %OutputVarWin%というようにして指定することで、そのウィンドウを指定することができる。
OutputVarControl マウスの下のコントロールのClassNNを格納する変数名。
取得した文字列は、コントロール関連コマンドのControl引数で使用できる。
AltMethod 以下の物の和を指定する。
1 OutputVarControlの取得に別の方法を使用する。
この方法では、MDIウィンドウのコントロールをうまく取得できる可能性があるが、他の目的ではうまく動作しない場合がある。
2 OutputVarControlにコントロールのClassNNではなくウィンドウハンドルを取得する。

Remarks[編集]

通常、X,Y座標はアクティブウィンドウの左上からの相対座標で取得される。
CoordMode,Mouse,Screenを実行すると、スクリーン左上からの座標で取得されるようになる。

引数が省略された部分の情報の取得は行われない。

ウィンドウやコントロールの取得に失敗した場合は、該当する変数は空になる。

AltMethodに1を指定していないと、一部のケースで正しい結果が得られない。
しかし、AltMethodに1を指定すると逆に正しい結果が得られない場合もある
以下のようにWM_NCHITTESTを使用することで、より確実に正しい結果を得られるようになる。

CoordMode,Mouse,Screen
MouseGetPos,x,y,hwnd,ctrl,3
SendMessage,0x84,0,%lp%,,ahk_id %ctrl%
If ErrorLevel = 4294967295
  MouseGetPos,,,,ctrl,2

Related[編集]

CoordMode, WinGet, SetDefaultMouseSpeed, MouseClick, MouseClickDrag, MouseMove

Example(s)[編集]

MouseGetPos, xpos, ypos 
MsgBox, The cursor is at X%xpos% Y%ypos%. 
; This example allows you to move the mouse around to see
; the title of the window currently under the cursor:
#Persistent
SetTimer, WatchCursor, 100
Return
WatchCursor:
MouseGetPos, , , id, control
WinGetTitle, title, ahk_id %id%
WinGetClass, class, ahk_id %id%
ToolTip, ahk_id %id%`nahk_class %class%`n%title%`nControl: %control%
Return