Using grips to move  
Author Message
the_hangman





PostPosted: Visual Basic Express Edition, Using grips to move Top

Ok I've been trying to do this for like 3 weeks now.

I see that there are a bunch of things such as the toolstrip and statusbars that have grips on them. The only problem is that they don't do anything. I can't seem to figure out how to access the grips so that a user could actually USE them to move the object around.

I've developed the code for using them.. Now I just have to figure out how to use the grips.. Isn't there like a ToolStripGrip.Click or something



Visual Studio Express Editions14  
 
 
Tall Dude





PostPosted: Visual Basic Express Edition, Using grips to move Top

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.De****NonUserCode()> _

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.De****StepThrough()> _

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



 
 
the_hangman





PostPosted: Visual Basic Express Edition, Using grips to move Top

I appreciate the help, but I already had a much shorter code that accomplished that exact thing.

I don't want the moving to occur when clicking on the right side. I want it to occur on the left (on the grip) so that the grip is the only part that can move it..


 
 
Tall Dude





PostPosted: Visual Basic Express Edition, Using grips to move Top

http://www.windowsforms.com/FAQs/default.aspx PageID=3&CategoryID=3&SubcategoryID=40

Says:

"A ToolStrip can be "movable" if it is placed in a ToolStripPanel and it's GripStyle is set to Visible"

I put a toolstrip in a toolstripcontainer, topstrippanel, and it's grip worked.

I think my first post missed the whole gist of what you were asking.



 
 
the_hangman





PostPosted: Visual Basic Express Edition, Using grips to move Top

Hrm.. thanks. That's what I needed to know. Unfortunately, It can only be moved around within the docking panels of the container.. I needed it be free flowing so you could drag it anywhere on the screen.

thanks anyway


 
 
Tall Dude





PostPosted: Visual Basic Express Edition, Using grips to move Top

I really don't know.

But if you want to work on the angle of the first code I posted.

The following additional code makes it work much better.

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim ctl As ToolStripItem

For Each ctl In ToolStrip1.Items

AddHandler ctl.MouseMove, AddressOf pb1_MouseMove

AddHandler ctl.MouseDown, AddressOf pb1_MouseDown

AddHandler ctl.MouseUp, AddressOf pb1_MouseUp

Next

End Sub



 
 
the_hangman





PostPosted: Visual Basic Express Edition, Using grips to move Top

It's cool. I think it turned out to be a lot easier to just draw my own grips and use them along with a code similar to the one you posted