The following is not the best of code examples.
I modified some test code that moved a picturebox around the form.
It took me a while to get it to move a toolstrip.
Some of the changes that mattered seemed to be:
ToolStrip Dock = none
ToolStrip Autosize= false (to make the toolstrip larger than it's contents)
(Grab the toolstrip by the right side)
---------------------------------------------------------------------------------------------------
< Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.ToolStrip1 = New System.Windows.Forms.ToolStrip
Me.ToolStripLabel1 = New System.Windows.Forms.ToolStripLabel
Me.ToolStripLabel2 = New System.Windows.Forms.ToolStripLabel
Me.ToolStripLabel3 = New System.Windows.Forms.ToolStripLabel
Me.ToolStrip1.SuspendLayout()
Me.SuspendLayout()
'
'ToolStrip1
'
Me.ToolStrip1.Anchor = System.Windows.Forms.AnchorStyles.None
Me.ToolStrip1.AutoSize = False
Me.ToolStrip1.Dock = System.Windows.Forms.DockStyle.None
Me.ToolStrip1.GripMargin = New System.Windows.Forms.Padding(20)
Me.ToolStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ToolStripLabel1, Me.ToolStripLabel2, Me.ToolStripLabel3})
Me.ToolStrip1.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.HorizontalStackWithOverflow
Me.ToolStrip1.Location = New System.Drawing.Point(36, 68)
Me.ToolStrip1.Margin = New System.Windows.Forms.Padding(90)
Me.ToolStrip1.Name = "ToolStrip1"
Me.ToolStrip1.Size = New System.Drawing.Size(533, 25)
Me.ToolStrip1.TabIndex = 3
Me.ToolStrip1.Text = "ToolStrip1"
'
'ToolStripLabel1
'
Me.ToolStripLabel1.Name = "ToolStripLabel1"
Me.ToolStripLabel1.Size = New System.Drawing.Size(80, 22)
Me.ToolStripLabel1.Text = "ToolStripLabel1"
'
'ToolStripLabel2
'
Me.ToolStripLabel2.Name = "ToolStripLabel2"
Me.ToolStripLabel2.Size = New System.Drawing.Size(80, 22)
Me.ToolStripLabel2.Text = "ToolStripLabel2"
'
'ToolStripLabel3
'
Me.ToolStripLabel3.Name = "ToolStripLabel3"
Me.ToolStripLabel3.Size = New System.Drawing.Size(80, 22)
Me.ToolStripLabel3.Text = "ToolStripLabel3"
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(717, 266)
Me.Controls.Add(Me.ToolStrip1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ToolStrip1.ResumeLayout(False)
Me.ToolStrip1.PerformLayout()
Me.ResumeLayout(False)
End Sub
Friend WithEvents ToolStrip1 As System.Windows.Forms.ToolStrip
Friend WithEvents ToolStripLabel1 As System.Windows.Forms.ToolStripLabel
Friend WithEvents ToolStripLabel2 As System.Windows.Forms.ToolStripLabel
Friend WithEvents ToolStripLabel3 As System.Windows.Forms.ToolStripLabel
End Class
---------------------------------------------------------------------------------------------------------------------------
Option Strict On
Imports System.Net
Public Class Form1
Dim MDown As Boolean
Dim Formloc As Point
Dim ToolStripLoc As Point
Dim CaptionHeight As Integer = System.Windows.Forms.SystemInformation.CaptionHeight
Private Sub pb1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ToolStrip1.MouseDown
'pb1 is picture box
If My.Computer.Mouse.ButtonsSwapped Then
If e.Button <> Windows.Forms.MouseButtons.Right Then Exit Sub
Else
If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
End If
ToolStripLoc.X = ( Me.Location.X + ToolStrip1.Location.X) - cp(Formloc, CaptionHeight, ToolStripLoc, True).X
ToolStripLoc.Y = (( Me.Location.Y + ToolStrip1.Location.Y) - CaptionHeight) - cp(Formloc, CaptionHeight, ToolStripLoc, True).Y
MDown = True
End Sub
Private Sub pb1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ToolStrip1.MouseMove
If MDown Then ToolStrip1.Location = cp(Formloc, CaptionHeight, ToolStripLoc)
End Sub
Private Shared Function cp(ByVal FormLoc As Point, ByVal capheight As Integer, ByVal ToolStrip1loc As Point, Optional ByVal Absolute As Boolean = False) As Point
Dim pt As Point = Cursor.Position
If Not Absolute Then
cp.X = pt.X - (FormLoc.X - ToolStrip1loc.X)
cp.Y = pt.Y - (FormLoc.Y - ToolStrip1loc.Y) + capheight
Else
cp = Cursor.Position
End If
End Function
Private Sub pb1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ToolStrip1.MouseUp, Me.MouseUp
MDown = False
End Sub
Private Sub Form1_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
Formloc = Me.Location
End Sub
End Class
|