Option Strict Off
Option Explicit On
'------------------------------------ 2023/11/09 ------------------------------------
Public Class 五clsScrolRectWnd

#Region "宣言"
    Private Const MeIDS As String = "五clsScrolRectWnd"
    Private mSpaceXY As Integer
    Private mPichX As Integer
    Private mPichY As Integer
    Private mSizeY As Integer
    Private mMax As Integer
    Private mCols As Integer
    Private mRows As Integer
    Private mRowsVisible As Integer
    Private mPPshowLoadHeight As Integer
#End Region

#Region "New ....Finalize"
    Public Sub New()
        MyBase.New()
        mSpaceXY = 6 : mPichX = 36 : mPichY = 36 : mSizeY = 32
    End Sub
    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub
#End Region

#Region "関数"
    Friend Function PichX() As Integer
        Return mPichX
    End Function
    Friend Function PichY() As Integer
        Return mPichY
    End Function
    Friend Function ItemWD() As Integer
        Return mPichX - mSpaceXY
    End Function
    Friend Function ItemHT() As Integer
        Return mPichY - mSpaceXY
    End Function
    Friend Function SpaceXY() As Integer
        Return mSpaceXY
    End Function
    Friend Function CurColCount() As Integer
        Return mCols
    End Function

    Friend Function ItemRectSizeRetRect(ByRef Index As Integer, ByRef rc As RECT) As Boolean
        Dim rrr, lll, ttt, bbb As Integer
        ItemRectSizeRetRect = False
        If ItemRectSize(Index, lll, ttt, rrr, bbb) Then
            Call SetRect(rc, lll, ttt, rrr, bbb)
            Return True
        End If
        Return False
    End Function

    Friend Function ItemRectSize(ByRef Index As Integer, ByRef lll As Integer, ByRef ttt As Integer, ByRef rrr As Integer, ByRef bbb As Integer) As Boolean
        ItemRectSize = False
        If GetPostXY(Index, lll, ttt) Then
            rrr = lll + mPichX - mSpaceXY
            bbb = ttt + mPichY - mSpaceXY
            Return True
        End If
        Return False
    End Function

    Friend Function StartSizeSeting(ByRef pctBack As Panel, ByRef pctShow As PictureBox, ByRef vsbScroBar As VScrollBar, Optional ByRef SizeX As Object = 32, Optional ByRef SizeY As Object = 32, Optional ByRef SpaceXY As Object = 4) As Boolean
        Dim hh, ww As Integer
        hh = Math.Max(40, pctBack.ClientRectangle.Height)
        ww = Math.Max(40, pctBack.ClientRectangle.Width - vsbScroBar.Width)
        pctShow.SetBounds(0, 0, ww, hh, BoundsSpecified.All)
        vsbScroBar.SetBounds(pctShow.Width, 0, vsbScroBar.Width, hh, BoundsSpecified.All)
        mRowsVisible = 2
        mPPshowLoadHeight = pctShow.Height
        '-----------------------------------------------------------
        If SizeX <> -1 Then 'ReSize　の呼び出しではこれは実行しない。-1 の場合。
            mSpaceXY = SpaceXY
            mPichX = SizeX + SpaceXY
            mPichY = SizeY + SpaceXY
            mSizeY = SizeY
        End If
        mMax = 0
        Return VscbarSet(pctBack, vsbScroBar, True)
    End Function

    Friend Function SetSize(ByRef pctBack As Panel, ByRef pctShow As PictureBox, ByRef vsbScroBar As VScrollBar, ByRef max As Integer, ByRef BarReset As Boolean, Optional ByRef BarSizeSet As Boolean = True) As Boolean
        Try
            'max = 1 to n
            mCols = (pctShow.ClientRectangle.Width - mSpaceXY) \ mPichX
            If mCols <= 1 Then
                mCols = 1
                mPichX = pctShow.ClientRectangle.Width - mSpaceXY
            End If
            mRowsVisible = (pctBack.ClientRectangle.Height - mSpaceXY) \ mPichY
            If max > 0 Then
                mRows = ((max - 1) \ mCols) + 1 'max = 1 to n
                mPPshowLoadHeight = mSpaceXY * 2 + mRows * mPichY + mPichY 'add space for pctBack height <> seting just size
            Else
                mRows = 0
                mPPshowLoadHeight = pctBack.ClientRectangle.Height
            End If
            pctShow.Height = Math.Max(pctBack.ClientRectangle.Height, mPPshowLoadHeight)
            mMax = max
            If BarSizeSet Then Call VscbarSet(pctBack, vsbScroBar, BarReset)
            Return True
        Catch ex As Exception
            MsgBox("ERROR in SetSize()" & vbCrLf & ex.ToString)
            Return False
        End Try
    End Function

    Friend Function VscbarPageUpDown(ByRef Up1Down2 As Integer, ByRef vsbScroBar As VScrollBar) As Boolean
        Try
            With vsbScroBar
                If Not .Enabled Then Exit Function
                If Up1Down2 = 1 Then 'UP
                    .Value = Math.Min(0, .Value + mRowsVisible)
                Else
                    .Value = Math.Max((.Maximum - .LargeChange + 1), .Value - mRowsVisible)
                End If
            End With
            Return True
        Catch ex As Exception
            MsgBox("ERROR in VscbarPageUpDown()" & vbCrLf & ex.ToString)
            Return False
        End Try
    End Function

    Friend Function VscbarSet(ByRef pctBack As Panel, ByRef vsbScroBar As VScrollBar, ByRef BarReset As Boolean) As Boolean
        Try
            Dim max As Integer
            Dim e As Boolean
            '-----------------------------------------------------------
            vsbScroBar.SetBounds(pctBack.ClientRectangle.Width - vsbScroBar.Width, 0, vsbScroBar.Width, Math.Max(50, pctBack.ClientRectangle.Height))
            '-----------------------------------------------------------
            If mPPshowLoadHeight > pctBack.ClientRectangle.Height Then max = Math.Max(mRows - mRowsVisible, 0) Else max = 0
            e = (max > 0)
            With vsbScroBar
                .Enabled = e
                If e Then
                    Dim n As Integer = mRowsVisible \ 2
                    .SmallChange = 1 : .LargeChange = n
                    .Minimum = 0 : .Maximum = max + n
                Else
                    .SmallChange = 0 : .LargeChange = 0
                    .Minimum = 0 : .Maximum = 0
                End If
                If (e) And (BarReset) And (.Value > 0) Then .Value = 0
            End With
            Return True
        Catch ex As Exception
            MsgBox("ERROR in VscbarSet()" & vbCrLf & ex.ToString)
            Return False
        End Try
    End Function

    Friend Function GetPostAndSize(ByRef rIndex As Integer, ByRef wleft As Integer, ByRef wTop As Integer, ByRef wWidth As Integer, ByRef wHeight As Integer, Optional ByRef UserColumn As Integer = 0) As Boolean
        'アイコン位置のサイズからスペース分を追加して
        'Left / ttt - (mSpaceXY \ 2)
        'Right / bbb + (mSpaceXY \ 2)
        'のサイズを返す｡
        Dim X, Y As Integer
        If GetPostXY(rIndex, X, Y, UserColumn) Then
            wleft = X - (mSpaceXY \ 2)
            ' qqqqq CStr(wleft)
            wTop = Y - (mSpaceXY \ 2)
            wWidth = mPichX
            wHeight = mPichY
            Return True
        Else
            Return False
        End If
    End Function

    Friend Function GetPostXY(ByRef Index As Integer, ByRef X As Integer, ByRef Y As Integer, Optional ByRef UserColumn As Integer = 0) As Boolean
        Dim b, a, col As Integer
        If Index < 1 Or Index > mMax Then Return False
        If UserColumn > 0 Then col = UserColumn Else col = mCols
        a = Index - 1 'index = 1 to n
        b = a \ col
        X = (a - (b * col)) * mPichX + mSpaceXY
        Y = b * mPichY + mSpaceXY
        Return True
    End Function

    Friend Function HitTest(ByRef X As Integer, ByRef Y As Integer, Optional ByRef UserColumn As Integer = 0, Optional ByRef UserRows As Integer = 0) As Integer
        Dim col, x1, a, y1, row As Integer
        If UserColumn > 0 Then col = UserColumn Else col = mCols
        If UserRows > 0 Then row = UserRows Else row = mRows
        If X >= mSpaceXY And X < mSpaceXY + mPichX * col And Y >= mSpaceXY And Y < mSpaceXY + mPichY * row Then
            y1 = (Y - mSpaceXY) \ mPichY
            x1 = (X - mSpaceXY) \ mPichX
            a = y1 * col + x1
            a = a + 1 'index = 1 to n
            If a <= mMax Then Return a
        End If
        Return 0
    End Function

    Friend Function Event_VscbarChange(ByRef pctShow As PictureBox, ByRef vsbScroBar As VScrollBar) As Boolean
        Try
            Dim a As Integer
            a = Math.Max(0, vsbScroBar.Value * mPichY)
            pctShow.Top = -a  'vsbScroBar value = 0 to +nnn  /  pctShow top = 0 to -nnn
            Call ⅩSetFocusControl(pctShow)
            Return True
        Catch ex As Exception
            MsgBox(ErrorToString())
            Return False
        End Try
    End Function

#End Region

End Class