Jump to content

Adjust scrollbar range


Recommended Posts

What's the relation between scrollbar range and the GUI size? I have a script like below where I create randomly a number of labels in a child window and I create a scrollbar that eventually should fit all the child window content but I don't see the relation between whatever unit is scrollbar using and the GUI content.

#include <GuiScrollBars.au3>
#include <ScrollBarConstants.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <WinAPI.au3>

Global $iPos = 0
$iRandom = Random(1, 15, 1)

$hMain = GUICreate("Main", 1000, 700)
$cPosDisplay = GUICtrlCreateLabel("", 10, 10, 200, 30, BitOR($SS_SUNKEN, $SS_CENTERIMAGE, $SS_CENTER))
$cNumOfLabels = GUICtrlCreateLabel($iRandom, 10, 50, 200, 30, BitOR($SS_SUNKEN, $SS_CENTERIMAGE, $SS_CENTER))
$hChild = GUICreate("", 500, 700, 250, 0, $WS_POPUP, $WS_EX_MDICHILD, $hMain)
For $Index = 1 To $iRandom
    GUICtrlCreateLabel($Index, 10, 10 + ($Index - 1) * 500, 480, 480, BitOR($SS_SUNKEN, $SS_CENTERIMAGE, $SS_CENTER))
Next
GUISetState(@SW_SHOW, $hMain)
GUISetState(@SW_SHOW, $hChild)

GUIRegisterMsg($WM_VSCROLL, 'WM_VSCROLL')

$hVScroll = _GUIScrollBars_Init($hChild)
_GUIScrollBars_SetScrollRange($hChild, $SB_VERT, 0, 700) ; << -- 
_GUIScrollBars_ShowScrollBar($hChild, $SB_HORZ, False)

Do
    Sleep(10)
Until GUIGetMsg() = -3

Func WM_VSCROLL($hWnd, $iMsg, $wParam, $lParam)
    Switch _WinAPI_LoWord($wParam)
        Case $SB_THUMBPOSITION
            $iPos = _WinAPI_HiWord($wParam)
    EndSwitch
    GUICtrlSetData($cPosDisplay, $iPos)
    _GUIScrollBars_SetScrollInfoPos($hChild, $SB_VERT, $iPos)
EndFunc

 

When the words fail... music speaks
Link to post
Share on other sites
  • Moderators

Andreik,

Scrollbars work in character height and width units - not at all intuitive. Take a look at my Scrollbars UDF (the link  is in my sig) and you will see how I went about trying to get them to work - or just use the UDF itself.

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 post
Share on other sites

I know what you mean Zedna but it's not detailed info, it's basic info in my opinion and as Melba already pointed it's not intuitive at all. When you're looking in help file for example at _GUIScrollBars_Init() where you have two parameters and the only details are max size of horizontal/vertical scrollbar then you may can ignore such a description that is not helpful anyway since you don't have a clue what this size really is. All I want to say is that a hint would make things clear. Anyway I figured it out thanks to Melba's hint.

When the words fail... music speaks
Link to post
Share on other sites
  • 4 weeks later...

@Melba23 I have one more question regarding scroll bars. What's the proper way to set scroll bar position to a certain position? Based on your UDF I wrote this function to create a scroll bar for a window to fit all controls and then a function to scroll to a certain control but it's very tedious and the scrolling effect is awful.

#include <GuiScrollBars.au3>
#include <ScrollBarConstants.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <WinAPI.au3>

$iRandom = Random(8, 20, 1)
$iScrollTo = Random(1, $iRandom, 1)

$hMain = GUICreate("Main", 1000, 700)
$cScroll = GUICtrlCreateLabel($iScrollTo, 10, 10, 200, 30, BitOR($SS_SUNKEN, $SS_CENTERIMAGE, $SS_CENTER))
$cNumOfLabels = GUICtrlCreateLabel($iRandom, 10, 50, 200, 30, BitOR($SS_SUNKEN, $SS_CENTERIMAGE, $SS_CENTER))
$hChild = GUICreate("", 500, 700, 250, 0, $WS_POPUP, $WS_EX_MDICHILD, $hMain)
For $Index = 1 To $iRandom
    GUICtrlCreateLabel($Index, 10, ($Index - 1) * 480, 480, 480, BitOR($SS_SUNKEN, $SS_CENTERIMAGE, $SS_CENTER))
Next
GUISetState(@SW_SHOW, $hMain)
GUISetState(@SW_SHOW, $hChild)

