AutoHotkey的 - 当被点击窗口中的某个按钮触发脚本(Autohotkey - Trigge

2019-11-01 16:38发布

我知道实际按钮的手柄。 我想检查是否被点击的按钮,如果是的话,将引发一个AutoHotkey的脚本。

提前致谢!

Answer 1:

你是对的。 你可以用它来代替ImgSearch。

ControlGetPos, x, y, w, h, button1, ahk_class CalcFrame
MsgBox, %x% %y% %w% %h%
return

所以,你会在每次鼠标点击(只有当目标窗口的标题是活动的)后运行ControlGetPos,然后比较按钮点击区域实际鼠标坐标。

下面是计算器一些代码:

#SingleInstance Force
#Persistent

#IfWinActive, ahk_class CalcFrame
    ~LButton::
    MouseGetPos, MouseX, MouseY
    ControlGetPos, ButtonX, ButtonY, ButtonW, ButtonH, button1, ahk_class CalcFrame
    ButtonX2:=ButtonX + ButtonW
    ButtonY2:=ButtonY + ButtonH
    if MouseX between %ButtonX% and %ButtonX2%
    {
        if MouseY between %ButtonY% and %ButtonY2%
        {
            MsgBox, You pressed the MC button
        }
    }
    Return
#IfWinActive


Answer 2:

您是否尝试过使用ImgSearch为“动态”寻找按钮的XY坐标,如果再做一个(MouseX => ImageX和MouseX = <ImageX的+ ImageWidth)?

伪代码(未测试):

Settimer, FindButton, 1000
Settitlematchmode, 2
Return

FindButton:
IfWinActive, YourAppWindowTitle
    ImageSearch, ImageX, ImageY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\ButtonImage.bmp
Return

#IfWinActive, YourAppWindowTitle
~LButton::
MouseGetPos, MouseX, MouseY
if (MouseX => ImageX and MouseX =< ImageX + ImageWidth)
{
    if (MouseY => ImageY and MouseY =< ImageY + ImageHeight)
    {
        Run your code here
    }
}
Return
#IfWinActive


文章来源: Autohotkey - Trigger script when a certain button was clicked in a window