Sign in to follow this  
Followers 0

_ChoiceBox

9 posts in this topic

Posted

A simple ChoiceBox.

What do you think about it? :)

post-34381-1222679275_thumb.gif

Add to UDF???

#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 = 10
	
	If $LabelText = "" Then 
		GUICreate($Caption, $GUIWidth, ($ChoiceArray[0] * 20 + 40), $GUIXPOS, $GUIYPOS)	
	Else
		GUICreate($Caption, $GUIWidth, ($ChoiceArray[0] * 20 + 70), $GUIXPOS, $GUIYPOS)
		GUICtrlCreateLabel($LabelText,10,10,$LabelWidth,20)
		Dim $YPOS = 30
	EndIf
		
	For $i = 1 To $ChoiceArray[0]
		Switch $SelectionType
			Case 0
				$SelectionArray[$i] = GUICtrlCreateCheckbox	($ChoiceArray[$i], 10, $YPOS, $LabelWidth, 20)
			Case 1
				$SelectionArray[$i] = GUICtrlCreateRadio	($ChoiceArray[$i], 10, $YPOS, $LabelWidth, 20)
			Case Else
				Return 0
		EndSwitch
		$YPOS = $YPOS + 20
	Next
	
	$BtnOk = GUICtrlCreateButton("&OK", 5, $YPOS + 5, 80, 20)
	
	GUISetState()
	
	While 1
		$msg = GUIGetMsg()
		Switch $msg
			Case $GUI_EVENT_CLOSE
				Return 0

			Case $BtnOk
				Dim $Selected
				For $i = 1 To $ChoiceArray[0]
					If GUICtrlRead($SelectionArray[$i]) = $GUI_CHECKED Then
						$Selected = $Selected & GUICtrlRead($SelectionArray[$i], 1) & "|"
					EndIf
				Next
				$Selected = StringTrimRight($Selected, 1)
				Return StringSplit($Selected, "|")
		EndSwitch
	WEnd
EndFunc   ;==>_ChoiceBox

Share this post


Link to post
Share on other sites



Posted

Nice Newbie script man! Keep up the good work and learn more and more!

Share this post


Link to post
Share on other sites

Posted

Nice Newbie script man! Keep up the good work and learn more and more!

your still alive.

And so am i :)

Share this post


Link to post
Share on other sites

Posted

Nice Newbie script man! Keep up the good work and learn more and more!

Ok, let me show your modified code and teach me!!!

Share this post


Link to post
Share on other sites

Posted (edited)

I made something like this once using a TreeView ctrl. Here's my version.

#include-once
#include <GuiTreeView.au3>
#include <GUIConstants.au3>
#include <Array.au3> ;only used for this example _ArrayDisplay

Dim $items[4]=["First","Second","Third","Forth"]

$results = _MakeChoice($items,"These Items")
_ArrayDisplay($results)


Func _MakeChoice($arrItems,$title)
	If Not IsArray($arrItems) then Return 0
	Dim $tTreeArr[1]
	$tGui = GUICreate($title,250,326,-1,-1,"","")
	$tTree = GUICtrlCreateTreeView(5,5,235,258,BitOr(256,55))
	$tSelectAll = GUICtrlCreateButton("Select All",85,270,75,20)
	$tDeSelectAll = GUICtrlCreateButton("Deselect All",165,270,75,20)
	$tOKBtn = GUICtrlCreateButton("OK",5,270,75,20)
		GUICtrlSetState(-1,$GUI_FOCUS)
	
	For $i = 0 to UBound($arrItems) - 1
		$tTreeArr[$i] = GUICtrlCreateTreeViewItem($arrItems[$i],$tTree)
		If $i < UBound($arrItems)-1 Then ReDim $tTreeArr[$i + 2]
	Next
	
	$tVarTreeCount = _GUICtrlTreeView_GetCount($tTree)
	GUISetState(@SW_SHOW,$tGui)
	
	While 1
		$msgSelection = GUIGetMsg()
		
		If $msgSelection = $tSelectAll Then
			For $i = 0 to $tVarTreeCount - 1
				GUICtrlSetState($tTreeArr[$i],$GUI_CHECKED)
			Next
		EndIf
		
		If $msgSelection = $tDeSelectAll Then
			For $i = 0 to $tVarTreeCount - 1
				GUICtrlSetState($tTreeArr[$i],$GUI_UNCHECKED)
			Next
		EndIf
		
		If $msgSelection = $tOKBtn Then
			Local $arrSelectedHosts[1]=[0]
			$x = 0
			For $i = 0 to $tVarTreeCount -1
				If BitAnd(GUICtrlRead($tTreeArr[$i]),$GUI_CHECKED) Then
					$x += 1
					ReDim $arrSelectedHosts[$x + 1]
					$arrSelectedHosts[$x] = GUICtrlRead($tTreeArr[$i],1)
				EndIf
			Next
			If Ubound($arrSelectedHosts) > 1 then $arrSelectedHosts[0] = Ubound($arrSelectedHosts)-1
			GUIDelete($tGui)
			Return $arrSelectedHosts
		EndIf		
	WEnd
EndFunc

Edited by spudw2k

Share this post


Link to post
Share on other sites

Posted

Good job, BartvanBeek.

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

Kip made nice set of Scroll UDF's.

Share this post


Link to post
Share on other sites

Posted (edited)

Ok.. Here's a quick example.

Posted Image

You'll need the Scroll UDF.

Here's a modifed version (includes fixes from the post)

CODE

#cs ----------------------------------------------------------------------------

AutoIt Version: 3.2.13.3 (beta)

