Jump to content

GUICtrlSetImage Help


Recommended Posts

Hi all

We are currently building a menu system that features quite a lot of image based checkboxes.

When a checkbox is clicked our current code sets a variable to either 0 or 1 and then changes the image accordingly (see posted example).

This works perfectly well but is obviously a lot of coding for each image, is there anyway to make the code universal so it can change any variable and image from a single OnEvent call?

Func HealthCheck_P1_Report()
If $HealthCheck_P1_ReportSlideVar = 0 Then
  GUICtrlSetImage($HealthCheck_P1_ReportSlide, "./images/switchon.bmp")
  $HealthCheck_P1_ReportSlideVar = 1
Else
  GUICtrlSetImage($HealthCheck_P1_ReportSlide, "./images/switchoff.bmp")
  $HealthCheck_P1_ReportSlideVar = 0
EndIf
EndFunc
Link to comment
Share on other sites

Post part where you declare picture controls and where you declare/use variables used in these functions.

It's less work to accomodate existing script than write it from scratch.

Ideally post small reproducing script with two functions only but also with other stuff like GUI/controls creation - fully working repro script.

Link to comment
Share on other sites

Ok i have put this together.

It features 4 of the buttons that we are hoping to make the universal code for.

The images attached arent the proper images but they convey the point.

Let me know if you need anything else

PS - The image path is relative to the script so just put them in a folder together and it should work fine.

** UPDATE **

Also the variable for each option needs to remain in place as that will be read later in the script to determine the action to take

exit.bmp

HealthCheck_Page1.au3

switchoff.bmp

switchon.bmp

Edited by DOTCOMmunications
Link to comment
Share on other sites

Nice example.

Array of variables is not needed in your example

and parameter for function can't be used because you use OnEvent mode.

There can be specified default color/backcolor for all controls by GUICtrlSetDefBkColor, GUICtrlSetDefColor .

For all picture checkbox controls is used only one OnEvent function (HealthCheck)

and inside is used @GUI_CtrlId macro for distinguishing from where it was called

Opt("GUIOnEventMode", 1)  ; Change to OnEvent mode
; ----------------------------------------------------------------------------
; START INCLUDING ALL NECESSARY AUTOIT FILES TO RUN
; ----------------------------------------------------------------------------
#include <String.au3>
#include <Array.au3>
#include <Math.au3>
#include <Date.au3>
#include <File.au3>
#include <FileConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#include <GUIConstantsEX.au3>
#include <GuiRichEdit.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <ProgressConstants.au3>
#Include <GuiToolBar.au3>
; ----------------------------------------------------------------------------
; END INCLUDES
; ----------------------------------------------------------------------------
$HealthCheck_Page1 = GUICreate("", 500, 495, -1, -1, $WS_POPUP)
GUISetBkColor(0x313131, $HealthCheck_Page1)
GUICtrlSetDefBkColor(0x313131, $HealthCheck_Page1)
GUICtrlSetDefColor(0xb8b8b8, $HealthCheck_Page1)
; ** Set Background **
;GUICtrlCreatePic("./images/menubackground.jpg", 0, 0, 500, 500, $WS_CLIPSIBLINGS)
; ** Create Header image **
;GUICtrlCreatePic("./images/bannerhealthchecks.jpg", 0, 0, 500, 193)
; ** Create Input Fields **
GUICtrlCreateLabel("Customer Name: ", 10, 198, 140, 30)
GUICtrlSetFont(-1, 14, 400, 0, "Comic Sans MS")
GUICtrlSetColor(-1, 0xb8b8b8)
$HealthCheck_P1_Customer = GUICtrlCreateInput("", 160, 198, 330, 30, "", $WS_EX_TRANSPARENT)
GUICtrlSetFont(-1, 14, 400, 0, "Comic Sans MS")
GUICtrlSetBkColor(-1, 0x414141)
GUICtrlCreateLabel("Machine ID: ", 10, 238, 140, 30)
GUICtrlSetFont(-1, 14, 400, 0, "Comic Sans MS")
$HealthCheck_P1_MachineID = GUICtrlCreateInput("", 160, 238, 330, 30, "", $WS_EX_TRANSPARENT)
GUICtrlSetFont(-1, 14, 400, 0, "Comic Sans MS")
GUICtrlSetBkColor(-1, 0x414141)
; ** Create Checkboxes **
GUICtrlCreateLabel("Create a restore point first: ", 10, 281, 300, 30)
GUICtrlSetFont(-1, 14, 400, 0, "Comic Sans MS")
$HealthCheck_P1_RestorePointSlide = GUICtrlCreatePic("./switchoff.bmp", 305, 278, 60, 30, $SS_NOTIFY)
GUICtrlSetOnEvent($HealthCheck_P1_RestorePointSlide, "HealthCheck")
$HealthCheck_P1_RestorePointSlideVar = False
GUICtrlCreateLabel("Backup registry first: ", 10, 321, 300, 30)
GUICtrlSetFont(-1, 14, 400, 0, "Comic Sans MS")
$HealthCheck_P1_RegistrySlide = GUICtrlCreatePic("./switchoff.bmp", 305, 318, 60, 30, $SS_NOTIFY)
GUICtrlSetOnEvent($HealthCheck_P1_RegistrySlide, "HealthCheck")
$HealthCheck_P1_RegistrySlideVar = False
GUICtrlCreateLabel("Produce report for this machine: ", 10, 361, 300, 30)
GUICtrlSetFont(-1, 14, 400, 0, "Comic Sans MS")
$HealthCheck_P1_ReportSlide = GUICtrlCreatePic("./switchoff.bmp", 305, 358, 60, 30, $SS_NOTIFY)
GUICtrlSetOnEvent($HealthCheck_P1_ReportSlide, "HealthCheck")
$HealthCheck_P1_ReportSlideVar = False
GUICtrlCreateLabel("Reboot machine when completed: ", 10, 401, 300, 30)
GUICtrlSetFont(-1, 14, 400, 0, "Comic Sans MS")
$HealthCheck_P1_RebootSlide = GUICtrlCreatePic("./switchoff.bmp", 305, 398, 60, 30, $SS_NOTIFY)
GUICtrlSetOnEvent($HealthCheck_P1_RebootSlide, "HealthCheck")
$HealthCheck_P1_RebootSlideVar = False
; ** Navigation Buttons **
$HealthCheck_P1_BackBtn = GUICtrlCreatePic("./exit.bmp", 10, 448, 106, 37, $SS_NOTIFY)
GUICtrlSetOnEvent($HealthCheck_P1_BackBtn, "HealthCheck_P1_Back")
$HealthCheck_P1_NextBtn = GUICtrlCreatePic("./exit.bmp", 384, 448, 106, 37, $SS_NOTIFY)
GUICtrlSetOnEvent($HealthCheck_P1_NextBtn, "HealthCheck_P1_Next")
GUISetState(@SW_SHOW, $HealthCheck_Page1)
While 1
  Sleep(500)   ; Just idle around