GUIRegisterMsg($WM_VSCROLL, 'WM_VSCROLL')

SetWindowScrollbar($hChild, $iRandom * 480)
ScrollTo($iScrollTo)

Do
    Sleep(10)
Until GUIGetMsg() = -3

Func WM_VSCROLL($hWnd, $iMsg, $wParam, $lParam)
    #forceref $iMsg, $wParam, $lParam
    Local $nScrollCode = BitAND($wParam, 0x0000FFFF)
    Local $iIndex = -1, $yChar = 16, $yPos
    Local $Min, $Max, $Page, $Pos, $TrackPos
    Local $tSCROLLINFO = _GUIScrollBars_GetScrollInfoEx($hWnd, $SB_VERT)
    $Min = DllStructGetData($tSCROLLINFO, "nMin")
    $Max = DllStructGetData($tSCROLLINFO, "nMax")
    $Page = DllStructGetData($tSCROLLINFO, "nPage")
    $yPos = DllStructGetData($tSCROLLINFO, "nPos")
    $TrackPos = DllStructGetData($tSCROLLINFO, "nTrackPos")
    $Pos = $yPos
    Switch $nScrollCode
        Case $SB_TOP
            DllStructSetData($tSCROLLINFO, "nPos", $Min)
        Case $SB_BOTTOM
            DllStructSetData($tSCROLLINFO, "nPos", $Max)
        Case $SB_LINEUP
            DllStructSetData($tSCROLLINFO, "nPos", $Pos - 1)
        Case $SB_LINEDOWN
            DllStructSetData($tSCROLLINFO, "nPos", $Pos + 1)
        Case $SB_PAGEUP
            DllStructSetData($tSCROLLINFO, "nPos", $Pos - $Page)
        Case $SB_PAGEDOWN
            DllStructSetData($tSCROLLINFO, "nPos", $Pos + $Page)
        Case $SB_THUMBTRACK
            DllStructSetData($tSCROLLINFO, "nPos", $TrackPos)
    EndSwitch
    DllStructSetData($tSCROLLINFO, "fMask", $SIF_POS)
    _GUIScrollBars_SetScrollInfo($hWnd, $SB_VERT, $tSCROLLINFO)
    _GUIScrollBars_GetScrollInfo($hWnd, $SB_VERT, $tSCROLLINFO)
    $Pos = DllStructGetData($tSCROLLINFO, "nPos")
    If ($Pos <> $yPos) Then
        _GUIScrollBars_ScrollWindow($hWnd, 0, $yChar * ($yPos - $Pos))
        $yPos = $Pos
    EndIf
    Return 0
EndFunc

Func SetWindowScrollbar($hWnd, $iHeight)
    Local $iCharHeight = 16
    Local $aClient = WinGetClientSize($hWnd)
    If $iHeight <= $aClient[1] Then Return 0
    _GUIScrollBars_ShowScrollBar($hWnd, $SB_HORZ, False)
    _GUIScrollBars_ShowScrollBar($hWnd, $SB_VERT, True)
    Local $iPage = Floor($aClient[1] / $iCharHeight)
    Local $iMaxSize = Floor($iHeight / $iCharHeight)
    _GUIScrollBars_SetScrollInfoMax($hWnd, $SB_VERT, $iMaxSize)
    _GUIScrollBars_SetScrollInfoPage($hWnd, $SB_VERT, $iPage)
EndFunc

Func ScrollTo($iVal)
    For $Index = 1 To ($iVal - 1) * 30  ; 480 / 16 = 30
        _SendMessage($hChild, $WM_VSCROLL, $SB_LINEDOWN)
    Next
EndFunc

 

When the words fail... music speaks
Link to post
Share on other sites
  • Moderators

Andreik,

Try this;

#include <GuiScrollBars.au3>
#include <ScrollBarConstants.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <WinAPI.au3>

$iRandom = Random(8, 20, 1)
$iScrollTo = Random(1, $iRandom, 1)

$hMain = GUICreate("Main", 1000, 700)
$cScroll = GUICtrlCreateLabel($iScrollTo, 10, 10, 200, 30, BitOR($SS_SUNKEN, $SS_CENTERIMAGE, $SS_CENTER))
$cNumOfLabels = GUICtrlCreateLabel($iRandom, 10, 50, 200, 30, BitOR($SS_SUNKEN, $SS_CENTERIMAGE, $SS_CENTER))
$hChild = GUICreate("", 500, 700, 250, 0, $WS_POPUP, $WS_EX_MDICHILD, $hMain)
For $Index = 1 To $iRandom
    GUICtrlCreateLabel($Index, 10, ($Index - 1) * 480, 480, 480, BitOR($SS_SUNKEN, $SS_CENTERIMAGE, $SS_CENTER))
