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
 
 
 
 
 
 
TitleLet the user drag a toolbar between a form's edges
DescriptionThis example shows how to let the user drag a toolbar between a form's edges in Visual Basic 6. It uses the ToolBar's MouseDown, MouseMove, and MouseUp events to see where the user is dragging the ToolBar and makes the control align on the appropriate edge.
Keywordstoolbar, drag, dock
CategoriesControls
 
When the ToolBar receives a MouseDown event, the program sets DraggingToolbar = True to indicate that a drag is in progress.

When the ToolBar receives a MouseMove event, the program checks DraggingToolbar. If a drag is in progress, the program calls function MousePosition to see where the ToolBar should be positioned. If the position has changed, the program sets the control's new Align value appropriately. If the compiler constant IMMEDIATE_UPDATE is True, the program also calls PositionPanel to reposition the PictureBox covering the rest of the form.

When the ToolBar receives a MouseUp event, the program stops dragging the control. If IMMEDIATE_UPDATE is False, then the PictureBox covering the rest of the form was not updated in the MouseMove event handler so the program does that now.

 
#Const IMMEDIATE_UPDATE = False

Private DraggingToolbar As Boolean

' Let the user drag the toolbar to another
' part of the form.
Private Sub tbrTools_MouseDown(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    DraggingToolbar = True
End Sub

Private Sub tbrTools_MouseMove(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
Dim new_position As Integer

    If Not DraggingToolbar Then Exit Sub

    ' See where the ToolBar should be aligned.
    new_position = MousePosition(X, Y)
    If tbrTools.Align <> new_position Then
       tbrTools.Align = new_position
    End If

    #If IMMEDIATE_UPDATE Then
        ' Position the panel.
        PositionPanel
    #End If
End Sub

' Reposition the toolbar.
Private Sub tbrTools_MouseUp(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    If Not DraggingToolbar Then Exit Sub
    DraggingToolbar = False

    #If Not IMMEDIATE_UPDATE Then
        ' Position the panel.
        PositionPanel
    #End If
End Sub
 
Function MousePosition returns the alignment that the ToolBar should have for a particular mouse position. It aligns the ToolBar to the form's edge that is closest to the mouse position.
 
' Return the Align constant for this mouse position.
Private Function MousePosition(ByVal X As Single, ByVal Y _
    As Single) As Integer
Const GAP = 360

    ' Convert the coordinates into form
    ' coordinates.
    X = X + tbrTools.Left
    Y = Y + tbrTools.Top

    ' Keep the position on the form.
    If X < 0 Then X = 0
    If X > ScaleWidth Then X = ScaleWidth
    If Y < 0 Then Y = 0
    If Y > ScaleHeight Then Y = ScaleHeight

    If X * ScaleHeight < Y * ScaleWidth Then
        ' Left or bottom.
        If ScaleHeight - Y < X Then
            ' Bottom.
            MousePosition = vbAlignBottom
        Else
            ' Left.
            MousePosition = vbAlignLeft
        End If
    Else
        ' Right or top.
        If ScaleWidth - X < Y Then
            ' Right.
            MousePosition = vbAlignRight
        Else
            ' Top.
            MousePosition = vbAlignTop
        End If
    End If
End Function
 
Subroutine PositionPanel makes the picPanel PictureBox fill the form not occupied by the ToolBar. It then draws an ellipse in picPanel so it is easy to tell where the PictureBox is.
 
' Position the panel that covers the parts
' of the form not covered by the toolbar.
Private Sub PositionPanel()
Dim wid As Single
Dim hgt As Single

    Select Case tbrTools.Align
        Case vbAlignBottom
            hgt = ScaleHeight - tbrTools.Height
            If hgt < 120 Then hgt = 120
            picPanel.Move 0, 0, ScaleWidth, hgt
        Case vbAlignTop
            hgt = ScaleHeight - tbrTools.Height
            If hgt < 120 Then hgt = 120
            picPanel.Move 0, tbrTools.Height, ScaleWidth, _
                hgt
        Case vbAlignLeft
            wid = ScaleWidth - tbrTools.Width
            If wid < 120 Then wid = 120
            picPanel.Move tbrTools.Width, 0, wid, _
                ScaleHeight
        Case vbAlignRight
            wid = ScaleWidth - tbrTools.Width
            If wid < 120 Then wid = 120
            picPanel.Move 0, 0, wid, ScaleHeight
    End Select

    ' Draw an ellipse to make it easier to see.
    picPanel.Cls
    wid = picPanel.ScaleWidth - picPanel.ScaleX(1, _
        vbPixels, vbTwips)
    hgt = picPanel.ScaleHeight - picPanel.ScaleX(1, _
        vbPixels, vbTwips)
    If wid > hgt Then
        picPanel.Circle (wid / 2, hgt / 2), _
            wid / 2, vbRed, _
            , , hgt / wid
    Else
        picPanel.Circle (wid / 2, hgt / 2), _
            hgt / 2, vbRed, _
            , , hgt / wid
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated