Jump to content

creating a gui with image buttons, with scrolling and highlighting the buttons


Recommended Posts

i am trying to reuse code from another thread that was from 2011,  so i hope opening a new thread is Ok..  

i have been working on this code trying to  make it work with a set of images i get from a xml file...  i haven't gotten that far yet i'm just setting the image count and later will get that  here is the code i have so far....

#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
#include <WinAPI.au3>
#include <Skin.au3>
#include <GDIPlus.au3>
#include <Array.au3>
#include <GuiButton.au3>
#include <GuiListView.au3>
#include "GUIScrollbars_Ex.au3"

#include ".\Skins\Cosmo.au3"
#include "_UskinLibrary.au3"

_Uskin_LoadDLL()
_USkin_Init(_Cosmo(True))


Global $X = 195, $Count = 0, $Width = 1250, $Height = 750
Global $sImagePath = @ScriptDir & "\Imgs\"

; Put your image paths in this array
;Global $aImage[8] = [$sImagePath & "League of Legends.bmp", $sImagePath & "Team Fortress 2.bmp", "Image_Path_3", "Image_Path_4", "Image_Path_5", "Image_Path_6", "Image_Path_7", "Image_Path_8"]

Global $aImage[4000], $Button[4000] , $aLabel[4000], $aButton[4000], $aLabel[4000]
Global $imgCount = 100


MainGui()

Func MainGui()
   $MainGUI = GUICreate("", $Width, $Height) ; will create a dialog box that when displayed is centered]
    Opt("GUICoordMode", 20)

  For $i = 1 To $imgCount
        $X += 100
     Next

    _GUIScrollbars_Generate($MainGUI, 0, $X)
   GUICtrlSetResizing(-1, $GUI_DOCKAUTO)


    ; Create labels
    For $i = 0 To $imgCount - 1
       ; $Count += 1
        For $j = 0 To 3
            $iIndex = $j + ($i * 4)
            $aLabel[$iIndex] = GUICtrlCreateLabel("", 45 + (300 * $j), 25 + (300 * $i), 266, 266)
            GUICtrlSetBkColor($aLabel[$iIndex], 0xFFFFFF)
            GUICtrlSetState($aLabel[$iIndex], $GUI_DISABLE)
        Next
    Next

    ; Create buttons
    For $i = 0 To $imgCount - 1
       ; $Count += 1
        For $j = 0 To 3
            $iIndex = $j + ($i * 4)
            $aButton[$iIndex] = GUICtrlCreateButton("Button " & ($iIndex + 1), 50 + (300 * $j), 30 + (300 * $i), 256, 256, $BS_BITMAP)
            GUICtrlSetImage($aButton[$iIndex], $aImage[$iIndex])
        Next
    Next


    ; Set default button
    GUICtrlSetState($aButton[0], $GUI_FOCUS)
    GUICtrlSetBkColor($aLabel[0], 0x00FF00)
    $hActive = GUICtrlGetHandle($aButton[0])

    ; Create dummy controls
    $cDummy_Up = GUICtrlCreateDummy()
    $cDummy_Dn = GUICtrlCreateDummy()

    GUISetState()

    ; Set the Up/Down keys as accelerators - they will only act like this when your GUI is active
    Local $aAccelKeys[2][2] = [["{UP}", $cDummy_Up], ["{DOWN}", $cDummy_Dn]]
    GUISetAccelerators($aAccelKeys)


    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $cDummy_Up, $cDummy_Dn
                _Vertical_TabStop()

            Case $aButton[$i]
                  MsgBox($MB_SYSTEMMODAL, "Pressed", $aButton[$i])
        EndSwitch


        ; Get focused control
        $hCurrFocus = _WinAPI_GetFocus()
        ; If it has changed
        If $hCurrFocus <> $hActive Then
            ; See if it is a button
            For $i = 0 To $imgCount
                If $hCurrFocus = GUICtrlGetHandle($aButton[$i]) Then
                    ; Reset all the labels
                    For $j = 0 To $imgCount
                        GUICtrlSetBkColor($aLabel[$j], 0xFFFFFF)
                    Next
                    ; Highlight the correct label
                    GUICtrlSetBkColor($aLabel[$i], 0x00FF00)
                    ExitLoop
                EndIf
            Next
            $hActive = $hCurrFocus
        EndIf
     WEnd

EndFunc   ;==>Mainscript

Func _Vertical_TabStop()

    ; Get active control
    $hActive = _WinAPI_GetFocus()
    For $i = 0 To 4
        ; If it is a button
        If $hActive = GUICtrlGetHandle($aButton[$i]) Then
            ; Then determine index of the button above/below
            $iIndex = Mod($i + 4, $imgCount)
            ; And set focus
            GUICtrlSetState($aButton[$iIndex], $GUI_FOCUS)
            ; No point in looking further
            ExitLoop
        EndIf
    Next

EndFunc

 

expand popup

Now a couple issues i am having and maybe you can see the simple mistak9s)

1.)  when button is clicked  it starts with saying button presses was the number 403 and stops at 504
2.)  i cant go up and down ,  i can go down 3 and then it stops
3.) the highlighting only goes so far and just stops which is actually 503
4.) its showing more than 100 buttons
5.) the scroll bar should stop at the last row and i cant seem to get that to work either

I feel i am so close but so far :(

any help and explanation to what i am missing would be awesome

i am basically taking images from a xml file and adding to the gui and need to use  up down and left and right arrows to  move to each one and highlight when i do.

Edited by dynamitemedia
Link to comment
Share on other sites

it is definitely in the for loop

#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
#include <WinAPI.au3>
#include <Skin.au3>
#include <GDIPlus.au3>
#include <Array.au3>
#include <GuiButton.au3>
#include <GuiListView.au3>
#include "GUIScrollbars_Ex.au3"

#include ".\Skins\Cosmo.au3"
#include "_UskinLibrary.au3"

_Uskin_LoadDLL()
_USkin_Init(_Cosmo(True))


Global $Count = 0, $Width = 1250, $Height = 750
Global $sImagePath = @ScriptDir & "\Imgs\"

; Put your image paths in this array
;Global $aImage[8] = [$sImagePath & "League of Legends.bmp", $sImagePath & "Team Fortress 2.bmp", "Image_Path_3", "Image_Path_4", "Image_Path_5", "Image_Path_6", "Image_Path_7", "Image_Path_8"]

Global $aImage[4000]


Global $Button[4000], $aLabel[4000], $aButton[4000], $aLabel[4000]
Global $imgCount = 10
Global $X = 200


MainGui()

Func MainGui()
   $MainGUI = GUICreate("", $Width, $Height) ; will create a dialog box that when displayed is centered]
    Opt("GUICoordMode", 20)

For $i = 0 To $imgCount - 1
        $X += 280
     Next

    _GUIScrollbars_Generate($MainGUI, 0, $X)
   GUICtrlSetResizing(-1, $GUI_DOCKAUTO)

    ; Create labels
    For $i = 0 To $imgCount
      ;  ConsoleWrite( "label i:  " & $i & @CRLF ) ; If you're in SciTE
        For $j = 0 To $imgCount
            ; ConsoleWrite( "label j:  " & $j & @CRLF ) ; If you're in SciTE
            $iIndex = $j + ($i * 4)
            $aLabel[$iIndex] = GUICtrlCreateLabel("", 45 + (300 * $j), 25 + (300 * $i), 266, 266)

            GUICtrlSetState($aLabel[$iIndex], $GUI_DISABLE)
        Next
    Next


#cs
   ; Create buttons
   $aIndex = 0
   For $i = 0 To $imgCount - 1
   $aIndex += 1
         ConsoleWrite( "button aIndex:  " & $aIndex & @CRLF ) ; If you're in SciTE
         ; $aButton[$aIndex] = GUICtrlCreateButton("Button " & ($aIndex), 50 + (300 * $imgCount), 30 + (300 * $imgCount), 256, 256, $BS_BITMAP)
         ;GUICtrlSetBkColor($aLabel[$iIndex], 0xFFFFFF)
         GUICtrlSetImage($aButton[$aIndex], $aImage[$iIndex])
   Next

#ce

; Create buttons
$aIndex = 0
    For $i = 0 To $imgCount - 1
 $aIndex += 1
        For $j = 0 To 3
            $aButton[$iIndex] = GUICtrlCreateButton("Button " & ($aIndex), 50 + (300 * $j), 30 + (300 * $i), 256, 256, $BS_BITMAP)
            GUICtrlSetBkColor($aButton[$iIndex], 0xFFFFFF)
            GUICtrlSetImage($aButton[$iIndex], $aImage[$iIndex])
        Next
    Next

    ; Set default button

    ;$minus1=
    GUICtrlSetState($aButton[0], $GUI_FOCUS)
    GUICtrlSetBkColor($aLabel[0], 0x00FF00)
    $hActive = GUICtrlGetHandle($aButton[0])


    ; Create dummy controls
    $cDummy_Up = GUICtrlCreateDummy()
    $cDummy_Dn = GUICtrlCreateDummy()

    GUISetState()

    ; Set the Up/Down keys as accelerators - they will only act like this when your GUI is active
    Local $aAccelKeys[2][2] = [["{UP}", $cDummy_Up], ["{DOWN}", $cDummy_Dn]]
    GUISetAccelerators($aAccelKeys)


    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $cDummy_Up, $cDummy_Dn
                _Vertical_TabStop()

            Case $aButton[$i]
                  $buttonLabel = $aButton[$i]
                  ; MsgBox($MB_SYSTEMMODAL, "Pressed",  $buttonLabel)
        EndSwitch

; ----------------------  working and stopping at right image count   -------------------------- >

        ; Get focused control
        $hCurrFocus = _WinAPI_GetFocus()
        ; If it has changed
        If $hCurrFocus <> $hActive Then
            ; See if it is a button
            For $i = 0 To $imgCount - 1
                If $hCurrFocus = GUICtrlGetHandle($aButton[$i]) Then
                    ; Reset all the labels
                    For $j = 0 To $imgCount - 1
                        GUICtrlSetBkColor($aLabel[$j], 0xFFFFFF)
                    Next
                    ; Highlight the correct label
                    GUICtrlSetBkColor($aLabel[$i], 0x00FF00)


                     $buttonLabel = $aLabel[$i] - 2  ; cause it starts at 3 for some reason??
                    ConsoleWrite( "Button Label:  " & $buttonLabel & @CRLF ) ; If you're in SciTE
                    ExitLoop
                EndIf
            Next
            $hActive = $hCurrFocus
        EndIf
     WEnd

EndFunc   ;==>Mainscript

Func _Vertical_TabStop()

    ; Get active control
    $hActive = _WinAPI_GetFocus()
    For $i = 0 To $imgCount
        ; If it is a button
        If $hActive = GUICtrlGetHandle($aButton[$i]) Then
            ; Then determine index of the button above/below
            $iIndex = Mod($i + 4, $imgCount)


            ; And set focus
            GUICtrlSetState($aButton[$iIndex], $GUI_FOCUS)
            ; No point in looking further
            ExitLoop
        EndIf
    Next

EndFunc

@Melba23 since you wrote this can you see where i am going wrong?

this 2nd version show what the labels etc look like,  i have spent 3 days on this and cant figure out where i am going wrong i can get it to do the correct amount of  back ground colors if i set it for 10 it will be 10  but the minute i try to make buttons its all messed up

 

think scrolling is working fine now

Link to comment
Share on other sites

Scroll bar works? I wasn't able to get it to work on my end but I implemented a _IsPressed for the scroll bar and it works fine. 

#include <Misc.au3>

Local $hDLL = DllOpen("user32.dll")

While 1
    _LeftArrow()
    _RightArrow()
    While _IsPressed("28", $hDLL)
        sleep(250)
    WEnd
WEnd

DllClose($hDLL)

Func _RightArrow()
    If _IsPressed("27", $hDLL) Then
        MouseWheel("down", 1)
    EndIf
EndFunc   ;==>_LeftArrow

Func _LeftArrow()
    If _IsPressed("25", $hDLL) Then
        MouseWheel("up", 1)
    EndIf
EndFunc   ;==>_LeftArrow

 

Edited by aa2zz6
Link to comment
Share on other sites

thanks i'll check that out...

the biggest issue im having is the drawing of the correct amount of buttons

; Create buttons
$aIndex = 0
    For $i = 0 To $imgCount - 1
 $aIndex += 1
        For $j = 0 To 3
            $aButton[$iIndex] = GUICtrlCreateButton("Button " & ($aIndex), 50 + (300 * $j), 30 + (300 * $i), 256, 256, $BS_BITMAP)
            GUICtrlSetBkColor($aButton[$iIndex], 0xFFFFFF)
            GUICtrlSetImage($aButton[$iIndex], $aImage[$iIndex])
        Next
    Next

it creates the correct amount of back grounds but not buttons and look how they are numbered

Link to comment
Share on other sites

The numbering is fixed.. 5 boxes for every 19 rows?

The $x +=55 is what you was looking at for the amount of spaces added.

For $i = 1 To $imgCount
        $X += 55
     Next

Working script - Revised

#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
#include <WinAPI.au3>
#include <Skin.au3>
#include <GDIPlus.au3>
#include <Array.au3>
#include <GuiButton.au3>
#include <GuiListView.au3>
#include "GUIScrollbars_Ex.au3"

#include ".\Skins\Cosmo.au3"
#include "_UskinLibrary.au3"

_Uskin_LoadDLL()
_USkin_Init(_Cosmo(True))


Global $X = 195, $Count = 0, $Width = 1250, $Height = 750
Global $sImagePath = @ScriptDir & "\Imgs\"

; Put your image paths in this array
;Global $aImage[8] = [$sImagePath & "League of Legends.bmp", $sImagePath & "Team Fortress 2.bmp", "Image_Path_3", "Image_Path_4", "Image_Path_5", "Image_Path_6", "Image_Path_7", "Image_Path_8"]

Global $aImage[4000], $Button[4000] , $aLabel[4000], $aButton[4000], $aLabel[4000]
Global $imgCount = 100


MainGui()

Func MainGui()
   $MainGUI = GUICreate("", $Width, $Height) ; will create a dialog box that when displayed is centered]
    Opt("GUICoordMode", 20)

  For $i = 1 To $imgCount
        $X += 28
     Next

    _GUIScrollbars_Generate($MainGUI, 0, $X)
   GUICtrlSetResizing(-1, $GUI_DOCKAUTO)


    ; Create labels
    For $i = 0 To $imgCount - 1
       ; $Count += 1
        For $j = 0 To 3
            $iIndex = $j + ($i * 2)
            $aLabel[$iIndex] = GUICtrlCreateLabel("", 45 + (300 * $j), 25 + (300 * $i), 266, 266)
            GUICtrlSetBkColor($aLabel[$iIndex], 0xFFFFFF)
            GUICtrlSetState($aLabel[$iIndex], $GUI_DISABLE)
        Next
    Next

    ; Create buttons
    For $i = 0 To $imgCount - 1
        $Count += 1
        For $j = 0 To 3
            $iIndex = $j + ($i * 4)
            $aButton[$iIndex] = GUICtrlCreateButton("Button " & ($iIndex + 1), 50 + (300 * $j), 30 + (300 * $i), 256, 256, $BS_BITMAP)
            GUICtrlSetImage($aButton[$iIndex], $aImage[$iIndex])
        Next
    Next


    ; Set default button
    GUICtrlSetState($aButton[0], $GUI_FOCUS)
    GUICtrlSetBkColor($aLabel[0], 0x00FF00)
    $hActive = GUICtrlGetHandle($aButton[0])

    ; Create dummy controls
    $cDummy_Up = GUICtrlCreateDummy()
    $cDummy_Dn = GUICtrlCreateDummy()

    GUISetState()

    ; Set the Up/Down keys as accelerators - they will only act like this when your GUI is active
    Local $aAccelKeys[2][2] = [["{UP}", $cDummy_Up], ["{DOWN}", $cDummy_Dn]]
    GUISetAccelerators($aAccelKeys)


    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $cDummy_Up, $cDummy_Dn
                _Vertical_TabStop()

            Case $aButton[$i]
                  MsgBox($MB_SYSTEMMODAL, "Pressed", $aButton[$i])
        EndSwitch


        ; Get focused control
        $hCurrFocus = _WinAPI_GetFocus()
        ; If it has changed
        If $hCurrFocus <> $hActive Then
            ; See if it is a button
            For $i = 0 To $imgCount
                If $hCurrFocus = GUICtrlGetHandle($aButton[$i]) Then
                    ; Reset all the labels
                    For $j = 0 To $imgCount
                        GUICtrlSetBkColor($aLabel[$j], 0xFFFFFF)
                    Next
                    ; Highlight the correct label
                    GUICtrlSetBkColor($aLabel[$i], 0x00FF00)
                    ExitLoop
                EndIf
            Next
            $hActive = $hCurrFocus
        EndIf
     WEnd