Next
GUISetState(@SW_SHOW, $hMain)
GUISetState(@SW_SHOW, $hChild)

GUIRegisterMsg($WM_VSCROLL, 'WM_VSCROLL')

; Get the scrollbar max position
$iMax = SetWindowScrollbar($hChild, $iRandom * 480) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Calculate the required position
ScrollTo(($iScrollTo - 1)/ $iRandom * $iMax)

Do
    Sleep(10)
Until GUIGetMsg() = -3

Func WM_VSCROLL($hWnd, $iMsg, $wParam, $lParam)
    #forceref $iMsg, $wParam, $lParam
    Local $nScrollCode = BitAND($wParam, 0x0000FFFF)
    Local $iIndex = -1, $yChar = 16, $yPos
    Local $Min, $Max, $Page, $Pos, $TrackPos
    Local $tSCROLLINFO = _GUIScrollBars_GetScrollInfoEx($hWnd, $SB_VERT)
    $Min = DllStructGetData($tSCROLLINFO, "nMin")
    $Max = DllStructGetData($tSCROLLINFO, "nMax")
    $Page = DllStructGetData($tSCROLLINFO, "nPage")
    $yPos = DllStructGetData($tSCROLLINFO, "nPos")
    $TrackPos = DllStructGetData($tSCROLLINFO, "nTrackPos")
    $Pos = $yPos
    Switch $nScrollCode
        Case $SB_TOP
            DllStructSetData($tSCROLLINFO, "nPos", $Min)
        Case $SB_BOTTOM
            DllStructSetData($tSCROLLINFO, "nPos", $Max)
        Case $SB_LINEUP
            DllStructSetData($tSCROLLINFO, "nPos", $Pos - 1)
        Case $SB_LINEDOWN
            DllStructSetData($tSCROLLINFO, "nPos", $Pos + 1)
        Case $SB_PAGEUP
            DllStructSetData($tSCROLLINFO, "nPos", $Pos - $Page)
        Case $SB_PAGEDOWN
            DllStructSetData($tSCROLLINFO, "nPos", $Pos + $Page)
        Case $SB_THUMBTRACK
            DllStructSetData($tSCROLLINFO, "nPos", $TrackPos)
    EndSwitch
    DllStructSetData($tSCROLLINFO, "fMask", $SIF_POS)
    _GUIScrollBars_SetScrollInfo($hWnd, $SB_VERT, $tSCROLLINFO)
    _GUIScrollBars_GetScrollInfo($hWnd, $SB_VERT, $tSCROLLINFO)
    $Pos = DllStructGetData($tSCROLLINFO, "nPos")
    If ($Pos <> $yPos) Then
        _GUIScrollBars_ScrollWindow($hWnd, 0, $yChar * ($yPos - $Pos))
        $yPos = $Pos
    EndIf
    Return 0
EndFunc

Func SetWindowScrollbar($hWnd, $iHeight)
    Local $iCharHeight = 16
    Local $aClient = WinGetClientSize($hWnd)
    If $iHeight <= $aClient[1] Then Return 0

    ; You need to activate the scrollbar!!!!!!!
    _GUIScrollBars_Init($hWnd) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    _GUIScrollBars_ShowScrollBar($hWnd, $SB_HORZ, False)
    _GUIScrollBars_ShowScrollBar($hWnd, $SB_VERT, True)
    Local $iPage = Floor($aClient[1] / $iCharHeight)
    Local $iMaxSize = Floor($iHeight / $iCharHeight)
    _GUIScrollBars_SetScrollInfoMax($hWnd, $SB_VERT, $iMaxSize)
    _GUIScrollBars_SetScrollInfoPage($hWnd, $SB_VERT, $iPage)

    ; Return the max size
    Return $iMaxSize ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
EndFunc

Func ScrollTo($iVal)

    ; And move the scrollbar
    _GUIScrollBars_SetScrollInfoPos($hChild, $SB_VERT, $iVal) ; <<<<<<<<<<<<<<<<<<<<
    
EndFunc

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 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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...