#include <GUIConstants.au3>;Example START#include <Array.au3>$ChoiceArray=StringSplit("Choice 1|Choice 2|Choice 3|Choice 4|Choice 5|Choice 6|Choice 7|Choice 8|Choice 9|Choice 10","|")$ReturnArray=_ChoiceBox("Choice",$ChoiceArray,0,"Make your choice")_ArrayDisplay($ReturnArray,"Choice");Example END;===================================================================================================; Function Name: _ChoiceBox(); Description: Generate a ChoiceBox using an array; Syntax: _ChoiceBox($GUICaption, $ChoiceArray, $SelectionType[, $LabelText = ""]; [,[$GUIWidth = 150][, $LabelWidth = 130][, $GUIXPOS = -1][, $GUIYPOS = -1]); Parameter(s): $Caption : The caption of the ChoiceBox; $ChoiceArray : One dimensional array with items to select; $SelectionType : 0 = Checkbox, 1 = Radiobutton; $LabelText : Label with text below captionbar and above first item (Default = ""); $GUIWidth : ChoiceBox width, only needed when long values are exists in $ChoiceArray (Default = 150); $LabelWidth : Item label width, only needed when long values are exists in $ChoiceArray (Default = 130); $GUIXPOS : X-Pos of the ChoiceBox (Default = -1); $GUIYPOS : Y-Pos of the ChoiceBox (Default = -1);; Return Value(s): Succes: Returns an array, the first element ($array[0]) contains the number of selected items, ; the remaining elements ($array[1], $array[2], etc.) contain the selected items.; Failure: Returns a 0 (zero); Requirement(s): AutoIt 3.2.10.0 and above; Note(s): None; ; Author(s): Bart van Beek;===================================================================================================Func_ChoiceBox($Caption,$ChoiceArray,$SelectionType,$LabelText="",$GUIWidth=150,$LabelWidth=130,$GUIXPOS=-1,$GUIYPOS=-1)Dim$SelectionArray[$ChoiceArray[0]+1]Dim$YPOS=10If$LabelText=""ThenGUICreate($Caption,$GUIWidth,($ChoiceArray[0]*20+40),$GUIXPOS,$GUIYPOS)ElseGUICreate($Caption,$GUIWidth,($ChoiceArray[0]*20+70),$GUIXPOS,$GUIYPOS)GUICtrlCreateLabel($LabelText,10,10,$LabelWidth,20)Dim$YPOS=30EndIfFor$i=1To$ChoiceArray[0]Switch$SelectionTypeCase0$SelectionArray[$i]=GUICtrlCreateCheckbox($ChoiceArray[$i],10,$YPOS,$LabelWidth,20)Case1$SelectionArray[$i]=GUICtrlCreateRadio($ChoiceArray[$i],10,$YPOS,$LabelWidth,20)CaseElseReturn0EndSwitch$YPOS=$YPOS+20Next$BtnOk=GUICtrlCreateButton("&OK",5,$YPOS+5,80,20)GUISetState()While1$msg=GUIGetMsg()Switch$msgCase$GUI_EVENT_CLOSEReturn0Case$BtnOkDim$SelectedFor$i=1To$ChoiceArray[0]IfGUICtrlRead($SelectionArray[$i])=$GUI_CHECKEDThen$Selected=$Selected&GUICtrlRead($SelectionArray[$i],1)&"|"EndIfNext$Selected=StringTrimRight($Selected,1)ReturnStringSplit($Selected,"|")EndSwitchWEndEndFunc;==>_ChoiceBox
And very nice alternative spudw2k. It's a shame Treeviews don't support more controls like ComboBox, Radios or mixture of different controls. I suppose some hacking would have to be done.
Maybe using ScrollBars would be better fitted for such a case of dynamic GUI creation. This way the GUI can handle more than it can fit...
for $i = 1 to UBound($SCROLL_AMOUNTS)-1 If $SCROLL_AMOUNTS[$i][0] = $hWnd And $SCROLL_AMOUNTS[$i][1] = $SB_VERT Then $iRound = $SCROLL_AMOUNTS[$i][2] EndIf Next
If Not $iRound Then Return 0
_GUIScrollBars_ScrollWindow($hWnd, 0, Round(($iCurrentPos-$iPos)/$iRound)*$iRound) ElseIf $iBar = $SB_HORZ Then
$iRound = 0
for $i = 1 to UBound($SCROLL_AMOUNTS)-1 If $SCROLL_AMOUNTS[$i][0] = $hWnd And $SCROLL_AMOUNTS[$i][1] = $SB_HORZ Then $iRound = $SCROLL_AMOUNTS[$i][2] EndIf Next
for $i = 1 to UBound($SCROLL_AMOUNTS)-1 If $SCROLL_AMOUNTS[$i][0] = $hWnd And $SCROLL_AMOUNTS[$i][1] = $iBar Then $iID = $i ExitLoop EndIf Next
If Not $iID Then Return 0
$SCROLL_AMOUNTS[$iID][2] = $iStep
Return 1
EndFunc
Func WM_VSCROLL($hWnd, $Msg, $wParam, $lParam)
#forceref $Msg, $wParam, $lParam Local $nScrollCode = BitAND($wParam, 0x0000FFFF) Local $index = -1, $yChar, $yPos Local $Min, $Max, $Page, $Pos, $TrackPos
; Get all the vertial scroll bar information Local $tSCROLLINFO = _GUIScrollBars_GetScrollInfoEx($hWnd, $SB_VERT) $Min = DllStructGetData($tSCROLLINFO, "nMin") $Max = DllStructGetData($tSCROLLINFO, "nMax") $Page = DllStructGetData($tSCROLLINFO, "nPage") ; Save the position for comparison later on $yPos = DllStructGetData($tSCROLLINFO, "nPos") $Pos = $yPos $TrackPos = DllStructGetData($tSCROLLINFO, "nTrackPos")
$iRound = 0
for $i = 1 to UBound($SCROLL_AMOUNTS)-1 If $SCROLL_AMOUNTS[$i][0] = $hWnd And $SCROLL_AMOUNTS[$i][1] = $SB_VERT Then $iRound = $SCROLL_AMOUNTS[$i][2] EndIf Next
if Not $iRound Then Return $GUI_RUNDEFMSG
Switch $nScrollCode Case $SB_TOP ; user clicked the HOME keyboard key DllStructSetData($tSCROLLINFO, "nPos", $Min)
Case $SB_BOTTOM ; user clicked the END keyboard key DllStructSetData($tSCROLLINFO, "nPos", $Max)
Case $SB_LINEUP ; user clicked the top arrow DllStructSetData($tSCROLLINFO, "nPos", $Pos - $iRound)
Case $SB_LINEDOWN ; user clicked the bottom arrow DllStructSetData($tSCROLLINFO, "nPos", $Pos + $iRound)
Case $SB_PAGEUP ; user clicked the scroll bar shaft above the scroll box DllStructSetData($tSCROLLINFO, "nPos", $Pos - $Page)
Case $SB_PAGEDOWN ; user clicked the scroll bar shaft below the scroll box DllStructSetData($tSCROLLINFO, "nPos", $Pos + $Page)
Case $SB_THUMBTRACK ; user dragged the scroll box DllStructSetData($tSCROLLINFO, "nPos", Round($TrackPos/$iRound)*$iRound) EndSwitch
;~ // Set the position and then retrieve it. Due to adjustments ;~ // by Windows it may not be the same as the value set.
DllStructSetData($tSCROLLINFO, "fMask", $SIF_POS) _GUIScrollBars_SetScrollInfo($hWnd, $SB_VERT, $tSCROLLINFO) _GUIScrollBars_GetScrollInfo($hWnd, $SB_VERT, $tSCROLLINFO) ;// If the position has changed, scroll the window and update it $Pos = DllStructGetData($tSCROLLINFO, "nPos")
If ($Pos <> $yPos) Then _GUIScrollBars_ScrollWindow($hWnd, 0, $yPos - $Pos) EndIf
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_VSCROLL
Func WM_HSCROLL($hWnd, $Msg, $wParam, $lParam)
#forceref $Msg, $wParam, $lParam Local $nScrollCode = BitAND($wParam, 0x0000FFFF) Local $index = -1, $yChar, $yPos Local $Min, $Max, $Page, $Pos, $TrackPos
; Get all the vertial scroll bar information Local $tSCROLLINFO = _GUIScrollBars_GetScrollInfoEx($hWnd, $SB_HORZ) $Min = DllStructGetData($tSCROLLINFO, "nMin") $Max = DllStructGetData($tSCROLLINFO, "nMax") $Page = DllStructGetData($tSCROLLINFO, "nPage") ; Save the position for comparison later on $yPos = DllStructGetData($tSCROLLINFO, "nPos") $Pos = $yPos $TrackPos = DllStructGetData($tSCROLLINFO, "nTrackPos")
$iRound = 0
for $i = 1 to UBound($SCROLL_AMOUNTS)-1 If $SCROLL_AMOUNTS[$i][0] = $hWnd And $SCROLL_AMOUNTS[$i][1] = $SB_HORZ Then $iRound = $SCROLL_AMOUNTS[$i][2] EndIf Next
if Not $iRound Then Return $GUI_RUNDEFMSG
Switch $nScrollCode Case $SB_TOP ; user clicked the HOME keyboard key DllStructSetData($tSCROLLINFO, "nPos", $Min)
Case $SB_BOTTOM ; user clicked the END keyboard key DllStructSetData($tSCROLLINFO, "nPos", $Max)
Case $SB_LINEUP ; user clicked the top arrow DllStructSetData($tSCROLLINFO, "nPos", $Pos - $iRound)
Case $SB_LINEDOWN ; user clicked the bottom arrow DllStructSetData($tSCROLLINFO, "nPos", $Pos + $iRound)
Case $SB_PAGEUP ; user clicked the scroll bar shaft above the scroll box DllStructSetData($tSCROLLINFO, "nPos", $Pos - $Page)
Case $SB_PAGEDOWN ; user clicked the scroll bar shaft below the scroll box DllStructSetData($tSCROLLINFO, "nPos", $Pos + $Page)
Case $SB_THUMBTRACK ; user dragged the scroll box DllStructSetData($tSCROLLINFO, "nPos", Round($TrackPos/$iRound)*$iRound) EndSwitch
;~ // Set the position and then retrieve it. Due to adjustments ;~ // by Windows it may not be the same as the value set.
DllStructSetData($tSCROLLINFO, "fMask", $SIF_POS) _GUIScrollBars_SetScrollInfo($hWnd, $SB_HORZ, $tSCROLLINFO) _GUIScrollBars_GetScrollInfo($hWnd, $SB_HORZ, $tSCROLLINFO) ;// If the position has changed, scroll the window and update it $Pos = DllStructGetData($tSCROLLINFO, "nPos")
If ($Pos <> $yPos) Then _GUIScrollBars_ScrollWindow($hWnd, $yPos - $Pos, 0) EndIf
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_HSCROLL
Func WM_SIZE($hWnd, $Msg, $wParam, $lParam) #forceref $Msg, $wParam Local $index = -1, $yChar, $xChar, $xClientMax, $xClient, $yClient, $ivMax For $x = 0 To UBound($aSB_WindowInfo) - 1 If $aSB_WindowInfo[$x][0] = $hWnd Then $index = $x $xClientMax = $aSB_WindowInfo[$index][1] $xChar = $aSB_WindowInfo[$index][2] $yChar = $aSB_WindowInfo[$index][3] $ivMax = $aSB_WindowInfo[$index][7] ExitLoop EndIf Next If $index = -1 Then Return 0
Local $tSCROLLINFO = DllStructCreate($tagSCROLLINFO)
; Retrieve the dimensions of the client area. $xClient = BitAND($lParam, 0x0000FFFF) $yClient = BitShift($lParam, 16) $aSB_WindowInfo[$index][4] = $xClient $aSB_WindowInfo[$index][5] = $yClient
; Set the vertical scrolling range and page size DllStructSetData($tSCROLLINFO, "fMask", BitOR($SIF_RANGE, $SIF_PAGE)) DllStructSetData($tSCROLLINFO, "nMin", 0) DllStructSetData($tSCROLLINFO, "nMax", $ivMax) DllStructSetData($tSCROLLINFO, "nPage", $yClient / $yChar) _GUIScrollBars_SetScrollInfo($hWnd, $SB_VERT, $tSCROLLINFO)
; Set the horizontal scrolling range and page size DllStructSetData($tSCROLLINFO, "fMask", BitOR($SIF_RANGE, $SIF_PAGE)) DllStructSetData($tSCROLLINFO, "nMin", 0) DllStructSetData($tSCROLLINFO, "nMax", 2 + $xClientMax / $xChar) DllStructSetData($tSCROLLINFO, "nPage", $xClient / $xChar) _GUIScrollBars_SetScrollInfo($hWnd, $SB_HORZ, $tSCROLLINFO)
Return $GUI_RUNDEFMSG EndFunc ;==>WM_SIZE
And the example
AutoIt
#include <WindowsConstants.au3>#include <GuiconstantsEx.au3>#include <Timers.au3>#include <Array.au3>#Include"GUIScroll.au3"$Caption="Choice"$ChoiceArray=StringSplit("Choice 1|Choice 2|Choice 3|Choice 4|Choice 5|Choice 6|Choice 7|Choice 8|Choice 9|Choice 10|Choice 11|Choice 12|Choice 13|Choice 14|Choice 15","|")$GUIWidth=150$LabelWidth=130$GUIXPOS=-1$GUIYPOS=-1$LabelText="Make your choice"$SelectionType=0$MaxHeight=250$ReturnArray=_ChoiceBox($Caption,$ChoiceArray,$SelectionType,$LabelText,$GUIWidth,$LabelWidth,$GUIXPOS,$GUIYPOS,$MaxHeight)_ArrayDisplay($ReturnArray,"Choice")Func_ChoiceBox($Caption,$ChoiceArray,$SelectionType,$LabelText="",$GUIWidth=150,$LabelWidth=130,$GUIXPOS=-1,$GUIYPOS=-1,$MaxHeight=250)Dim$SelectionArray[$ChoiceArray[0]+1]Dim$YPOS=10If$LabelText=""Then$GUIHeight=($ChoiceArray[0]*20+40)If$GUIHeight>$MaxHeightThen$GUIHeight=$MaxHeight$GUI=GUICreate($Caption,$GUIWidth,$GUIHeight,$GUIXPOS,$GUIYPOS)Else$GUIHeight=($ChoiceArray[0]*20+70)If$GUIHeight>$MaxHeightThen$GUIHeight=$MaxHeight+70$GUI=GUICreate($Caption,$GUIWidth,$GUIHeight,$GUIXPOS,$GUIYPOS)GUICtrlCreateLabel($LabelText,10,10,$LabelWidth,20)Dim$YPOS=30EndIf
Scrollbar_Create($GUI,$SB_VERT,$GUIHeight+100); But the actual window is 700 pixels high
Scrollbar_Step(1,$GUI,$SB_VERT); Scrolls per 20 pixels. If not set the default is 1 (smooth scrolling)For$i=1To$ChoiceArray[0]Switch$SelectionTypeCase0$SelectionArray[$i]=GUICtrlCreateCheckbox($ChoiceArray[$i],10,$YPOS,$LabelWidth,20)Case1$SelectionArray[$i]=GUICtrlCreateRadio($ChoiceArray[$i],10,$YPOS,$LabelWidth,20)CaseElseEndSwitch$YPOS=$YPOS+20Next$BtnOk=GUICtrlCreateButton("&OK",5,$YPOS+5,80,20)GUISetState()While1$msg=GUIGetMsg()Switch$msgCase$GUI_EVENT_CLOSEReturn0Case$BtnOkDim$SelectedFor$i=1To$ChoiceArray[0]IfGUICtrlRead($SelectionArray[$i])=$GUI_CHECKEDThen$Selected=$Selected&GUICtrlRead($SelectionArray[$i],1)&"|"EndIfNext$Selected=StringTrimRight($Selected,1)$Array=StringSplit($Selected,"|")ReturnStringSplit($Selected,"|")EndSwitchWEndEndFunc
Edited by mrRevoked, 30 September 2008 - 01:21 AM.