Jump to content

Update Buttons


Recommended Posts

I want a button to update when another button is pressed. I got this code so far:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
$TFarbe = 0xC0C0C0
$Hfarbe = 0x000000
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Test", 692, 211, 175, 144)
$Button1 = GUICtrlCreateButton("", 8, 32, 25, 25)
GUICtrlSetBkColor(-1, 0x000000)
$Button3 = GUICtrlCreateButton("", 40, 32, 25, 25)
GUICtrlSetBkColor(-1, 0x000080)
$Button4 = GUICtrlCreateButton("", 72, 32, 25, 25)
GUICtrlSetBkColor(-1, 0x008000)
$Button5 = GUICtrlCreateButton("", 104, 32, 25, 25)
GUICtrlSetBkColor(-1, 0x008080)
$Button6 = GUICtrlCreateButton("", 136, 32, 25, 25)
GUICtrlSetBkColor(-1, 0x800000)
$Button7 = GUICtrlCreateButton("", 168, 32, 25, 25)
GUICtrlSetBkColor(-1, 0x800080)
$Button8 = GUICtrlCreateButton("", 200, 32, 25, 25)
GUICtrlSetBkColor(-1, 0x808000)
$Button9 = GUICtrlCreateButton("", 232, 32, 25, 25)
GUICtrlSetBkColor(-1, 0xC0C0C0)
$Button10 = GUICtrlCreateButton("", 264, 32, 25, 25)
GUICtrlSetBkColor(-1, 0x808080)
$Button11 = GUICtrlCreateButton("", 296, 32, 25, 25)
GUICtrlSetBkColor(-1, 0x0000FF)
$Button12 = GUICtrlCreateButton("", 328, 32, 25, 25)
GUICtrlSetBkColor(-1, 0x00FF00)
$Button13 = GUICtrlCreateButton("", 360, 32, 25, 25)
GUICtrlSetBkColor(-1, 0x00FFFF)
$Button14 = GUICtrlCreateButton("", 392, 32, 25, 25)
GUICtrlSetBkColor(-1, 0xFF0000)
$Button15 = GUICtrlCreateButton("", 424, 32, 25, 25)
GUICtrlSetBkColor(-1, 0xFF00FF)
$Button16 = GUICtrlCreateButton("", 456, 32, 25, 25)
GUICtrlSetBkColor(-1, 0xFFFF00)
$Button17 = GUICtrlCreateButton("", 488, 32, 25, 25)
GUICtrlSetBkColor(-1, 0xFFFFFF)
$Button2 = GUICtrlCreateButton("", 488, 100, 25, 25)
GUICtrlSetBkColor(-1, 0xFFFFFF)
$Button18 = GUICtrlCreateButton("", 104, 100, 25, 25)
GUICtrlSetBkColor(-1, 0x008080)
$Button19 = GUICtrlCreateButton("", 168, 100, 25, 25)
GUICtrlSetBkColor(-1, 0x800080)
$Button20 = GUICtrlCreateButton("", 136, 100, 25, 25)
GUICtrlSetBkColor(-1, 0x800000)
$Button21 = GUICtrlCreateButton("", 8, 100, 25, 25)
GUICtrlSetBkColor(-1, 0x000000)
$Button22 = GUICtrlCreateButton("", 40, 100, 25, 25)
GUICtrlSetBkColor(-1, 0x000080)
$Button23 = GUICtrlCreateButton("", 72, 100, 25, 25)
GUICtrlSetBkColor(-1, 0x008000)
$Button24 = GUICtrlCreateButton("", 200, 100, 25, 25)
GUICtrlSetBkColor(-1, 0x808000)
$Button25 = GUICtrlCreateButton("", 232, 100, 25, 25)
GUICtrlSetBkColor(-1, 0xC0C0C0)
$Button26 = GUICtrlCreateButton("", 264, 100, 25, 25)
GUICtrlSetBkColor(-1, 0x808080)
$Button27 = GUICtrlCreateButton("", 296, 100, 25, 25)
GUICtrlSetBkColor(-1, 0x0000FF)
$Button28 = GUICtrlCreateButton("", 328, 100, 25, 25)
GUICtrlSetBkColor(-1, 0x00FF00)
$Button29 = GUICtrlCreateButton("", 360, 100, 25, 25)
GUICtrlSetBkColor(-1, 0x00FFFF)
$Button30 = GUICtrlCreateButton("", 392, 100, 25, 25)
GUICtrlSetBkColor(-1, 0xFF0000)
$Button31 = GUICtrlCreateButton("", 424, 100, 25, 25)
GUICtrlSetBkColor(-1, 0xFF00FF)
$Button32 = GUICtrlCreateButton("", 456, 100, 25, 25)
GUICtrlSetBkColor(-1, 0xFFFF00)
GUICtrlSetState(-1, $GUI_DISABLE)
$Label1 = GUICtrlCreateLabel("Wähle die Textfarbe", 8, 8, 125, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$Label2 = GUICtrlCreateLabel("Wähle die Hintergrundfarbe", 9, 71, 168, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$Button33 = GUICtrlCreateButton("", 576, 16, 57, 57)
GUICtrlSetBkColor(-1, $TFarbe)
GUICtrlSetState(-1, $GUI_DISABLE)
$Button34 = GUICtrlCreateButton("", 576, 88, 57, 57)
GUICtrlSetBkColor(-1, $Hfarbe)
GUICtrlSetState(-1, $GUI_DISABLE)
$OK = GUICtrlCreateButton("OK", 224, 160, 105, 33)
$Abbrechen = GUICtrlCreateButton("Abbrechen", 352, 160, 105, 33)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Abbrechen
Exit
Case $Button1
$TFarbe = 0x000000
Case $Button3
$TFarbe = 0x000080
Case $Button4
$TFarbe = 0x008000
Case $Button5
$TFarbe = 0x008080
Case $Button6
$TFarbe = 0x800000
Case $Button7
$TFarbe = 0x800080
Case $Button8
$TFarbe = 0x808000
Case $Button9
$TFarbe = 0xC0C0C0
Case $Button10
$TFarbe = 0x808080
Case $Button11
$TFarbe = 0x0000FF
Case $Button12
$TFarbe = 0x00FF00
Case $Button13
$TFarbe = 0x00FFFF
Case $Button14
$TFarbe = 0xFF0000
Case $Button15
$TFarbe = 0xFF00FF
Case $Button16
$TFarbe = 0xFFFF00
Case $Button17
$TFarbe = 0xFFFFFF
EndSwitch
WEnd

I want $Button34 and $Button33 To change to color $Tfarbe (for 33) or $HFarbe (for 34) if you press on one of the other buttons which change the variables $Tfarbe and $Hfarbe. I hope it's not a problem that the variables are in german :)

Edited by Reinhardt1julian
Link to comment
Share on other sites

You code is only changing the value that $TFarbe contains, it's not going to affect the buttons unless you reset the color after you've changed the value of the variable.

Something like this (one example)

Case $Button1
     $TFarbe = 0x000000
     GUICtrlSetBkColor($Button1, $TFarbe)

; or even easier and not using the variable.
Case $Button1
     GUICtrlSetBkColor($Button1, 0x000000)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • Moderators

Reinhardt1julian,

You need to learn about loops and arrays: ;)

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#include <Array.au3>

$TFarbe = 0xC0C0C0
$Hfarbe = 0x000000
Global $aFarbe[32] = [0x000000, 0x000080, 0x008000, 0x008080, 0x800000, 0x800080, 0x808000, 0xC0C0C0, 0x808080, 0x0000FF, 0x00FF00, 0x00FFFF, 0xFF0000, 0xFF00FF, 0xFFFF00, 0xFFFFFF, _
                      0xFFFFFF, 0x008080, 0x800080, 0x800000, 0x000000, 0x000080, 0x008000, 0x808000, 0xC0C0C0, 0x808080, 0x0000FF, 0x00FF00, 0x00FFFF, 0xFF0000, 0xFF00FF, 0xFFFF00]
Global $aButton[32]

$Form1 = GUICreate("Test", 692, 211, 175, 144)
For $j = 0 To 1
    For $i = 0 To 14
        $iIndex = $i + ($j * 15)
        $aButton[$iIndex] = GUICtrlCreateButton("", 8 + (32 * $i), 32 + (68 * $j), 25, 25)
        GUICtrlSetBkColor(-1, $aFarbe[$iIndex])
    Next
Next

$Label1 = GUICtrlCreateLabel("Wähle die Textfarbe", 8, 8, 125, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$Label2 = GUICtrlCreateLabel("Wähle die Hintergrundfarbe", 9, 71, 168, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$Button33 = GUICtrlCreateButton("", 576, 16, 57, 57)
GUICtrlSetBkColor(-1, $TFarbe)
GUICtrlSetState(-1, $GUI_DISABLE)
$Button34 = GUICtrlCreateButton("", 576, 88, 57, 57)
GUICtrlSetBkColor(-1, $Hfarbe)
GUICtrlSetState(-1, $GUI_DISABLE)
$OK = GUICtrlCreateButton("OK", 224, 160, 105, 33)
$Abbrechen = GUICtrlCreateButton("Abbrechen", 352, 160, 105, 33)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Abbrechen
            Exit
        Case $aButton[0] To $aButton[29]
            For $i = 0 To 29
                If $aButton[$i] = $nMsg Then
                    Switch $i
                        Case 0 To 14
                            $TFarbe = $aFarbe[$i]
                            GUICtrlSetBkColor($Button33, $TFarbe)
                            ExitLoop
                        Case 15 To 29
                            $Hfarbe = $aFarbe[$i]
                            GUICtrlSetBkColor($Button34, $HFarbe)
                            ExitLoop
                    EndSwitch

                EndIf
            Next
    EndSwitch
WEnd

Please ask if you have any questions. :)

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:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

Reinhardt1julian,

I must have placed the colours into the array incorrectly. You just need to change them into the correct ones - the $aFarbe array elements have the same location in the script as the buttons to which they apply, so it should be easy for you to do. :)

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:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Abbrechen
            Exit
        Case $aButton[0] To $aButton[29]
            $i = $nMsg - $aButton[0]
            Switch $i
                Case 0 To 14
                    $TFarbe = $aFarbe[$i]
                    GUICtrlSetBkColor($Button33, $TFarbe)
                Case 15 To 29
                    $Hfarbe = $aFarbe[$i]
                    GUICtrlSetBkColor($Button34, $HFarbe)
            EndSwitch
    EndSwitch
WEnd

#include <WinAPI.au3>
#include <WindowsConstants.au3>
Global Const $GUI_RUNDEFMSG = 'GUI_RUNDEFMSG'
Global $btn1, $btn2, $iMemo = 0, $Button1, $Button2, $Button, $aFarbe, $Label, $Label1, $Label2
Global $aFarbe1[17] = [16, 0x000000, 0x000080, 0x008000, 0x008080, 0x800000, 0x800080, 0x808000, 0xC0C0C0, 0x808080, 0x0000FF, 0x00FF00, 0x00FFFF, 0xFF0000, 0xFF00FF, 0xFFFF00, 0xFFFFFF]
Global $aFarbe2[17] = [16, 0xFFFFFF, 0x008080, 0x800080, 0x800000, 0x000000, 0x000080, 0x008000, 0x808000, 0xC0C0C0, 0x808080, 0x0000FF, 0x00FF00, 0x00FFFF, 0xFF0000, 0xFF00FF, 0xFFFF00]

$Gui = GUICreate('My Program', 250, 260)
$Button1 = GUICtrlCreateButton('Color', 10, 10, 92, 22)
$Button2 = GUICtrlCreateButton('Color', 120, 10, 92, 22)
$Label1 = GUICtrlCreateLabel('', 10, 32, 92, 92)
$Label2 = GUICtrlCreateLabel('', 120, 32, 92, 92)
$StatusBar = GUICtrlCreateLabel('StatusBar', 5, 260 - 20, 150, 17)
GUISetState()
GUIRegisterMsg(0x004E, "WM_NOTIFY")

While 1
    $msg = GUIGetMsg()
    If $iMemo = 2 Then
        $iMemo = 0
        _MsgFile($Button, $aFarbe, $Label)
    EndIf
    Switch $msg
        Case -3
            Exit
    EndSwitch
WEnd

Func _MsgFile($ctrl, $aFarbe, $Label)
    Local $EditBut, $Gui1, $GuiPos, $msg, $StrBut, $iOpt, $Gui1ci, $hcgp, $cgp, $tpoint, $x, $y
    $hcgp = GUICtrlGetHandle($ctrl)
    $cgp = WinGetPos($hcgp)
    $tpoint = DllStructCreate("int X;int Y")
    _WinAPI_ClientToScreen($hcgp, $tpoint)
    $x = DllStructGetData($tpoint, "X")
    $y = DllStructGetData($tpoint, "Y") + $cgp[3]

    $Gui1 = GUICreate('', 22 * 4, 22 * 4, $x, $y, $WS_POPUP + $WS_DLGFRAME, -1, $Gui)
    Local $aBt[$aFarbe[0] + 1]
    $aBt[0] = $aFarbe[0]
    $iOpt = Opt("GUICoordMode", 2)
    For $i = 1 To $aFarbe[0]
        Switch $i
            Case 5, 9, 13
                $aBt[$i] = GUICtrlCreateLabel('', -22 * 4, 0, 22, 22)
                GUICtrlSetBkColor(-1, $aFarbe[$i])
            Case 1
                $aBt[$i] = GUICtrlCreateLabel('', 1, 2, 22, 22)
                GUICtrlSetBkColor(-1, $aFarbe[$i])
            Case Else
                $aBt[$i] = GUICtrlCreateLabel('', 0, -1, 22, 22)
                GUICtrlSetBkColor(-1, $aFarbe[$i])
        EndSwitch
    Next
    Opt("GUICoordMode", $iOpt)
    GUISetState(@SW_SHOW, $Gui1)

    While 1
        $msg = GUIGetMsg()
        Switch $msg
            Case $aBt[1] To $aBt[$aBt[0]]
                $i = $msg - $aBt[1] +1
                ; $Farbe = $aFarbe[$i]
                GUICtrlSetData($Button, Hex($aFarbe[$i],6))
                GUICtrlSetBkColor($Label, $aFarbe[$i])
                ExitLoop
        EndSwitch
        If $iMemo = 1 Then
            $Gui1ci = GUIGetCursorInfo($Gui1)
            ; GUICtrlSetData($Label, $Gui1ci[0])
            If $Gui1ci[0] < -2 Or $Gui1ci[1] < -5 Or $Gui1ci[0] > 90 Or $Gui1ci[1] > 90 Then
                $iMemo = 0
                ExitLoop
            EndIf
        EndIf
    WEnd
    GUIDelete($Gui1)
EndFunc   ;==>_MsgFile

Func WM_NOTIFY($hWnd, $msg, $wParam, $lParam)
    #forceref $hWnd, $Msg, $wParam
    Local Const $BCN_HOTITEMCHANGE = -1249
    Local $tNMBHOTITEM = DllStructCreate("hwnd hWndFrom;int IDFrom;int Code;dword dwFlags", $lParam)
    Local $nNotifyCode = DllStructGetData($tNMBHOTITEM, "Code")
    Local $nID = DllStructGetData($tNMBHOTITEM, "IDFrom")
    Local $hCtrl = DllStructGetData($tNMBHOTITEM, "hWndFrom")
    Local $dwFlags = DllStructGetData($tNMBHOTITEM, "dwFlags")

    Switch $nID
        Case $Button1
            $Button = $Button1
            $aFarbe = $aFarbe1
            $Label = $Label1
            Switch $nNotifyCode
                Case $BCN_HOTITEMCHANGE ; Win XP and Above
                    If BitAND($dwFlags, 0x10) = 0x10 Then
                        $iMemo = 2
                    ElseIf BitAND($dwFlags, 0x20) = 0x20 Then
                        $iMemo = 1
                    EndIf
            EndSwitch
        Case $Button2
            $Button = $Button2
            $aFarbe = $aFarbe2
            $Label = $Label2
            Switch $nNotifyCode
                Case $BCN_HOTITEMCHANGE ; Win XP and Above
                    If BitAND($dwFlags, 0x10) = 0x10 Then
                        $iMemo = 2
                    ElseIf BitAND($dwFlags, 0x20) = 0x20 Then
                        $iMemo = 1
                    EndIf
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
Edited by AZJIO
Link to comment
Share on other sites

@M23,

Why another For loop

the msg loop could be simplified as this

Local $iNdEx
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Abbrechen
Exit
Case $aButton[0] To $aButton[29]
$iNdEx = $nMsg - $aButton[0]
Switch $iNdEx
Case 0 To 14
$TFarbe = $aFarbe[$iNdEx]
GUICtrlSetBkColor($Button33, $TFarbe)
Case 15 To 29
$Hfarbe = $aFarbe[$iNdEx]
GUICtrlSetBkColor($Button34, $Hfarbe)
EndSwitch
EndSwitch
WEnd
Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

  • Moderators

PhoenixXL,

I quite agree with you, but given the obvious inexperience of the OP I thought it best to keep it as simple as I could initially. :)

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:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...