View Single Post
Old 06-20-2014, 07:13 AM
dexmix dexmix is offline

* Expert *
Join Date: Oct 2002
Posts: 476
Default Setting the FlexGrid Scrollbars to indicate visible page sizes...

I know I've seen posts elsewhere asking how to do this, with some API suggestions - but never code posted: so here's mine.

SetScrollPageSize needs to be called whenever
*the data on a grid is refreshed/populated
*on any form/grid resize,
*and on any column resize event if you allow column sizing.

I personally subclass the grid to get a column resize and then PostMessage a WM_SIZE event back to the form.

Private Const SB_HORZ As Long = 0
Private Const SB_VERT As Long = 1
Private Const SIF_RANGE As Long = 1
Private Const SIF_PAGE As Long = 2

    cbSize As Long
    fMask As Long
    nMin As Long
    nMax As Long
    nPage As Long
    nPos As Long
    nTrackPos As Long
End Type

Private Declare Function SetScrollInfo Lib "user32" (ByVal hWnd As Long, ByVal fnBar As Long, ByRef lpsi As SCROLLINFO, ByVal fRedraw As Boolean) As Long

Public Function FlexPageSize(Grid As MSHFlexGrid) As Long
    FlexPageSize = Grid.Height \ Grid.RowHeight(0) - Grid.FixedRows
End Function

Public Function FlexPageSizeHorizontal(Grid As MSHFlexGrid) As Long
    Const EXTRA_PAD As Long = 1 'extra padding / undersizing to make sure we can scroll to last column

    Dim i As Long
    Dim ColCount As Long
    For i = Grid.LeftCol To Grid.Cols - Grid.FixedCols
        If Not Grid.ColIsVisible(i) Then
            FlexPageSizeHorizontal = Max(ColCount - EXTRA_PAD, 0)
            Exit Function
        End If
        ColCount = ColCount + 1
    Next i
    FlexPageSizeHorizontal = Max(ColCount - 1 - EXTRA_PAD, 0)
End Function

' SetScrollPageSize
' Overrides a flexgrids scrollbars to properly indicate the grid's view/page size
Public Function SetScrollPageSize(Grid As MSHFlexGrid) As Long
    Dim si As SCROLLINFO
    si.cbSize = Len(si)
    si.fMask = SIF_PAGE Or SIF_RANGE
    si.nMin = Grid.FixedCols
    si.nMax = Grid.Cols - Grid.FixedCols
    si.nPage = Max(FlexPageSizeHorizontal(Grid) - 1, 1)
    SetScrollPageSize = SetScrollInfo(Grid.hWnd, SB_HORZ, si, True)

    si.nMin = Grid.FixedRows
    si.nMax = Grid.Rows - Grid.FixedRows
    si.nPage = Max(FlexPageSize(Grid) - 1, 1)
    SetScrollPageSize = SetScrollInfo(Grid.hWnd, SB_VERT, si, True)
End Function

Last edited by dexmix; 06-20-2014 at 09:10 AM. Reason: swapped vert/horz SetScroll
Reply With Quote