Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleCreate a four-way vertical and horizontal splitter (paned window) using only VB
Keywordssplitter, paned window, sash
CategoriesControls
 
Thanks to John Stevens for the initial version of this program.

Place four PictureBoxes on the form separated by a small distance vertically and horizontally.

In the form's move event handler, see if a drag is in progress. If it is, rearrange the PictureBoxes appropriately.

If no drag is in progress, set the mouse pointer to something appropriate (up/down, left/right, or resize all arraws).

When the user clicks and drags on the form, make the form's event handlers adjust the sizes and positions of the PictureBoxes.

 
Private Const SPLITTER_HEIGHT = 40
Private Const SPLITTER_WIDTH = 40

' Minimum allowed sizes for any PictureBox.
Private Const MIN_HORIZONTAL = 10
Private Const MIN_VERTICAL = 10

' Drag directions.
Private Const DRAG_NONE = 0
Private Const DRAG_HORIZONTAL = 1
Private Const DRAG_VERTICAL = 2
Private Const DRAG_BOTH = 3

' The percentage occupied by the PictureBox.
Private PercentageHorizontal As Single
Private PercentageVertical As Single

' True when we are dragging the splitter.
Private DragDir As Integer

' Arrange the controls on the form.
Private Sub ArrangeControls()
Dim hgt1 As Single
Dim hgt2 As Single
Dim wid1 As Single
Dim wid2 As Single

    ' Don't bother if we're iconized.
    If WindowState = vbMinimized Then Exit Sub

    hgt1 = (ScaleHeight - SPLITTER_HEIGHT) * _
        PercentageHorizontal
    wid1 = (ScaleWidth - SPLITTER_WIDTH) * _
        PercentageVertical
    hgt2 = (ScaleHeight - SPLITTER_HEIGHT) - hgt1
    wid2 = (ScaleWidth - SPLITTER_WIDTH) - wid1
    picNW.Move 0, 0, wid1, hgt1
    picSW.Move 0, hgt1 + SPLITTER_HEIGHT, wid1, hgt2
    picNE.Move wid1 + SPLITTER_WIDTH, 0, wid2, hgt1
    picSE.Move wid1 + SPLITTER_WIDTH, hgt1 + _
        SPLITTER_HEIGHT, wid2, hgt2
End Sub

' Return drag flags indicating the area at this point.
Private Function OverDragArea(ByVal X As Single, ByVal Y As _
    Single)
Dim over As Integer

    over = DRAG_NONE
    If X > picNW.Width And X < picNE.Left _
        Then over = over Or DRAG_HORIZONTAL
    If Y > picNW.Height And Y < picSW.Top _
        Then over = over Or DRAG_VERTICAL
    OverDragArea = over
End Function

Private Sub Form_MouseDown(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    DragDir = OverDragArea(X, Y)
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
Dim over As Integer

    If DragDir = DRAG_NONE Then
        ' Set the correct arrow type.
        over = OverDragArea(X, Y)
        Select Case over
            Case DRAG_HORIZONTAL
                MousePointer = vbSizeWE
            Case DRAG_VERTICAL
                MousePointer = vbSizeNS
            Case DRAG_BOTH
                MousePointer = vbSizeNWSE
        End Select
        Exit Sub
    End If

    If DragDir And DRAG_VERTICAL Then
        If Y < MIN_VERTICAL Then
            Y = MIN_VERTICAL
        ElseIf ScaleHeight - Y < MIN_VERTICAL Then
            Y = ScaleHeight - MIN_VERTICAL
        End If
        PercentageHorizontal = Y / ScaleHeight
    End If

    If DragDir And DRAG_HORIZONTAL Then
        If X < MIN_HORIZONTAL Then
            X = MIN_HORIZONTAL
        ElseIf ScaleWidth - X < MIN_HORIZONTAL Then
            X = ScaleWidth - MIN_HORIZONTAL
        End If
        PercentageVertical = X / ScaleWidth
    End If

    ArrangeControls
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    DragDir = DRAG_NONE
End Sub

Private Sub Form_Resize()
    ArrangeControls
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated