Adding the following code will add a sizable, draggable, panel to the toolbox. Although in this instance, a panel is used. It just as easily could have been any other windo
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class ResizeablePanel
Inherits Panel
#Region "Resize"
Private mDragging As Boolean
Private mMousePos As Point
Private mSize As Size
Public Sub New()
Me.BackColor = Color.FromKnownColor(KnownColor.ButtonShadow)
End Sub
Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)
If (mDragging) Then
Me.Width = mSize.Width + e.X - mMousePos.X
Me.Height = mSize.Height + e.Y - mMousePos.Y
Me.Invalidate()
Else
If (e.X >= Me.Width - 8 AndAlso e.Y >= Me.Height - 8) Then
Me.Cursor = Cursors.SizeNWSE
Else
Me.Cursor = Cursors.Default
End If
End If
MyBase.OnMouseMove(e)
End Sub
Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
mDragging = IIf(Me.Cursor = Cursors.SizeNWSE, True, False)
mMousePos = e.Location
mSize = Me.Size
MyBase.OnMouseDown(e)
End Sub
Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
mDragging = False
MyBase.OnMouseUp(e)
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim rc As Rectangle = New Rectangle(Me.Width - 16, Me.Height - 16, 16, 16)
ControlPaint.DrawSizeGrip(e.Graphics, Me.BackColor, rc)
MyBase.OnPaint(e)
End Sub
#End Region
#Region "Drag"
' Used to store the current cursor shape when we start
' to move the control
Private m_CurrentCursor As Cursor
' Holds the mouse position relative to the inside of
' our control when the mouse button goes down
Private m_CursorOffset As Point
' Used by the MoveMove event handler to show that the
' setup to move the control has completed
Private m_Moving As Boolean
'Public Sub New()
' ' Call the constructor of the base Label class to properly
' ' initialize our control
' MyBase.New()
' ' Setup the Cursor property so that, by default, when the
' ' mouse passes over our control, the cursor changes to a hand
' Me.Cursor = Cursors.Hand
'End Sub
Private Sub MovableLabel_MouseDown( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseDown
' Check to see if the correct button has been pressed
If Not mDragging Then
If e.Button = Windows.Forms.MouseButtons.Left Then
m_CurrentCursor = MyBase.Cursor
' replace it with our new image that says were in Movable mode
MyBase.Cursor = Cursors.SizeAll
' Save the location of the mouse pointer relative to the top-left
' corner of our control
m_CursorOffset = e.Location
' Set the mode flag to signal the MouseMove event handler that it
' needs to now calculate new positions for our control
m_Moving = True
End If
End If
End Sub
Private Sub MovableLabel_MouseMove( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseMove
' Check which mode we're in. If we're supposed to be moving
' our control
If Not mDragging Then
If m_Moving Then
MyBase.Cursor = Cursors.SizeAll
' get the screen position of the mouse pointer and map it
' to the position relative to the top-left corner of our
' parent container
Dim clientPosition As Point = _
MyBase.Parent.PointToClient(System.Windows.Forms.Cursor.Position)
' Calculate the new position of our control, maintaining
' the relative position stored by the MoveDown event
Dim adjustedLocation As New Point( _
clientPosition.X - m_CursorOffset.X, _
clientPosition.Y - m_CursorOffset.Y)
' Set the new position of our control
MyBase.Location = adjustedLocation
End If
End If
End Sub
Private Sub MovableLabel_MouseUp( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseUp
' The button was released, so we're going back to Static mode.
If Not mDragging Then
m_Moving = False
MyBase.Cursor = m_CurrentCursor
End If
End Sub
#End Region
End Class
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment