Sign in to follow this  
Followers 0
jfcby

Label Change Color On Mouse Click

2 posts in this topic

#1 ·  Posted (edited)

Hi,

I'm tring to get the labels to change color when I click them. I tried _IsPressed and $GUI_EVENT_PRIMARYDOWN but I can't them to work in the follwoing script.

How can the following script be modified to change the labels color when clicked?

#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>

Opt("MouseCoordMode", 0) ; 1 means screen, 0 active window
opt("GUIOnEventMode", 0)

; GLOBAL DECLARATIONS
Global $thisGuiTitle = "Label Mouse Over and Click"; Change as needed

; Don't change these
Global $prevXPos, $prevYPos,$xpos,$ypos;
Global $dll = DllOpen("user32.dll")

; CREATE GUI WITH LABELS

GUICreate($thisGuiTitle, 400, 200, "", "", $WS_THICKFRAME) ; can be resized - try it

$greenLab=GUICtrlCreateLabel ("Label 1", 10, 10,50,20,$SS_CENTER ) ; will grow when window is resized
GUICtrlSetBkColor ( -1, 0x00ff00 ) ; green

$redLab=GUICtrlCreateLabel ("Label 2", 10, 30,50,20,$SS_CENTER ) ; will grow when window is resized
GUICtrlSetBkColor ( -1, 0xff0000 ); red

$xreport=GUICtrlCreateLabel ("0", 100, 10,50,20,$SS_CENTER )
$yreport=GUICtrlCreateLabel ("0", 100, 30,50,20,$SS_CENTER )


GUISetState()

; Run the GUI until the user closes it
While 1
    $msg = GUIGetMsg()
    getMousePos() 
    Select
        Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    EndSelect
WEnd
DllClose($dll)


;==== FUNCTIONS ====

Func doMouseOvers()     
    mouseOverChangeBk($greenLab,$xpos,$ypos,0x00ff00,0xffcc33,0x0000FF)
    mouseOverChangeBk($redLab,$xpos,$ypos,0xff0000,0xffcc33,0x0000FF)

EndFunc


Func getMousePos() 
    $pos = MouseGetPos() 
    $xpos=$pos[0];
    $ypos=$pos[1];

    If $xpos == $prevXPos AND $ypos == $prevYPos Then 
        ; do nothing
    Else
        doMouseOvers()

        $prevXPos=$xpos
        $prevYPos=$ypos

        GUIctrlSetData($xreport,"Left:"&$xpos) 
        GUIctrlSetData($yreport,"Top:"&$ypos) 
    EndIf
EndFunc

Func mouseOverChangeBk($object,$xpos,$ypos,$normalColor,$overColor,$clickColor)
;~  MsgBox(0, "", "Test")
    If gotcha($object) == 1 Then 
        GUICtrlSetBkColor ( $object, $overColor )
    ElseIf gotcha($object) == 0 Then
        GUICtrlSetBkColor ( $object, $normalColor )
    ElseIf  _IsPressed("01", $dll) Then
        GUICtrlSetBkColor ( $object, $clickColor )
    EndIf
        
EndFunc ; =>mouseOverChangeImage


Func gotcha($object)
    Local $Vcompensator=29;
    Local $Hcompensator=3;
    $SBPos = ControlGetPos($thisGuiTitle, "", $object)
    Local $sbLeft=$SBPos[0]+$Hcompensator
    Local $sbTop=$SBPos[1]+$Vcompensator;
    Local $sbW=$SBPos[2];
    Local $sbH=$SBPos[3];
    Local $sbRight=($sbLeft+$sbW+2)
    Local $sbBot=($sbTop+$sbH+2) 
        If $xpos>$sbLeft AND $xpos<$sbRight AND $ypos>$sbTop AND $ypos<$sbBot Then 
            Return 1
        Else
            Return 0
        EndIf 

EndFunc

Thank you for your help,

jfcby

Edited by jfcby

Determined -- Devoted -- Delivered Make your mind up -- to seriously apply yourself -- accomplishing the desired results. **** A soft answer turneth away wrath: but grievous words stir up anger. Proverbs 15:1 KJB ****

Share this post


Link to post
Share on other sites



Labels fire a event to the form when clicked so you can respond to it like this:

; Run the GUI until the user closes it
While 1
    $msg = GUIGetMsg()
    getMousePos() 
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $redLab
            GUICtrlSetBkColor ( $redLab, 0xffff00 )
        Case $msg = $greenLab
            GUICtrlSetBkColor ( $greenLab, 0xffff00 )
    EndSelect
WEnd

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