LoginSignup
4
3

More than 5 years have passed since last update.

MouseGetPosコマンドで、コントロール名を正しく取得するための注意

Posted at

AutoHotKeyJpに載っている MouseGetPos コマンドのサンプルがうまく動作しなかったので、調べた結果を補完。

経緯

下記ページに書かれているサンプルコードでは、グループボックス内のコントロールのウィンドウハンドルがうまく取得できませんでした。

wrong_example.ahk
MouseGetPos,x,y,hwnd,ctrl,3
SendMessage,0x84,0,%lp%,,ahk_id %ctrl%
If ErrorLevel=4294967295
  MouseGetPos,,,,ctrl,2

解決策

3行名に書かれている %lp% とはなんぞや?と思っていたのですが、上の行にコードが抜けていたようです。

lp := (y << 16) | x

成功したコード

それを生かして、マウスカーソル上の各種情報を取得する関数を作成したのでそこまで載せておきます。

GetInfoAtMousePosEx.ahk
; マウスカーソル位置の各種情報を取得(座標=アクティブウィンドウからの相対位置)
GetInfoAtMousePosEx(ByRef X, ByRef Y, ByRef WinHWND, ByRef ControlHWND, ByRef ControlClassNN, ByRef WinTitle, ByRef ControlText, ByRef ClassName)
{
  WM_NCHITTEST=0x84
  ERROR=0xFFFFFFFF

  ; Get ControlHWND
  MouseGetPos,X,Y,WinHWND,ControlHWND,3
  lParam := (Y << 16) | X
  SendMessage,%WM_NCHITTEST%,0,%lParam%,, ahk_id %ControlHWND%
  If ErrorLevel=%ERROR%
    MouseGetPos,,,,ControlHWND,2

  ; Get ControlClassNN
  MouseGetPos,,,,ControlClassNN

  ; Get othe info.
  WinGetClass, ClassName, ahk_id %WinHWND%
  WinGetTitle, WinTitle, ahk_id %WinHWND%
  ControlGetText, ControlText, , ahk_id %ControlHWND%

  return True
}

AutoHotKeyJpは、日本語で AutoHotKey の情報が書かれているサイトで大変重宝しております。感謝。

参考

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3