Jump to content
Sign in to follow this  
Frogscape

If mouse hover over a specific button then click

Recommended Posts

Frogscape

I am trying to get a left mouse to click when ever the mouse pointer passes over (or hovers over) a specific button. The idea is that when the user slowly moves the mouse vertically from the top to bottom of the message box the pointer passes over button 1 (called detect pointer) the mouse automatically clicks button 2 (called freeze). The problem is that I need help detecting when the mouse pointer is over button 1. How do I write this so the program understands this and send out a click?

Thanks

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 230, 297, 303, 543)
$Button1 = GUICtrlCreateButton("detect pointer", 80, 100, 75, 25, $WS_GROUP)
$Button2 = GUICtrlCreateButton("FREEZE", 80, 128, 75, 25, $WS_GROUP)

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

$var = [CLASS:Button; INSTANCE:1]   ; error: how to define when mouse is over this button is the problem

If $var > 1 Then ;If mouse hovers over button 1 then click left mouse
   MouseClick("left") ; click once on left mouse
   Sleep(3500) ;Pause to let user view action
Else
EndIf

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd
post-46358-12685345061235_thumb.gif

Share this post


Link to post
Share on other sites
Yashied

#Include <GUIConstantsEx.au3>
#Include <WinAPI.au3>

Global $Over = False

$Form1 = GUICreate('Form1', 230, 297, 303, 543)
$Button1 = GUICtrlCreateButton('detect pointer', 80, 100, 75, 25)
$Button2 = GUICtrlCreateButton('FREEZE', 80, 128, 75, 25)
GUISetState(@SW_SHOW)

$hWnd = ControlGetHandle(WinGetHandle('Form1'), '', '[CLASS:Button; INSTANCE:1]')

While 1
    $tPoint = _WinAPI_GetMousePos()
    If _WinAPI_WindowFromPoint($tPoint) = $hWnd Then
        If Not $Over Then
            ; Do something
            ConsoleWrite('Over' & @CR)
            $Over = 1
        EndIf
    Else
        If $Over Then
            ; Do something
            ConsoleWrite('Lost' & @CR)
            $Over = 0
        EndIf
    EndIf
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

But if it's AutoIt window, you need to use GUIGetCursorInfo().

Share this post


Link to post
Share on other sites
Frogscape

#Include <GUIConstantsEx.au3>
#Include <WinAPI.au3>

Global $Over = False

$Form1 = GUICreate('Form1', 230, 297, 303, 543)
$Button1 = GUICtrlCreateButton('detect pointer', 80, 100, 75, 25)
$Button2 = GUICtrlCreateButton('FREEZE', 80, 128, 75, 25)
GUISetState(@SW_SHOW)

$hWnd = ControlGetHandle(WinGetHandle('Form1'), '', '[CLASS:Button; INSTANCE:1]')

While 1
    $tPoint = _WinAPI_GetMousePos()
    If _WinAPI_WindowFromPoint($tPoint) = $hWnd Then
        If Not $Over Then
            ; Do something
            ConsoleWrite('Over' & @CR)
            $Over = 1
        EndIf
    Else
        If $Over Then
            ; Do something
            ConsoleWrite('Lost' & @CR)
            $Over = 0
        EndIf
    EndIf
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

But if it's AutoIt window, you need to use GUIGetCursorInfo().

Thank you for the quick response and taking the time to write that awsome code. It work too perfectly! :mellow: I also tried GUIGetCursorInfo(), as you suggested, and that also works too. Thank you. Edited by Frogscape

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×