WEnd
; ----------------------------------------------------------------------------
; START FUNCTIONS FOR PAGE
; ----------------------------------------------------------------------------
Func HealthCheck_P1_Back()
    Exit
EndFunc
 
Func HealthCheck_P1_Next()
    Exit
EndFunc
 
Func HealthCheck()
    Local $var
    $pic = @GUI_CtrlId
    Switch $pic
        Case $HealthCheck_P1_RebootSlide
            $HealthCheck_P1_RebootSlideVar = Not $HealthCheck_P1_RebootSlideVar
            $var = $HealthCheck_P1_RebootSlideVar
        Case $HealthCheck_P1_ReportSlide
            $HealthCheck_P1_ReportSlideVar = Not $HealthCheck_P1_ReportSlideVar
            $var = $HealthCheck_P1_ReportSlideVar
        Case $HealthCheck_P1_RegistrySlide
            $HealthCheck_P1_RegistrySlideVar = Not $HealthCheck_P1_RegistrySlideVar
            $var = $HealthCheck_P1_RegistrySlideVar
        Case $HealthCheck_P1_RestorePointSlide
            $HealthCheck_P1_RestorePointSlideVar = Not $HealthCheck_P1_RestorePointSlideVar
            $var = $HealthCheck_P1_RestorePointSlideVar
    EndSwitch
    
    If $Var = True Then
        GUICtrlSetImage($pic, "./switchon.bmp")
    Else
        GUICtrlSetImage($pic, "./switchoff.bmp")
    EndIf
EndFunc
 
; ----------------------------------------------------------------------------
; END FUNCTIONS
; ----------------------------------------------------------------------------
Edited by Zedna
Link to comment
Share on other sites

All the variables are global so you can use them anywhere

$HealthCheck_P1_RebootSlideVar = Not $HealthCheck_P1_RebootSlideVar

$HealthCheck_P1_ReportSlideVar = Not $HealthCheck_P1_ReportSlideVar

$HealthCheck_P1_RegistrySlideVar = Not $HealthCheck_P1_RegistrySlideVar

$HealthCheck_P1_RestorePointSlideVar = Not $HealthCheck_P1_RestorePointSlideVar

Edited by Zedna
Link to comment
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
 Share

  • Recently Browsing   0 members

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