EndFunc   ;==>Mainscript

Func _Vertical_TabStop()

    ; Get active control
    $hActive = _WinAPI_GetFocus()
    For $i = 0 To 4
        ; If it is a button
        If $hActive = GUICtrlGetHandle($aButton[$i]) Then
            ; Then determine index of the button above/below
            $iIndex = Mod($i + 4, $imgCount)
            ; And set focus
            GUICtrlSetState($aButton[$iIndex], $GUI_FOCUS)
            ; No point in looking further
            ExitLoop
        EndIf
    Next

EndFunc

EDIT - Uploaded revised working script

Buttons work correctly. 

$iIndex = $j + ($i * 4)

 

Edited by aa2zz6
Link to comment
Share on other sites

Ok i have actually stripped down some other code i was using and got it almost working

#include "GUIScrollbars_Ex.au3"
#include <Skin.au3>
#include <GDIPlus.au3>
#include <Array.au3>
#include <GuiButton.au3>
#include <GuiListView.au3>

Opt("GUIOnEventMode", 1)

#include ".\Skins\Cosmo.au3"
#include "_UskinLibrary.au3"

_Uskin_LoadDLL()
_USkin_Init(_Cosmo(True))

Global $MainGUI, $X = 200, $Count = 0

Global $ButtonX = 20, $ButtonY = 160, $GameList
Dim $Button[4000], $img[4000],  $aLabel[4000]

Global $imgCount = 25
Global $Width = 1350
Global $Height = 925

Global $MaxInaRow = 4
$ButtonX = 50

CreateGui()


Func CreateGui()
    $MainGUI = GUICreate("addd name here ", $Width - 100, $Height - 100, -1, -1, BitOR($WS_SIZEBOX, $WS_SYSMENU))
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
    GUISetState(@SW_SHOW)



  Opt("GUICoordMode", 4)

; ------------------------    create Buttons  ------------------------

    For $i = 0 To $imgCount - 1
      $Count += 1

      $aLabel[$i] = GUICtrlCreateLabel("", $ButtonX, $ButtonY, 266, 266)
      GUICtrlSetBkColor($aLabel[$i], 0xFFFFFF)
      GUICtrlSetState($aLabel[$i], $GUI_DISABLE)

      $Button[$i] = GUICtrlCreateButton("", $ButtonX + 5, $ButtonY + 5, 256, 256, BitOR ($BS_BITMAP, $BS_DEFPUSHBUTTON))
      _GUICtrlButton_SetImage($Button[$i], $img[$i])
      GUICtrlSetResizing(-1, $GUI_DOCKAUTO)

      GUICtrlSetOnEvent(-1, "_ButtonPushed")
      $ButtonX += 290

        If $Count = $MaxInaRow Then
            $X += 300

            If $MaxInaRow = 3 Then
                $ButtonX = 60
            ElseIf $MaxInaRow = 4 Then
                $ButtonX = 50
            ElseIf $MaxInaRow = 5 Then
                $ButtonX = 25
            ElseIf $MaxInaRow = 6 Then
                $ButtonX = 20
            EndIf

            $ButtonY += 320
            $Count = 0
        EndIf
    Next

