Floating Toolbar

rbulph

Junior Contributor
Joined
Feb 17, 2003
Messages
397
Anyone figured out how to provide a floating toolbar in conjunction with ToolStripPanels? I'm using four ToolStripPanels rather than a ToolStripContainer because it's an MDI form and the ToolStripContainer doesn't seem to allow the display of MDI children.
 
Well here's some code which works reasonably well. The main problems outstanding are that (i) the user can't drag the toolbar over a docking position onto the other side. Once it's docked it's stuck, and (ii) the toolbar form doesn't an activated caption.

Hopefully this is useful to someone, and if anyone has any suggestions for improvement, I'd be glad to hear them.

You will need two forms, Form1 being the startup form. It contains toolstrippanels on each side and a toolstrip. Toolstrippanels don't seem to appear in the toolbox so I had to add them manually with code in the form's designer.

PHP:
Imports System
Imports System.Windows.Forms
Imports Microsoft.WindowsCE.Forms

Public Class Form1

    Friend toolbarformsetup As Boolean
    Dim frmToolbar As Form2

    Private Sub ToolStrip1_EndDrag(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolStrip1.EndDrag

        Dim InPanel As Boolean
        For Each c As Control In Me.Controls
            If TypeOf c Is ToolStripPanel Then
                If c.Bounds.Contains(Me.PointToClient(MousePosition)) Then InPanel = True
            End If
        Next

        If Not InPanel Then
            tspTop.Controls.Remove(Me.ToolStrip1)
            frmToolbar = New Form2
            frmToolbar.Controls.Add(Me.ToolStrip1)
            ToolStrip1.Location = New Point(0, 0)
            ToolStrip1.GripStyle = ToolStripGripStyle.Hidden

            frmToolbar.Show(Me)
            frmToolbar.Location = MousePosition
            toolbarformsetup = True
            frmToolbar.Activate()

        End If

    End Sub

    Friend Sub ReviewToolbar()

        Dim p As Point = PointToClient(MousePosition)
        Dim border As Integer = (Width - ClientSize.Width) / 2
        Dim ClientTop As Integer = Height - ClientSize.Height - border

        If toolbarformsetup And Me.Bounds.Contains(MousePosition) Then
            Dim RelTSP As ToolStripPanel = Nothing
            Dim ThinDimension As Integer = Math.Min(ToolStrip1.Width, ToolStrip1.Height)
            If p.Y > ClientTop And p.Y < ClientTop + ThinDimension Then
                RelTSP = Me.tspTop
            ElseIf p.Y > Height - border - ThinDimension Then
                RelTSP = Me.tspBottom
            ElseIf p.X < border + ThinDimension Then
                RelTSP = Me.tspLeft
            ElseIf p.X > Width - border - ThinDimension Then
                RelTSP = Me.tspRight
            End If

            If Not RelTSP Is Nothing Then
                frmToolbar.Controls.Remove(ToolStrip1)
                RelTSP.Join(ToolStrip1)
                ToolStrip1.GripStyle = ToolStripGripStyle.Visible
                If RelTSP Is Me.tspTop Or RelTSP Is Me.tspBottom Then
                    ToolStrip1.Left = p.X - border
                Else
                    ToolStrip1.Top = p.Y - ClientTop
                End If

                Activate()
                frmToolbar.Close()
                toolbarformsetup = False
            End If

        End If

    End Sub

    'I want to have the toolbar show an active caption at the same time as the main form, but I haven't been successful in 
    'getting this to work.
    'Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal n As Long, ByVal w As Long, ByVal k As Long) As Long
    Const WM_NCACTIVATE = &H86
    'Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
    '    '   Dim msg As System.Windows.Forms.Message = _
    '    If Not p Is Nothing Then

    '        SendMessage(p.Handle.ToInt32, WM_NCACTIVATE, 1, 0)
    '        '   Dim msg As Microsoft.WindowsCE.Forms.Message = Microsoft.WindowsCE.Forms.Message.Create(p.Handle.ToInt32, _
    '        '    &H86, 1, 0) ', New IntPtr(e.X), New IntPtr(e.Y))

    '        '  Microsoft.WindowsCE.Forms.MessageWindow.SendMessage(msg)
    '    End If
    'End Sub

End Class

Public Class Form2
    'form must be TopMost, fixed toolwindow, autosize grow and shrink and have no maximise button (it won't show, but double-clicking will maximise it)
    'nor have minimize button nor show in toolbar.

    Private Sub Form2_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move

        Form1.ReviewToolbar()

    End Sub

End Class
 
Back
Top