Sign in to follow this  
Followers 0
sshrum

PIC on GUI...how do I tell if it is clicked on

11 posts in this topic

#1 ·  Posted (edited)

I've created a GUI and on it I've placed a number of 'buttons' (actually, they are jpg PICTURE objects).

Is there a way to detect when they are clicked on?

I'm using $msg = GUIGetMsg() but it doesn't seem to be returning anything (I have a label object on my GUI that displays $msg and the results of MouseGetPos()). I really don't want to have to create some sort of mapping scheme (ie get the coordinates on evey mouseclick), I'd rather the control ID just get sent back and I can monitor for that.

TIA

Edited by sshrum

Sean Shrum :: http://www.shrum.net

All my published AU3-based apps and utilities

'Make it idiot-proof, and someone will make a better idiot'

 

Share this post


Link to post
Share on other sites



More than you asked for, didn't feel like trimming it down. Just added the $Pic.

#include <GUIConstants.au3>

Opt("MustDeclareVars", 1)

Global $Button, $Label, $Pic

_Main()

Func _Main()

    GUICreate("Blash", 716, 561, 155, 134, BitOR($WS_MAXIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_OVERLAPPEDWINDOW, $WS_TILEDWINDOW, $WS_TABSTOP))

    $Button = GUICtrlCreateButton("Button Test", 10, 10, 89, 41, $BS_NOTIFY)
    $Label = GUICtrlCreateLabel("Label Test", 10, 60, 120, 22, BitOR($SS_NOTIFY, $SS_SUNKEN, $SS_CENTER))
    $Pic = GUICtrlCreatePic(@SystemDir & "\oobe\images\mslogo.jpg", 10, 90, 200, 50)
    GUISetState()
    
    GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
    
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc   ;==>_Main

Func Button_Click(ByRef $ctrl_id)
;~  _DebugPrint("Button_Click")
EndFunc   ;==>Button_Click

Func Button_DblClick(ByRef $ctrl_id)
;~  _DebugPrint("Button_DblClick")
    MsgBox(0, "Test", "Button_DblClick")
EndFunc   ;==>Button_DblClick

Func Label_Click(ByRef $ctrl_id)
    _DebugPrint("Label_Click")
EndFunc   ;==>Label_Click

Func Label_DblClick(ByRef $ctrl_id)
;~  _DebugPrint("Label_DblClick")
    MsgBox(0, "Test", "Label_DblClick")
EndFunc   ;==>Label_DblClick

Func Pic_Click(ByRef $ctrl_id)
    _DebugPrint("Pic_Click")
EndFunc   ;==>Pic_Click

Func Pic_DblClick(ByRef $ctrl_id)
;~  _DebugPrint("Pic_DblClick")
    MsgBox(0, "Test", "Pic_DblClick")
EndFunc   ;==>Pic_DblClick

Func _LoWord($x)
    Return BitAND($x, 0xFFFF)
EndFunc   ;==>_LoWord

Func _HiWord($x)
    Return BitShift($x, 16)
EndFunc   ;==>_HiWord

Func WM_COMMAND($hWnd, $msg, $wParam, $lParam)
    Local Const $BN_CLICKED = 0;
    Local Const $BN_PAINT = 1;
    Local Const $BN_HILITE = 2;
    Local Const $BN_PUSHED = $BN_HILITE;
    Local Const $BN_UNHILITE = 3;
    Local Const $BN_UNPUSHED = $BN_UNHILITE;
    Local Const $BN_DISABLE = 4;
    Local Const $BN_DOUBLECLICKED = 5;
    Local Const $BN_DBLCLK = $BN_DOUBLECLICKED;
    Local Const $BN_SETFOCUS = 6;
    Local Const $BN_KILLFOCUS = 7;

    Local Const $STN_CLICKED = 0
    Local Const $STN_DBLCLK = 1
    Local Const $STN_DISABLE = 3
    Local Const $STN_ENABLE = 2

    Local $nNotifyCode = _HiWord($wParam)
    Local $nID = _LoWord($wParam)
    Local $hCtrl = $lParam
    
    If $nID <> 2 Then
        Switch $nID
            Case $Button
                Switch $nNotifyCode
                    Case $BN_CLICKED
                        _DebugPrint("$BN_CLICKED: " & @LF & "GUIHWnd" & @TAB & ":" & $hWnd & @LF & _
                                "MsgID" & @TAB & ":" & $msg & @LF & _
                                "wParam" & @TAB & ":" & $wParam & @LF & _
                                "lParam" & @TAB & ":" & $lParam & @LF & @LF & _
                                "WM_COMMAND - Infos:" & @LF & _
                                "-----------------------------" & @LF & _
                                "Code" & @TAB & ":" & $nNotifyCode & @LF & _
                                "CtrlID" & @TAB & ":" & $nID & @LF & _
                                "CtrlHWnd" & @TAB & ":" & $hCtrl)
                        Button_Click($nID)
                    Case $BN_PAINT
                        _DebugPrint("$BN_PAINT")
                    Case $BN_PUSHED, $BN_HILITE
                        _DebugPrint("$BN_PUSHED, $BN_HILITE")
                    Case $BN_UNPUSHED, $BN_UNHILITE
                        _DebugPrint("$BN_UNPUSHED")
                    Case $BN_DISABLE
                        _DebugPrint("$BN_DISABLE")
                    Case $BN_DBLCLK, $BN_DOUBLECLICKED
;~                       _DebugPrint("$BN_DBLCLK, $BN_DOUBLECLICKED")
                        _DebugPrint("$BN_CLICKED: " & @LF & "GUIHWnd" & @TAB & ":" & $hWnd & @LF & _
                                "MsgID" & @TAB & ":" & $msg & @LF & _
                                "wParam" & @TAB & ":" & $wParam & @LF & _
                                "lParam" & @TAB & ":" & $lParam & @LF & @LF & _
                                "WM_COMMAND - Infos:" & @LF & _
                                "-----------------------------" & @LF & _
                                "Code" & @TAB & ":" & $nNotifyCode & @LF & _
                                "CtrlID" & @TAB & ":" & $nID & @LF & _
                                "CtrlHWnd" & @TAB & ":" & $hCtrl)
                        Button_DblClick($nID)
                    Case $BN_SETFOCUS
                        _DebugPrint("$BN_SETFOCUS")
                    Case $BN_KILLFOCUS
                        _DebugPrint("$BN_KILLFOCUS")
                EndSwitch
            Case $Label
                Switch $nNotifyCode
                    Case $STN_CLICKED
                        Label_Click($nID)
                    Case $STN_DISABLE
                        _DebugPrint("$STN_DISABLE")
                    Case $STN_ENABLE
                        _DebugPrint("$STN_ENABLE")
                    Case $STN_DBLCLK
                        Label_DblClick($nID)
                EndSwitch
            Case $Pic
                Switch $nNotifyCode
                    Case $STN_CLICKED
                        Pic_Click($nID)
                    Case $STN_DISABLE
                        _DebugPrint("$STN_DISABLE")
                    Case $STN_ENABLE
                        _DebugPrint("$STN_ENABLE")
                    Case $STN_DBLCLK
                        Pic_DblClick($nID)
                EndSwitch
        EndSwitch
    EndIf
    ; Proceed the default Autoit3 internal message commands.
    ; You also can complete let the line out.
    ; !!! But only 'Return' (without any value) will not proceed
    ; the default Autoit3-message in the future !!!
;~     Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

Func _DebugPrint($s_text)
    $s_text = StringReplace($s_text, @LF, @LF & "-->")
    ConsoleWrite( _
            "!===========================================================" & @LF & _
            "+===========================================================" & @LF & _
            "-->" & $s_text & @LF & _
            "+===========================================================" & @LF)
EndFunc   ;==>_DebugPrint

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

Ewwww! Someone made a stinky.

That sorta works but that's quite a bit of code (and a bunch of additional functionality) than I was looking for.

I was hoping there was a way to use GUIGetMsg for clicks on Pics like clicks on Buttons. Then all I do is a Select statement that checks for the CtrlID returned by GUIGetMsg.

If not, is there a way to make a transparent button over a Pic so I could capture the button click?


Sean Shrum :: http://www.shrum.net

All my published AU3-based apps and utilities

'Make it idiot-proof, and someone will make a better idiot'

 

Share this post


Link to post
Share on other sites

Ewwww! Someone made a stinky.

That sorta works but that's quite a bit of code (and a bunch of additional functionality) than I was looking for.

I was hoping there was a way to use GUIGetMsg for clicks on Pics like clicks on Buttons. Then all I do is a Select statement that checks for the CtrlID returned by GUIGetMsg.

If not, is there a way to make a transparent button over a Pic so I could capture the button click?

Not a stinky, just do a select like you want in the click function, pass the ctrlid clicked in and do a select.


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

This is odd, when I use the AutoIT Window Info tool over my GUI with picture controls (not your gui), I'm not getting any info back when I move the cursor over the picture control...I just get the main GUI window info. Is that normal?

Edited by sshrum

Sean Shrum :: http://www.shrum.net

All my published AU3-based apps and utilities

'Make it idiot-proof, and someone will make a better idiot'

 

Share this post


Link to post
Share on other sites

This registers when my picture is clicked for me.. short and sweet.

#include <GUIConstants.au3>

$GUI = GUICreate("Click Pick", 300, 200, -1, -1)
$PIC = GUICtrlCreatePic (@WindowsDir & "\Coffee Bean.bmp", 10, 10, 77, 72)
GUISetState(@SW_SHOW, $GUI)

While 1
    $msg = GUIgetMsg()
    Select
        case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $PIC
            MsgBox(0, "", "$PIC Clicked", 2)
    Endselect
Wend

Cheers

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

This registers when my picture is clicked for me.. short and sweet.

I agree. This should work...however on my PC (Vista Ult + AutoIT 3.2.4.9) when I run that code it immediately indicates that the pic is clicked (msgbox) as soon as I launch...and it just keeps popping up that the pic is been clicked (even though I have yet to click it after I clear the msgbox).