Author: Kip

Script Function:

Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

#cs

Functions:

Scrollbar_Create($hWnd, $iBar, $iMax)

Scrollbar_Scroll($hWnd, $iBar, $iPos)

Scrollbar_GetPos($hWnd, $iBar)

Scrollbar_Step($iStep, $hWnd=0, $iBar=0)

#CE

#Include <GuiScrollBars.au3>

#include<GuiconstantsEx.au3>

#include<WindowsConstants.au3>

#include <ScrollBarConstants.au3>

Global $SCROLL_AMOUNTS[1][3]

$SCROLL_AMOUNTS[0][0] = 1

func Scrollbar_Create($hWnd, $iBar, $iMax)

Local $Size = WinGetClientSize($hWnd)

If $iBar = $SB_HORZ Then

$Size = $Size[0]

ElseIf $iBar = $SB_VERT Then

$Size = $Size[1]

Else

Return 0

EndIf

ReDim $SCROLL_AMOUNTS[uBound($SCROLL_AMOUNTS)+1][3]

$SCROLL_AMOUNTS[uBound($SCROLL_AMOUNTS)-1][0] = $hWnd

$SCROLL_AMOUNTS[uBound($SCROLL_AMOUNTS)-1][1] = $iBar

$SCROLL_AMOUNTS[uBound($SCROLL_AMOUNTS)-1][2] = $SCROLL_AMOUNTS[0][0]

_GUIScrollBars_EnableScrollBar($hWnd, $iBar)

_GUIScrollBars_SetScrollRange($hWnd, $iBar, 0,$iMax-1)

_GUIScrollBars_SetScrollInfoPage($hWnd, $iBar, $Size)

GUIRegisterMsg($WM_VSCROLL, "WM_VSCROLL")

GUIRegisterMsg($WM_HSCROLL, "WM_HSCROLL")

GUIRegisterMsg($WM_SIZE, "WM_SIZE")

Return $iMax

EndFunc

Func Scrollbar_GetPos($hWnd, $iBar)

Local $tSCROLLINFO = _GUIScrollBars_GetScrollInfoEx($hWnd, $iBar)

Return DllStructGetData($tSCROLLINFO, "nPos")

EndFunc

Func Scrollbar_Scroll($hWnd, $iBar, $iPos)

Local $tSCROLLINFO = _GUIScrollBars_GetScrollInfoEx($hWnd, $iBar)

$iCurrentPos = DllStructGetData($tSCROLLINFO, "nPos")

DllStructSetData($tSCROLLINFO, "nPos", $iPos)

DllStructSetData($tSCROLLINFO, "fMask", $SIF_POS)

_GUIScrollBars_SetScrollInfo($hWnd, $iBar, $tSCROLLINFO)

If $iBar = $SB_VERT Then

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

If Not $iRound Then Return 0

_GUIScrollBars_ScrollWindow($hWnd, Round(($iCurrentPos-$iPos)/$iRound)*$iRound, 0)

Else

Return 0

EndIf

Return 1

EndFunc

Func Scrollbar_Step($iStep, $hWnd=0, $iBar=0)

If not $hWnd or Not $iBar Then

$SCROLL_AMOUNTS[0][0] = $iStep

Return 1

EndIf

$iID = 0

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

#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 = 10

	If $LabelText = "" Then
		$GUIHeight = ($ChoiceArray[0] * 20 + 40)
		If $GUIHeight > $MaxHeight Then $GUIHeight = $MaxHeight
		$GUI = GUICreate($Caption, $GUIWidth, $GUIHeight, $GUIXPOS, $GUIYPOS) 
	Else
		$GUIHeight = ($ChoiceArray[0] * 20 + 70)
		If $GUIHeight > $MaxHeight Then $GUIHeight = $MaxHeight + 70
		$GUI = GUICreate($Caption, $GUIWidth, $GUIHeight, $GUIXPOS, $GUIYPOS)
		GUICtrlCreateLabel($LabelText,10,10,$LabelWidth,20)
		Dim $YPOS = 30
	EndIf

	
	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 = 1 To $ChoiceArray[0]
		Switch $SelectionType
			Case 0
				$SelectionArray[$i] = GUICtrlCreateCheckbox ($ChoiceArray[$i], 10, $YPOS, $LabelWidth, 20)
			Case 1
				$SelectionArray[$i] = GUICtrlCreateRadio    ($ChoiceArray[$i], 10, $YPOS, $LabelWidth, 20)
			Case Else

		EndSwitch
		$YPOS = $YPOS + 20
	Next

	$BtnOk = GUICtrlCreateButton("&OK", 5, $YPOS + 5, 80, 20)

	GUISetState()
		

	While 1
		$msg = GUIGetMsg()
		Switch $msg
			Case $GUI_EVENT_CLOSE
				Return 0

			Case $BtnOk
				Dim $Selected
				For $i = 1 To $ChoiceArray[0]
					If GUICtrlRead($SelectionArray[$i]) = $GUI_CHECKED Then
						$Selected = $Selected & GUICtrlRead($SelectionArray[$i], 1) & "|"
					EndIf
				Next
				$Selected = StringTrimRight($Selected, 1)
				$Array = StringSplit($Selected, "|")
				Return StringSplit($Selected, "|")
		EndSwitch
	WEnd
EndFunc
Edited by mrRevoked

Share this post


Link to post
Share on other sites

Posted

Thanks mrRevoked, and kip. :) Good stuff. >_

Share this post


Link to post
Share on other sites

Posted

spudw2k and mrRevoked thanks for the code, i like it!!!

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