; -------------  scrollbar    ----------------

    For $i = 1 To $imgCount
        $X += 55
    Next
    _GUIScrollbars_Generate($MainGUI, 0, $X)
    GUICtrlSetResizing(-1, $GUI_DOCKAUTO)

; --------------------------------------------

; Set default button
    GUICtrlSetState($Button[0], $GUI_FOCUS)
    GUICtrlSetBkColor($aLabel[0], 0x00FF00)
    $hActive = GUICtrlGetHandle($Button[0])

    ; Create dummy controls
    $cDummy_Up = GUICtrlCreateDummy()
    $cDummy_Dn = GUICtrlCreateDummy()

    GUISetState()

   ; Set the Up/Down keys as accelerators - they will only act like this when your GUI is active
    Local $aAccelKeys[2][2] = [["{UP}", $cDummy_Up], ["{DOWN}", $cDummy_Dn]]
    GUISetAccelerators($aAccelKeys)

    While 1
        Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $cDummy_Up, $cDummy_Dn
                _Vertical_TabStop()
        EndSwitch

        ; Get focused control
        $hCurrFocus = _WinAPI_GetFocus()
        ; If it has changed
        If $hCurrFocus <> $hActive Then
            ; See if it is a button
            For $i = 0 To $imgCount - 1
                If $hCurrFocus = GUICtrlGetHandle($Button[$i]) Then
                    ; Reset all the labels
                    For $j = 0 To $imgCount - 1
                        GUICtrlSetBkColor($aLabel[$j], 0xFFFFFF)
                    Next
                    ; Highlight the correct label
                    GUICtrlSetBkColor($aLabel[$i], 0x00FF00)
                    ExitLoop
                EndIf
            Next
            $hActive = $hCurrFocus
        EndIf
    WEnd


EndFunc   ;==>_CreateGui

Func _Vertical_TabStop()

    ; Get active control
    $hActive = _WinAPI_GetFocus()
    For $i = 0 To 7
        ; If it is a button
        If $hActive = GUICtrlGetHandle($Button[$i]) Then
            ; Then determine index of the button above/below
            $iIndex = Mod($i + 4, 8)
            ; And set focus
            GUICtrlSetState($Button[$i], $GUI_FOCUS)
            ; No point in looking further
            ExitLoop
        EndIf
    Next

EndFunc


Func _Exit()
    $X = 200
    $Count = 0
    $ButtonX = 40
    $ButtonY = 160
    GUIDelete($MainGUI)
EndFunc   ;==>_Exit

only issues i am seeing is:

1.)  scroll bar  is way too long and i would like it to only go past the last row a little bit

2.)  still haven't got the up and down arrows working but this is a huge start

Link to comment
Share on other sites

Scroll bar solution

Decrease the $X +=__ to reduce the $MaxInaRow. I found that += 100 was a good number. Change as needed.

If $Count = $MaxInaRow Then
            $X += 100

            If $MaxInaRow = 3 Then
                $ButtonX = 60
            ElseIf $MaxInaRow = 4 Then
                $ButtonX = 50
            ElseIf $MaxInaRow = 5 Then
                $ButtonX = 25
            ElseIf $MaxInaRow = 6 Then
                $ButtonX = 20
            EndIf

            $ButtonY += 320
            $Count = 0
        EndIf
    Next

 

Edited by aa2zz6
Link to comment
Share on other sites

18 minutes ago, aa2zz6 said:

Awesome. If I can provide anymore help, please feel free to ask.

 

can you see what is happening with the up and down arrows?

it seems that this wont work cause it is getting the wrong  button index
 

Func _Vertical_TabStop()

    ; Get active control
    $hActive = _WinAPI_GetFocus()
    For $i = 0 To 7
        ; If it is a button
        If $hActive = GUICtrlGetHandle($Button[$i]) Then
            ; Then determine index of the button above/below
            $iIndex = Mod($i + 4, 8)
            ; And set focus
            GUICtrlSetState($Button[$i], $GUI_FOCUS)
            ; No point in looking further
            ExitLoop
        EndIf
    Next

EndFunc

 

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

×
×
  • Create New...