AutoIT bug?

P.S. Same result if I run with the current beta.

P.S.S. Seems the graphic wasn't loading...changed it to something local to my system and the image is now displayed....msgbox stopped popping up. Seems to be working now.

I need to go back over my code...I may post it if I can't root out the cause but it's pretty simple. Thanks for the example, at least I know that this should work.

Edited by sshrum

Sean Shrum :: http://www.shrum.net

All my published AU3-based apps and utilities

'Make it idiot-proof, and someone will make a better idiot'

 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Ahhh...found it (or rather what the problem is).

Create a GUI and in it place a picture that fills the whole GUI (this is the background pic). Now place another picture (smaller in size like a button) after that which will be the picture to click. When you click on the "button" picture, AutoIT is reporting back that it is the BACKGROUND, not the foreground "button" picture that is being clicked on.

Is this normal or is this a bug (seems buggy to me). I'd assume that stacked picture controls would been seen in the order defined (last to first....pyramid style stacking).

Edited by sshrum

Sean Shrum :: http://www.shrum.net

All my published AU3-based apps and utilities

'Make it idiot-proof, and someone will make a better idiot'

 

Share this post


Link to post
Share on other sites

Ahhh...found it (or rather what the problem is).

Create a GUI and in it place a picture that fills the whole GUI (this is the background pic). Now place another picture (smaller in size like a button) after that which will be the picture to click. When you click on the "button" picture, AutoIT is reporting back that it is the BACKGROUND, not the foreground "button" picture that is being clicked on.

Is this normal or is this a bug (seems buggy to me). I'd assume that stacked picture controls would been seen in the order defined (last to first....pyramid style stacking).

set the state of the background pic to $GUI_DISABLE

if that don't work the way your trying this should:

#include <GUIConstants.au3>

Opt("MustDeclareVars", 1)

Global $Pic[1] = [0], $main_GUI

_Main()

Func _Main()
    Local $y = 10
    $main_GUI = GUICreate("Blash", 716, 561, 155, 134, BitOR($WS_MAXIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_OVERLAPPEDWINDOW, $WS_TILEDWINDOW, $WS_TABSTOP))
    Local $bkg_pic = GUICtrlCreatePic(@SystemDir & "\oobe\images\mslogo.jpg", 0, 0, 716, 561)
    GUICtrlSetState($bkg_pic, $GUI_DISABLE)
    For $x = 1 To 5
        ReDim $Pic[UBound($Pic) + 1]
        $Pic[$x] = GUICtrlCreatePic(@SystemDir & "\oobe\images\mslogo.jpg", 10, $y, 200, 50)
        $y += 60
        $Pic[0] += 1
    Next
    GUISetState()
    
    GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
    
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc   ;==>_Main

Func Pic_Click(ByRef $ctrl_id)
    _DebugPrint("Pic_Click: " & $ctrl_id)
EndFunc   ;==>Pic_Click

Func Pic_DblClick(ByRef $ctrl_id)
;~  _DebugPrint("Pic_DblClick: " & $ctrl_id)
    MsgBox(0, "Test", "Pic_DblClick: " & $ctrl_id)
EndFunc   ;==>Pic_DblClick

Func _LoWord($x)
    Return BitAND($x, 0xFFFF)
EndFunc   ;==>_LoWord

Func _HiWord($x)
    Return BitShift($x, 16)
EndFunc   ;==>_HiWord

Func WM_COMMAND($hWnd, $msg, $wParam, $lParam)
    Local Const $STN_CLICKED = 0
    Local Const $STN_DBLCLK = 1
    Local Const $STN_DISABLE = 3
    Local Const $STN_ENABLE = 2

    Local $nNotifyCode = _HiWord($wParam)
    Local $nID = _LoWord($wParam)
    Local $hCtrl = $lParam
    
    For $x = 1 To $Pic[0]
        If $Pic[$x] = $nID Then
            Switch $nNotifyCode
                Case $STN_CLICKED
                    Pic_Click($nID)
                Case $STN_DISABLE
                    _DebugPrint("$STN_DISABLE")
                Case $STN_ENABLE
                    _DebugPrint("$STN_ENABLE")
                Case $STN_DBLCLK
                    Pic_DblClick($nID)
            EndSwitch
            ExitLoop
        EndIf
    Next
    ; Proceed the default Autoit3 internal message commands.
    ; You also can complete let the line out.
    ; !!! But only 'Return' (without any value) will not proceed
    ; the default Autoit3-message in the future !!!
;~     Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

Func _DebugPrint($s_text)
    $s_text = StringReplace($s_text, @LF, @LF & "-->")
    ConsoleWrite( _
            "!===========================================================" & @LF & _
            "+===========================================================" & @LF & _
            "-->" & $s_text & @LF & _
            "+===========================================================" & @LF)
EndFunc   ;==>_DebugPrint

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

More than you asked for, didn't feel like trimming it down. Just added the $Pic.

Excellent code, thank you very much.

However I had to add this to make it work:

Global Const $WM_COMMAND = 0x0111

Thank you again,

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  
Followers 0