' ***********************************************************************
' Author : Elektro
' Modified : 03-31-2014
' ***********************************************************************
' <copyright file="ControlsForm.vb" company="Elektro Studios">
' Copyright (c) Elektro Studios. All rights reserved.
' </copyright>
' *************************************************************************
''' <summary>
''' Class ControlsForm.
''' Invisible Form designed to host Controls.
''' </summary>
Public Class ControlsForm : Inherits Form
#Region " Variables & Properties "
''' <summary>
''' Indicates whether the Form and it's controls are moveable.
''' </summary>
Private _Moveable As Boolean = False
''' <summary>
''' Indicates whether the moveable events are handled
''' </summary>
Private MoveableIsHandled As Boolean = False
''' <summary>
''' Boolean Flag that indicates whether the Form should be moved.
''' </summary>
Private MoveFormFlag As Boolean = False
''' <summary>
''' Indicates the position where to move the form.
''' </summary>
Private MoveFormPosition As Point = Nothing
''' <summary>
''' Gets or sets a value indicating whether this <see cref="ControlsForm"/> and it's controls are movable.
''' </summary>
''' <value><c>true</c> if controls are movable; otherwise, <c>false</c>.</value>
Public Property Moveable As Boolean
Get
Return Me._Moveable
End Get
Set(ByVal value As Boolean)
Me._Moveable = value
Dim Pan As Panel =
(From p As Panel In MyBase.Controls.OfType(Of Panel)()
Where p.Tag = Me.Handle).First
Select Case value
Case True ' Add Moveable Events to EventHandler.
If Not Me.MoveableIsHandled Then ' If not Moveable handlers are already handled then...
For Each c As Control In Pan.Controls
AddHandler c.MouseDown, AddressOf MouseDown
AddHandler c.MouseUp, AddressOf MouseUp
AddHandler c.MouseMove, AddressOf MouseMove
Next c
Me.MoveableIsHandled = True
End If
Case False ' Remove Moveable Events from EventHandler.
If Me.MoveableIsHandled Then ' If Moveable handlers are already handled then...
For Each c As Control In Pan.Controls
RemoveHandler c.MouseDown, AddressOf MouseDown
RemoveHandler c.MouseUp, AddressOf MouseUp
RemoveHandler c.MouseMove, AddressOf MouseMove
Next c
Me.MoveableIsHandled = False
End If
End Select
End Set
End Property
#End Region
#Region " Constructors "
''' <summary>
''' Prevents a default instance of the <see cref="ControlsForm"/> class from being created.
''' </summary>
Private Sub New()
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="ControlsForm" /> class.
''' Constructor Overload to display a collection of controls.
''' </summary>
''' <param name="Controls">The control array to display in the Formulary.</param>
''' <param name="FormLocation">The default Formulary location.</param>
Public Sub New(ByVal [Controls] As Control(),
Optional ByVal FormLocation As Point = Nothing)
' InitializeComponent call.
MyBase.SuspendLayout()
MyBase.Name = "ControlsForm"
' Adjust Form size.
MyBase.ClientSize = New Size(0, 0)
MyBase.AutoSize = True
MyBase.AutoSizeMode = AutoSizeMode.GrowAndShrink
' Set the Transparency properties.
MyBase.AllowTransparency = True
MyBase.BackColor = Color.Fuchsia
MyBase.TransparencyKey = Color.Fuchsia
MyBase.DoubleBuffered = False
' Is not necessary to display borders, icon, and taskbar, hide them.
MyBase.FormBorderStyle = BorderStyle.None
MyBase.ShowIcon = False
MyBase.ShowInTaskbar = False
' Instance a Panel to add our controls.
Dim Pan As New Panel
With Pan
' Suspend the Panel layout.
Pan.SuspendLayout()
' Set the Panel properties.
.Name = "ControlsForm Panel"
.Tag = Me.Handle
.AutoSize = True
.AutoSizeMode = AutoSizeMode.GrowAndShrink
.BorderStyle = BorderStyle.None
.Dock = DockStyle.Fill
End With
' Add our controls inside the Panel.
Pan.Controls.AddRange(Controls)
' Add the Panel inside the Form.
MyBase.Controls.Add(Pan)
' If custom Form location is set then...
If Not FormLocation.Equals(Nothing) Then
' Set the StartPosition to manual relocation.
MyBase.StartPosition = FormStartPosition.Manual
' Relocate the form.
MyBase.Location = FormLocation
End If
' Resume layouts.
Pan.ResumeLayout(False)
MyBase.ResumeLayout(False)
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="ControlsForm" /> class.
''' Constructor Overload to display a single control.
''' </summary>
''' <param name="Control">The control to display in the Formulary.</param>
''' <param name="FormLocation">The default Formulary location.</param>
Public Sub New(ByVal [Control] As Control,
Optional ByVal FormLocation As Point = Nothing)
Me.New({[Control]}, FormLocation)
End Sub
#End Region
#Region " Event Handlers "
''' <summary>
''' Handles the MouseDown event of the Form and it's controls.
''' </summary>
''' <param name="sender">The source of the event.</param>
''' <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
Private Shadows Sub MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) _
Handles MyBase.MouseDown
If e.Button = MouseButtons.Left Then
Me.MoveFormFlag = True
Me.Cursor = Cursors.NoMove2D
Me.MoveFormPosition = e.Location
End If
End Sub
''' <summary>
''' Handles the MouseMove event of the Form and it's controls.
''' </summary>
''' <param name="sender">The source of the event.</param>
''' <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
Private Shadows Sub MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) _
Handles MyBase.MouseMove
If Me.MoveFormFlag Then
Me.Location += (e.Location - Me.MoveFormPosition)
End If
End Sub
''' <summary>
''' Handles the MouseUp event of the Form and it's controls.
''' </summary>
''' <param name="sender">The source of the event.</param>
''' <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
Private Shadows Sub MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) _
Handles MyBase.MouseUp
If e.Button = MouseButtons.Left Then
Me.MoveFormFlag = False
Me.Cursor = Cursors.Default
End If
End Sub
#End Region
End Class