Jump to content

Recommended Posts

Posted

I want to capture the button click but I only get the canvas click.  How do I get the button click?

Global $hCanvas= GUICreate("Canvas", 500, 500)
GUISetOnEvent($GUI_EVENT_PRIMARYDOWN,"CanvasClicked")

GUISetState(@SW_SHOW)

Local $idButton = GUICtrlCreateButton("hello",0,0,100,25)
GUICtrlSetOnEvent(-1, "ButtonClicked")
 

  • Moderators
Posted

TheWizEd,

I imagine it is because the GUISetOnEvent handler takes precedence over the GUICtrlSetOnEvent one and so swallows the click before the button gets a chance to see it. You could always do something like this:

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

Opt("GUIOnEventMode", 1)

Global $hCanvas= GUICreate("Canvas", 500, 500)
GUISetOnEvent($GUI_EVENT_PRIMARYDOWN,"CanvasClicked")
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

Local $idButton = GUICtrlCreateButton("hello",0,0,100,25)

 GUISetState(@SW_SHOW)

 While 1
     Sleep(10)
 WEnd

Func CanvasClicked()
    If (GUIGetCursorInfo($hCanvas))[4] = $idButton Then
        MsgBox($MB_SYSTEMMODAL, "Clicked", "Button")
    Else
        MsgBox($MB_SYSTEMMODAL, "Clicked", "Canvas")
    EndIf
EndFunc

Func _Exit()
    Exit
EndFunc

But why do you need to detect a mouse event on the entire GUI in the first place?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted (edited)

I'm trying to see if the user can move the control.  But also want to know if the user just clicks the canvas.

Edited by TheWizEd
  • Moderators
Posted

TheWizEd,

So how will you differentiate between a click and a move?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

  • Moderators
Posted

TheWizEd,

How about a draggable border:

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <SendMessage.au3>

Global Const $SC_DRAGMOVE = 0xF012

$hGUI = GUICreate("Test", 300, 200)

Global $cLabel = GUICtrlCreateLabel("", 95, 45, 90, 40)
GUICtrlSetBkColor($cLabel, 0x00FF00)
GUICtrlSetState($cLabel, $GUI_DISABLE) ; Needs to be disabled for the button to work

$cButton = GUICtrlCreateButton("Press Me!", 100, 50, 80, 30)

GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_PRIMARYDOWN
            _ControlMove()
        Case $GUI_EVENT_PRIMARYUP
            _ControlStop()
        Case $cButton
            ConsoleWrite("Button Pressed" & @CRLF) ; Just to show it works!
    EndSwitch
WEnd

Func _ControlMove()
    ; Check if over label
    Local $aCurPos = GUIGetCursorInfo()
    If @error Then Return False
    If $aCurPos[4] = $cLabel Then
        ; Enable label to enable drag
        GUICtrlSetState($cLabel, $GUI_ENABLE)
        ; Delete button
        GUICtrlDelete($cButton)
        ; Allow label drag
        _SendMessage(GUICtrlGetHandle($cLabel), $WM_SYSCOMMAND, $SC_DRAGMOVE, 0)
    EndIf

EndFunc   ;==>_ControlMove

Func _ControlStop()
    ; Check if over label
    Local $aCurPos = GUIGetCursorInfo()
    If @error Then Return False
    If $aCurPos[4] = $cLabel Then
        ; Disable label again
        GUICtrlSetState($cLabel, $GUI_DISABLE)
        ; Recreate button in new position
        $aPos = ControlGetPos($hGUI, "", $cLabel)
        $cButton = GUICtrlCreateButton("Press Me!", $aPos[0] + 5, $aPos[1] + 5, 80, 30)
    EndIf

EndFunc   ;==>_ControlStop

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...