如何在没有窗体边框的情况下移动窗体(visual studio) [英] How to move form without form border (visual studio)

查看:152
本文介绍了如何在没有窗体边框的情况下移动窗体(visual studio)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio 2013 Express中制作一个Windows窗体应用程序。为了使应用程序看起来更具个性化和吸引力,我在应用程序中设计了这些表单。
我将表单边框样式设置为'NONE',并创建了自己的标题和退出/ min / max按钮。
然而,在运行时移动表单的唯一方法是拖动标题栏,但我无法在表单边框和标题栏消失的情况下执行此操作。
那么,是否可以移动没有标题栏和边框的表单?也许如果我有我自己的标题栏,并试图使控制表单的位置?什么是最好的做法?



谢谢 这样简单,将此代码添加到您的Form-Class中:

  #Region移动表单

'[Move Form]
'
'// By Elektro

Public MoveForm As Boolean
Public MoveForm_MousePosition As

Public Sub MoveForm_MouseDown(sender As Object,e As MouseEventArgs)处理_
MyBase.MouseDown'在此添加更多句柄(示例:PictureBox1.MouseDown)

如果e.Button = MouseButtons.Left则
MoveForm = True
Me.Cursor = Cursors.NoMove2D
MoveForm_MousePosition = e.Location
End If

End Sub

Public Sub MoveForm_MouseMove(sender As Object,e As MouseEventArgs)Handles _
MyBase.MouseMove'在这里添加更多的句柄(例如:PictureBox1.MouseMove)

如果MoveForm Then
Me.Location = Me.Location +(e.Location - MoveForm_MousePosition)
End If

End Sub

Public Sub MoveForm_MouseUp(sender As Object, e作为MouseEventArgs)处理_
MyBase.MouseUp'在此添加更多句柄(例如:PictureBox1.MouseUp)

如果e.Button = MouseButtons.Left然后
MoveForm = False
Me.Cursor = Cursors.Default
End If

End Sub

#End Region

以下是更新后的版本:

 '*** ************************************************** ****************** 
'作者:Elektro
'修改时间:15-March-2015
'******* ************************************************** **************
'< copyright file =FormDragger.vbcompany =Elektro Studios>
'版权所有(c)Elektro Studios。版权所有。
'< / copyright>
'********************************************* **************************

#区域选项语句

显式选项在
选项Strict On
选项推断关闭

#End Region

#Region使用示例

'公共班级Form1

''''< summary>
''''< see cref =FormDragger/>管理表单拖动的实例。
''''< / summary>
'Private formDragger As FormDragger = FormDragger.Empty

'Private Sub Test()处理MyBase.Shown
'Me.InitializeDrag()
'End Sub

'Private Sub Button1_Click(ByVal sender As Object,ByVal e As EventArgs)_
'Handles Button1.Click

'Me.AlternateDragEnabled(Me)

'End Sub

'Private Sub InitializeDrag()

''使用单一Form构造函数的第一种方法:
'Me.formDragger =新的FormDragger(我,启用:= True,游标:= Cursors.SizeAll)

''第二种方式,使用多个窗体构造函数:
''Me.formDragger = New FormDragger {Me,Form2,form3})

''第三种方式,使用默认构造函数,然后在集合中添加一个Form:
''Me.formDragger = New FormDragger
' 'Me.formDragger.AddForm(Me,enabled:= True,cursor:= Cursors.SizeAll)

' End Sub

''''< summary>
''''交替拖动指定的表单。
''''< / summary>
''''< param name =form>表单。< / param>
'Private Sub AlternateDragEnabled(ByVal form As Form)

'Dim formInfo As FormDragger.FormDragInfo = Me.formDragger.FindFormDragInfo(form)
'formInfo.Enabled = Not formInfo。已启用

'End Sub

'End Class

#End Region

#RegionImports

Imports System.ComponentModel

#End Region

#RegionForm Dragger

'''< summary>
'''启用或禁用在< see cref =Form/>上的运行时拖放。
'''< / summary>
Public NotInheritable类FormDragger:实现IDisposable

#Region属性

'''< summary>
'''获取一个< see cref =IEnumerable(Of Form)/>包含表单能够执行可拖动操作的集合。
'''< / summary>
'''< value>< see cref =IEnumerable(Of Form)/>。< / value>
< EditorBrowsable(EditorBrowsableState.Always)>
公共ReadOnly财产形式作为IEnumerable(Of FormDragInfo)
获得
返回Me.forms1
结束获取
结束属性
'''< summary>
'''An< see cref =IEnumerable(Of Form)/>包含表单能够执行可拖动操作的集合。
'''< / summary>
Private forms1 As IEnumerable(Of FormDragInfo)= {}

'''< summary>
'''表示一个< see cref =FormDragger/> < c> Nothing< / c> ;.
'''< / summary>
'''<值>< c>无< / c>< /值>
< EditorBrowsable(EditorBrowsableState.Always)>
公共共享ReadOnly属性Empty作为FormDragger
获取
返回Nothing
结束获取
完成属性

#End Region

#RegionTypes

'''< summary>
'''定义< see cref =Form/>的可拖动信息。
'''< / summary>
<可串行化>
Public NotInheritable Class FormDragInfo

#RegionProperties

'''< summary>
'''获取关联的< see cref =Form/>用于执行可拖动的操作。
'''< / summary>
'''< value>关联的< see cref =Form/>。< / value>
< EditorBrowsable(EditorBrowsableState.Always)>
公开ReadOnly财产表格作为表格
获得
返回表格1
结束获得
结束属性
'''< summary>
'''关联的< see cref =Form/>
'''< / summary>
< NonSerialized>
Private ReadOnly form1 As Form

'''< summary>
'''获取关联的< see cref =Form/>的名称。
'''< / summary>
'''<值>表单。< /值>
< EditorBrowsable(EditorBrowsableState.Always)>
Public ReadOnly Property Name As String
Get
如果Me.Form IsNot Nothing Then
返回Form.Name
其他
返回String.Empty
End If
End
End Property

'''< summary>
'''获取或设置一个值,该值指示是否在关联的< see cref =Form/>中启用了拖动。
'''< / summary>
'''<值>< c> true< / c>如果启用了拖动;否则,< c> false< / c>。< /值>
< EditorBrowsable(EditorBrowsableState.Always)>
作为布尔值启用的公共属性

'''< summary>
'''A< see cref =FormDragger/>包含关联的< see cref =Form/>的可拖动信息的实例实例。
'''< / summary>
'''<值>可拖动的信息。< /值>
< EditorBrowsable(EditorBrowsableState.Never)>
Public Property DragInfo As FormDragger = FormDragger.Empty

'''< summary>
'''获取或设置< see cref =Cursor/>用于拖动关联的< see cref =Form/> ;.
'''< / summary>
'''< value>< see cref =Cursor/>。< / value>
< EditorBrowsable(EditorBrowsableState.Always)>
Public Property Cursor As Cursor = Cursors.SizeAll

'''< summary>
'''获取或设置旧窗体的光标以在拖动后恢复它。
'''< / summary>
'''<值>旧窗体的光标。< / value>
< EditorBrowsable(EditorBrowsableState.Never)>
Public Property OldCursor As Cursor = Nothing

'''< summary>
'''获取或设置鼠标的初始坐标,通常< see cref =Form.MousePosition/> ;.
'''< / summary>
'''<值>初始鼠标坐标。< /值>
< EditorBrowsable(EditorBrowsableState.Never)>
公共财产InitialMouseCoords As Point = Point.Empty

'''< summary>
'''获取或设置初始< see cref =Form/>位置,通常<请参阅cref =Form.Location/> ;.
'''< / summary>
'''<值>初始位置。< /值>
< EditorBrowsable(EditorBrowsableState.Never)>
公共财产InitialLocation As Point = Point.Empty

#End Region

#Region构造函数

'''<总结>
'''初始化< see cref =FormDragInfo/>的新实例。类。
'''< / summary>
'''< param name =form>表单。< / param>
Public Sub New(ByVal form As Form)
Me.form1 = form
Me.Cursor = form.Cursor
End Sub

''' <总结>
'''阻止< see cref =FormDragInfo/>的默认实例。从创建类。
'''< / summary>
Private Sub New()
End Sub

#End Region

#Region隐藏方法

'' '< summary>
'''作为特定类型的散列函数。
'''< / summary>
< EditorBrowsable(EditorBrowsableState.Never)>
Public Shadows Function GetHashCode()As Integer
返回MyBase.GetHashCode
End Function

'''< summary>
'''获取当前实例的System.Type。
'''< / summary>
'''< returns>当前实例的确切运行时类型。< / returns>
< EditorBrowsable(EditorBrowsableState.Never)>
Public Shadows Function [GetType]()As Type
返回MyBase.GetType
End Function
$ b $'''< summary>
'''确定指定的System.Object实例是否被视为相等。
'''< / summary>
< EditorBrowsable(EditorBrowsableState.Never)>
Public Shadows Function Equals(ByVal obj As Object)As Boolean
返回MyBase.Equals(obj)
End Function
$ b $'''< summary>
'''确定指定的System.Object实例是否是相同的实例。
'''< / summary>
< EditorBrowsable(EditorBrowsableState.Never)>
Private Shadows Sub ReferenceEquals()
End Sub

'''< summary>
'''返回一个表示当前对象的字符串。
'''< / summary>
< EditorBrowsable(EditorBrowsableState.Never)>
Public Shadows Function ToString()As String
返回MyBase.ToString
End Function

#End Region

End Class

#End Region

#Region构造函数

'''< summary>
'''初始化< see cref =FormDragger/>的新实例。类。
'''< / summary>
Public Sub New()
Me.forms1 = {}
End Sub

'''< summary>
'''初始化< see cref =FormDragger/>的新实例。类。
'''< / summary>
'''< param name =form>< see cref =Form/>用于执行可拖动操作。< / param>
'''< param name =enabled>如果设置为< c> true< / c>,可以在< see cref =Form/>上拖动。 ;
'''< param name =cursor>< see cref =Cursor/>用于拖动指定的< see cref =Form/>。< / param>
Public Sub New(ByVal form As Form,
Optional enabled As Boolean = False,
Optional cursor As Cursor = Nothing)

Me.forms1 =
{
新的FormDragInfo(表单)With
{
.Enabled = enabled,
.Cursor = cursor
}
}

Me.AssocHandlers(form)

End Sub

'''< summary>
'''初始化< see cref =FormDragger/>的新实例。类。
'''< / summary>
'''< param name =forms>< see cref =Forms/>用于执行可拖动操作。< / param>
Public Sub New(ByVal forms As IEnumerable(Of Form))

Me.forms1 =(From form As Form在窗体
中选择New FormDragInfo(form))。ToArray

对于每一个表格由于表格在表格中
Me.AssocHandlers(表格)
下一个表格

结束Sub

'' '< summary>
'''初始化< see cref =FormDragger/>的新实例。类。
'''< / summary>
'''< param name =formInfo>
'''< see cref =FormDragInfo/>实例
'''包含< see cref =Form/>参考和可拖动的信息。
'''< / param>
'''< param name =mouseCoordinates>当前的鼠标坐标。< / param>
'''< param name =location>当前位置。< / param>
Private Sub New(ByVal formInfo As FormDragInfo,
ByVal mouseCoordinates As Point,
ByVal location As Point)

formInfo.InitialMouseCoords = mouseCoordinates
formInfo。 InitialLocation = location

End Sub

#End Region

#Region公共方法

'''<< ;总结>
'''添加指定的< see cref =Form/>放入可拖曳的< see cref =Forms/>采集。
'''< / summary>
'''< param name =form>< see cref =Form/>。< / param>
'''< param name =enabled>如果设置为< c> true< / c>,可以在< see cref =Form/>上拖动。 ;
'''< param name =cursor>< see cref =Cursor/>用于拖动指定的< see cref =Form/>。< / param>
'''< exception cref =System.ArgumentException>已经添加了指定的表单;;表单< / exception>
Public Function AddForm(ByVal form As Form,
Optional enabled As Boolean = False,
Optional cursor As Cursor = Nothing)As FormDragInfo

For FormInfo As FormDragInfo在Me.forms1中

如果formInfo.Form.Equals(form)Then
抛出新的ArgumentException(指定的表单已被添加。,form)
Exit Function
End If

Next formInfo

Dim newFormInfo As New FormDragInfo(form)With {.Enabled = enabled,.Cursor = cursor}
Me。 forms1 = Me.forms1.Concat({newFormInfo})
Me.AssocHandlers(表单)

返回newFormInfo

End Function

'''< summary>
'''删除指定的< see cref =Form/>来自可拖动的< see cref =Forms/>采集。
'''< / summary>
'''< param name =form>表单。< / param>
'''< exception cref =System.ArgumentException>未找到指定的表单;;表单< / exception>
Public Sub RemoveForm(ByVal form As Form)

Dim formInfoToRemove As FormDragInfo = Nothing

For FormInfo As FormDragInfo In Me.forms1

如果formInfo.Form.Equals(form)然后
formInfoToRemove = formInfo
退出对于
结束如果

下一个formInfo

如果formInfoToRemove IsNot Nothing Then

Me.forms1 =从formInfo作为FormDragInfo在Me.forms1中
其中不是formInfo是formInfoToRemove

formInfoToRemove.Enabled = False
Me.DeassocHandlers(formInfoToRemove.Form)

其他
抛出新的ArgumentException(找不到指定的表单,表单)

End If

结束Sub

'''< summary>
'''查找< see cref =FormDragInfo/>与指定的< see cref =Form/>关联的实例参考。
'''< / summary>
'''< param name =form>< see cref =Form/>。< / param>
'''< returns>< see cref =FormDragInfo/>与指定的< see cref =Form/>关联的实例参考< /回报>
Public Function FindFormDragInfo(ByVal form As Form)As FormDragInfo

返回(从formInfo As FormDragger.FormDragInfo在Me.forms1
where formInfo.Form是form).FirstOrDefault

结束功能

'''< summary>
'''查找< see cref =FormDragInfo/>与指定的< see cref =Form/>关联的实例参考。
'''< / summary>
'''< param name =name>< see cref =Form/> 。名< / PARAM>
'''< returns>< see cref =FormDragInfo/>与指定的< see cref =Form/>关联的实例参考< /回报>
Public Function FindFormDragInfo(ByVal name As String,
可选stringComparison作为StringComparison =
StringComparison.OrdinalIgnoreCase)作为FormDragInfo

返回(从formInfo As FormDragger.FormDragInfo In Me .forms1
其中formInfo.Name.Equals(name,stringComparison))。FirstOrDefault

End Function

#End Region

#地区私人方法

'''< summary>
'''关联< see cref =Form/>处理程序启用可拖动操作。
'''< / summary>
'''< param name =form>表单。< / param>
Private Sub AssocHandlers(ByVal form As Form)

AddHandler form.MouseDown,AddressOf Me.Form_MouseDown
AddHandler form.MouseUp,AddressOf Me.Form_MouseUp
AddHandler窗体。 MouseMove,AddressOf Me.Form_MouseMove
AddHandler form.MouseEnter,AddressOf Me.Form_MouseEnter
AddHandler form.MouseLeave,AddressOf Me.Form_MouseLeave

End Sub

'''< summary>
'''取消关联< see cref =Form/>处理程序禁用可拖动操作。
'''< / summary>
'''< param name =form>表单。< / param>
Private Sub DeassocHandlers(ByVal form As Form)

如果不是form.IsDisposed AndAlso不是form.Disposing然后

RemoveHandler form.MouseDown,AddressOf Me.Form_MouseDown
RemoveHandler form.MouseUp,AddressOf Me.Form_MouseUp
RemoveHandler form.MouseMove,AddressOf Me.Form_MouseMove
RemoveHandler form.MouseEnter,AddressOf Me.Form_MouseEnter
RemoveHandler form.MouseLeave,AddressOf Me。 Form_MouseLeave

End If

End Sub

'''< summary>
'''返回新位置。
'''< / summary>
'''< param name =formInfo>
'''< see cref =FormDragInfo/>实例
'''包含< see cref =Form/>参考和可拖动的信息。
'''< / param>
'''< param name =mouseCoordinates>当前的鼠标坐标。< / param>
'''<返回>新位置。< / returns>
Private Function GetNewLocation(ByVal formInfo As FormDragInfo,
ByVal mouseCoordinates As Point)As Point

返回新点(formInfo.InitialLocation.X +(mouseCoordinates.X - formInfo.InitialMouseCoords .X),
formInfo.InitialLocation.Y +(mouseCoordinates.Y - formInfo.InitialMouseCoords.Y))

End Function

#End Region

#Region隐藏方法

'''< summary>
'''作为特定类型的散列函数。
'''< / summary>
< EditorBrowsable(EditorBrowsableState.Never)>
Public Shadows Function GetHashCode()As Integer
返回MyBase.GetHashCode
End Function

'''< summary>
'''获取当前实例的System.Type。
'''< / summary>
'''< returns>当前实例的确切运行时类型。< / returns>
< EditorBrowsable(EditorBrowsableState.Never)>
Public Shadows Function [GetType]()As Type
返回MyBase.GetType
End Function
$ b $'''< summary>
'''确定指定的System.Object实例是否被视为相等。
'''< / summary>
< EditorBrowsable(EditorBrowsableState.Never)>
Public Shadows Function Equals(ByVal obj As Object)As Boolean
返回MyBase.Equals(obj)
End Function
$ b $'''< summary>
'''确定指定的System.Object实例是否是同一个实例。
'''< / summary>
< EditorBrowsable(EditorBrowsableState.Never)>
Private Shadows Sub ReferenceEquals()
End Sub

'''< summary>
'''返回一个表示当前对象的字符串。
'''< / summary>
< EditorBrowsable(EditorBrowsableState.Never)>
Public Shadows Function ToString()As String
返回MyBase.ToString
End Function
$ b $ #End Region

#RegionEvent Handlers

'''< summary>
'''处理表单的MouseEnter事件。
'''< / summary>
'''< param name =sender>事件的来源。< / param>
'''< param name =e>< see cref =EventArgs/>包含事件数据的实例< / param>
Private Sub Form_MouseEnter(ByVal sender As Object,ByVal e As EventArgs)

Dim formInfo As FormDragInfo = FindFormDragInfo(DirectCast(sender,Form))

formInfo。 OldCursor = formInfo.Form.Cursor

如果formInfo.Enabled Then
formInfo.Form.Cursor = formInfo.Cursor
'可选:
'formInfo.Form.BringToFront ()
End If

End Sub

'''< summary>
'''处理表单的MouseLeave事件。
'''< / summary>
'''< param name =sender>事件的来源。< / param>
'''< param name =e>< see cref =EventArgs/>包含事件数据的实例< / param>
Private Sub Form_MouseLeave(ByVal sender As Object,ByVal e As EventArgs)

Dim formInfo As FormDragInfo = FindFormDragInfo(DirectCast(sender,Form))

formInfo。 Form.Cursor = formInfo.OldCursor

End Sub

'''< summary>
'''处理表单的MouseDown事件。
'''< / summary>
'''< param name =sender>事件的来源。< / param>
'''< param name =e>< see cref =MouseEventArgs/>包含事件数据的实例< / param>
Private Sub Form_MouseDown(ByVal sender As Object,ByVal e As MouseEventArgs)

Dim formInfo As FormDragInfo = FindFormDragInfo(DirectCast(sender,Form))

如果formInfo .Enabled Then
formInfo.DragInfo =新的FormDragger(formInfo,Form.MousePosition,formInfo.Form.Location)
End If

End Sub

'''< summary>
'''处理表单的MouseMove事件。
'''< / summary>
'''< param name =sender>事件的来源。< / param>
'''< param name =e>< see cref =MouseEventArgs/>包含事件数据的实例< / param>
Private Sub Form_MouseMove(ByVal sender As Object,ByVal e As MouseEventArgs)

Dim formInfo As FormDragInfo = FindFormDragInfo(DirectCast(sender,Form))

如果formInfo .Enabled AndAlso(formInfo.DragInfo IsNot FormDragger.Empty)然后
formInfo.Form.Location = formInfo.DragInfo.GetNewLocation(formInfo,Form.MousePosition)
End If

End Sub

'''< summary>
'''处理表单的MouseUp事件。
'''< / summary>
'''< param name =sender>事件的来源。< / param>
'''< param name =e>< see cref =MouseEventArgs/>包含事件数据的实例< / param>
Private Sub Form_MouseUp(ByVal sender As Object,ByVal e As MouseEventArgs)

Dim formInfo As FormDragInfo = FindFormDragInfo(DirectCast(sender,Form))

formInfo。 DragInfo = FormDragger.Empty

End Sub

#End Region

#RegionIDisposable

''' <总结>
'''处理时检测多余的呼叫。
'''< / summary>
Private isDisposed As Boolean = False

'''< summary>
'''执行与释放,释放或重置非托管资源相关的应用程序定义的任务。
'''< / summary>
Public Sub Dispose()实现IDisposable.Dispose
Me.Dispose(True)
GC.SuppressFinalize(Me)
End Sub

''' <总结>
'''发布非托管和可选管理的资源。
'''< / summary>
'''< param name =IsDisposing>
'''< c> true< / c>释放托管和非托管资源;
'''< c> false< / c>只发布非托管资源。
'''< / param>
Protected Sub Dispose(ByVal isDisposing As Boolean)

如果不是Me.isDisposed那么

如果isDisposing那么

对于每个formInfo As FormDragInfo In Me.forms1

With formInfo

.Enabled = False
.OldCursor = Nothing
.DragInfo = FormDragger.Empty
。 InitialMouseCoords = Point.Empty
.InitialLocation = Point.Empty

Me.DeassocHandlers(.Form)

End with'form

下一个formInfo

Me.forms1 = Nothing

End If'IsDisposing

End If'Not Me.IsDisposed

Me.isDisposed = True

End Sub

#End Region

End Class

#End Region


I am making a windows form application in visual studio 2013 Express. In order to make the application look more customized and attractive, I designed the forms in my application. I set the form border style to 'NONE' and made my own title and exit/min/max buttons. HOWEVER, the only way to move the form at runtime is by dragging the title bar, and I cannot do this with the form border and title bar gone. So, is it possible to move the form without the title bar and border. Maybe if I have my own title bar and try to make that control the forms position? What is the best way of doing it?

Thanks

解决方案

Simple as this, add this code to your Form-Class:

#Region " Move Form "

    ' [ Move Form ]
    '
    ' // By Elektro 

    Public MoveForm As Boolean
    Public MoveForm_MousePosition As Point

    Public Sub MoveForm_MouseDown(sender As Object, e As MouseEventArgs) Handles _
    MyBase.MouseDown ' Add more handles here (Example: PictureBox1.MouseDown)

        If e.Button = MouseButtons.Left Then
            MoveForm = True
            Me.Cursor = Cursors.NoMove2D
            MoveForm_MousePosition = e.Location
        End If

    End Sub

    Public Sub MoveForm_MouseMove(sender As Object, e As MouseEventArgs) Handles _
    MyBase.MouseMove ' Add more handles here (Example: PictureBox1.MouseMove)

        If MoveForm Then
            Me.Location = Me.Location + (e.Location - MoveForm_MousePosition)
        End If

    End Sub

    Public Sub MoveForm_MouseUp(sender As Object, e As MouseEventArgs) Handles _
    MyBase.MouseUp ' Add more handles here (Example: PictureBox1.MouseUp)

        If e.Button = MouseButtons.Left Then
            MoveForm = False
            Me.Cursor = Cursors.Default
        End If

    End Sub

#End Region

Here is an updated version:

' ***********************************************************************
' Author   : Elektro
' Modified : 15-March-2015
' ***********************************************************************
' <copyright file="FormDragger.vb" company="Elektro Studios">
'     Copyright (c) Elektro Studios. All rights reserved.
' </copyright>
' ***********************************************************************

#Region " Option Statements "

Option Explicit On
Option Strict On
Option Infer Off

#End Region

#Region " Usage Examples "

'Public Class Form1

'    ''' <summary>
'    ''' The <see cref="FormDragger"/> instance that manages the form(s) dragging.
'    ''' </summary>
'    Private formDragger As FormDragger = FormDragger.Empty

'    Private Sub Test() Handles MyBase.Shown
'        Me.InitializeDrag()
'    End Sub

'    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
'    Handles Button1.Click

'        Me.AlternateDragEnabled(Me)

'    End Sub

'    Private Sub InitializeDrag()

'        ' 1st way, using the single-Form constructor:
'        Me.formDragger = New FormDragger(Me, enabled:=True, cursor:=Cursors.SizeAll)

'        ' 2nd way, using the multiple-Forms constructor:
'        ' Me.formDragger = New FormDragger({Me, Form2, form3})

'        ' 3rd way, using the default constructor then adding a Form into the collection:
'        ' Me.formDragger = New FormDragger
'        ' Me.formDragger.AddForm(Me, enabled:=True, cursor:=Cursors.SizeAll)

'    End Sub

'    ''' <summary>
'    ''' Alternates the dragging of the specified form.
'    ''' </summary>
'    ''' <param name="form">The form.</param>
'    Private Sub AlternateDragEnabled(ByVal form As Form)

'        Dim formInfo As FormDragger.FormDragInfo = Me.formDragger.FindFormDragInfo(form)
'        formInfo.Enabled = Not formInfo.Enabled

'    End Sub

'End Class

#End Region

#Region " Imports "

Imports System.ComponentModel

#End Region

#Region " Form Dragger "

''' <summary>
''' Enable or disable drag at runtime on a <see cref="Form"/>.
''' </summary>
Public NotInheritable Class FormDragger : Implements IDisposable

#Region " Properties "

    ''' <summary>
    ''' Gets an <see cref="IEnumerable(Of Form)"/> collection that contains the Forms capables to perform draggable operations.
    ''' </summary>
    ''' <value>The <see cref="IEnumerable(Of Form)"/>.</value>
    <EditorBrowsable(EditorBrowsableState.Always)>
    Public ReadOnly Property Forms As IEnumerable(Of FormDragInfo)
        Get
            Return Me.forms1
        End Get
    End Property
    ''' <summary>
    ''' An <see cref="IEnumerable(Of Form)"/> collection that contains the Forms capables to perform draggable operations.
    ''' </summary>
    Private forms1 As IEnumerable(Of FormDragInfo) = {}

    ''' <summary>
    ''' Represents a <see cref="FormDragger"/> instance that is <c>Nothing</c>.
    ''' </summary>
    ''' <value><c>Nothing</c></value>
    <EditorBrowsable(EditorBrowsableState.Always)>
    Public Shared ReadOnly Property Empty As FormDragger
        Get
            Return Nothing
        End Get
    End Property

#End Region

#Region " Types "

    ''' <summary>
    ''' Defines the draggable info of a <see cref="Form"/>.
    ''' </summary>
    <Serializable>
    Public NotInheritable Class FormDragInfo

#Region " Properties "

        ''' <summary>
        ''' Gets the associated <see cref="Form"/> used to perform draggable operations.
        ''' </summary>
        ''' <value>The associated <see cref="Form"/>.</value>
        <EditorBrowsable(EditorBrowsableState.Always)>
        Public ReadOnly Property Form As Form
            Get
                Return form1
            End Get
        End Property
        ''' <summary>
        ''' The associated <see cref="Form"/>
        ''' </summary>
        <NonSerialized>
        Private ReadOnly form1 As Form

        ''' <summary>
        ''' Gets the name of the associated <see cref="Form"/>.
        ''' </summary>
        ''' <value>The Form.</value>
        <EditorBrowsable(EditorBrowsableState.Always)>
        Public ReadOnly Property Name As String
            Get
                If Me.Form IsNot Nothing Then
                    Return Form.Name
                Else
                    Return String.Empty
                End If
            End Get
        End Property

        ''' <summary>
        ''' Gets or sets a value indicating whether drag is enabled on the associated <see cref="Form"/>.
        ''' </summary>
        ''' <value><c>true</c> if drag is enabled; otherwise, <c>false</c>.</value>
        <EditorBrowsable(EditorBrowsableState.Always)>
        Public Property Enabled As Boolean

        ''' <summary>
        ''' A <see cref="FormDragger"/> instance instance containing the draggable information of the associated <see cref="Form"/>.
        ''' </summary>
        ''' <value>The draggable information.</value>
        <EditorBrowsable(EditorBrowsableState.Never)>
        Public Property DragInfo As FormDragger = FormDragger.Empty

        ''' <summary>
        ''' Gets or sets the <see cref="Cursor"/> used to drag the associated <see cref="Form"/>.
        ''' </summary>
        ''' <value>The <see cref="Cursor"/>.</value>
        <EditorBrowsable(EditorBrowsableState.Always)>
        Public Property Cursor As Cursor = Cursors.SizeAll

        ''' <summary>
        ''' Gets or sets the old form's cursor to restore it after dragging.
        ''' </summary>
        ''' <value>The old form's cursor.</value>
        <EditorBrowsable(EditorBrowsableState.Never)>
        Public Property OldCursor As Cursor = Nothing

        ''' <summary>
        ''' Gets or sets the initial mouse coordinates, normally <see cref="Form.MousePosition"/>.
        ''' </summary>
        ''' <value>The initial mouse coordinates.</value>
        <EditorBrowsable(EditorBrowsableState.Never)>
        Public Property InitialMouseCoords As Point = Point.Empty

        ''' <summary>
        ''' Gets or sets the initial <see cref="Form"/> location, normally <see cref="Form.Location"/>.
        ''' </summary>
        ''' <value>The initial location.</value>
        <EditorBrowsable(EditorBrowsableState.Never)>
        Public Property InitialLocation As Point = Point.Empty

#End Region

#Region " Constructors "

        ''' <summary>
        ''' Initializes a new instance of the <see cref="FormDragInfo"/> class.
        ''' </summary>
        ''' <param name="form">The form.</param>
        Public Sub New(ByVal form As Form)
            Me.form1 = form
            Me.Cursor = form.Cursor
        End Sub

        ''' <summary>
        ''' Prevents a default instance of the <see cref="FormDragInfo"/> class from being created.
        ''' </summary>
        Private Sub New()
        End Sub

#End Region

#Region " Hidden Methods "

        ''' <summary>
        ''' Serves as a hash function for a particular type.
        ''' </summary>
        <EditorBrowsable(EditorBrowsableState.Never)>
        Public Shadows Function GetHashCode() As Integer
            Return MyBase.GetHashCode
        End Function

        ''' <summary>
        ''' Gets the System.Type of the current instance.
        ''' </summary>
        ''' <returns>The exact runtime type of the current instance.</returns>
        <EditorBrowsable(EditorBrowsableState.Never)>
        Public Shadows Function [GetType]() As Type
            Return MyBase.GetType
        End Function

        ''' <summary>
        ''' Determines whether the specified System.Object instances are considered equal.
        ''' </summary>
        <EditorBrowsable(EditorBrowsableState.Never)>
        Public Shadows Function Equals(ByVal obj As Object) As Boolean
            Return MyBase.Equals(obj)
        End Function

        ''' <summary>
        ''' Determines whether the specified System.Object instances are the same instance.
        ''' </summary>
        <EditorBrowsable(EditorBrowsableState.Never)>
        Private Shadows Sub ReferenceEquals()
        End Sub

        ''' <summary>
        ''' Returns a String that represents the current object.
        ''' </summary>
        <EditorBrowsable(EditorBrowsableState.Never)>
        Public Shadows Function ToString() As String
            Return MyBase.ToString
        End Function

#End Region

    End Class

#End Region

#Region " Constructors "

    ''' <summary>
    ''' Initializes a new instance of the <see cref="FormDragger"/> class.
    ''' </summary>
    Public Sub New()
        Me.forms1={}
    End Sub

    ''' <summary>
    ''' Initializes a new instance of the <see cref="FormDragger"/> class.
    ''' </summary>
    ''' <param name="form">The <see cref="Form"/> used to perform draggable operations.</param>
    ''' <param name="enabled">If set to <c>true</c>, enable dragging on the <see cref="Form"/>.</param>
    ''' <param name="cursor">The <see cref="Cursor"/> used to drag the specified <see cref="Form"/>.</param>
    Public Sub New(ByVal form As Form,
                   Optional enabled As Boolean = False,
                   Optional cursor As Cursor = Nothing)

        Me.forms1 =
            {
                New FormDragInfo(form) With
                         {
                             .Enabled = enabled,
                             .Cursor = cursor
                         }
            }

        Me.AssocHandlers(form)

    End Sub

    ''' <summary>
    ''' Initializes a new instance of the <see cref="FormDragger"/> class.
    ''' </summary>
    ''' <param name="forms">The <see cref="Forms"/> used to perform draggable operations.</param>
    Public Sub New(ByVal forms As IEnumerable(Of Form))

        Me.forms1 = (From form As Form In forms
                     Select New FormDragInfo(form)).ToArray

        For Each form As Form In forms
            Me.AssocHandlers(form)
        Next form

    End Sub

    ''' <summary>
    ''' Initializes a new instance of the <see cref="FormDragger"/> class.
    ''' </summary>
    ''' <param name="formInfo">
    ''' The <see cref="FormDragInfo"/> instance 
    ''' that contains the <see cref="Form"/> reference and its draggable info.
    ''' </param>
    ''' <param name="mouseCoordinates">The current mouse coordinates.</param>
    ''' <param name="location">The current location.</param>
    Private Sub New(ByVal formInfo As FormDragInfo,
                    ByVal mouseCoordinates As Point,
                    ByVal location As Point)

        formInfo.InitialMouseCoords = mouseCoordinates
        formInfo.InitialLocation = location

    End Sub

#End Region

#Region " Public Methods "

    ''' <summary>
    ''' Adds the specified <see cref="Form"/> into the draggable <see cref="Forms"/> collection.
    ''' </summary>
    ''' <param name="form">The <see cref="Form"/>.</param>
    ''' <param name="enabled">If set to <c>true</c>, enable dragging on the <see cref="Form"/>.</param>
    ''' <param name="cursor">The <see cref="Cursor"/> used to drag the specified <see cref="Form"/>.</param>
    ''' <exception cref="System.ArgumentException">The specified form is already added.;form</exception>
    Public Function AddForm(ByVal form As Form,
                            Optional enabled As Boolean = False,
                            Optional cursor As Cursor = Nothing) As FormDragInfo

        For Each formInfo As FormDragInfo In Me.forms1

            If formInfo.Form.Equals(form) Then
                Throw New ArgumentException("The specified form is already added.", "form")
                Exit Function
            End If

        Next formInfo

        Dim newFormInfo As New FormDragInfo(form) With {.Enabled = enabled, .Cursor = cursor}
        Me.forms1 = Me.forms1.Concat({newFormInfo})
        Me.AssocHandlers(form)

        Return newFormInfo

    End Function

    ''' <summary>
    ''' Removes the specified <see cref="Form"/> from the draggable <see cref="Forms"/> collection.
    ''' </summary>
    ''' <param name="form">The form.</param>
    ''' <exception cref="System.ArgumentException">The specified form is not found.;form</exception>
    Public Sub RemoveForm(ByVal form As Form)

        Dim formInfoToRemove As FormDragInfo = Nothing

        For Each formInfo As FormDragInfo In Me.forms1

            If formInfo.Form.Equals(form) Then
                formInfoToRemove = formInfo
                Exit For
            End If

        Next formInfo

        If formInfoToRemove IsNot Nothing Then

            Me.forms1 = From formInfo As FormDragInfo In Me.forms1
                        Where Not formInfo Is formInfoToRemove

            formInfoToRemove.Enabled = False
            Me.DeassocHandlers(formInfoToRemove.Form)

        Else
            Throw New ArgumentException("The specified form is not found.", "form")

        End If

    End Sub

    ''' <summary>
    ''' Finds the <see cref="FormDragInfo"/> instance that is associated with the specified <see cref="Form"/> reference.
    ''' </summary>
    ''' <param name="form">The <see cref="Form"/>.</param>
    ''' <returns>The <see cref="FormDragInfo"/> instance that is associated with the specified <see cref="Form"/> reference.</returns>
    Public Function FindFormDragInfo(ByVal form As Form) As FormDragInfo

        Return (From formInfo As FormDragger.FormDragInfo In Me.forms1
                Where formInfo.Form Is form).FirstOrDefault

    End Function

    ''' <summary>
    ''' Finds the <see cref="FormDragInfo"/> instance that is associated with the specified <see cref="Form"/> reference.
    ''' </summary>
    ''' <param name="name">The <see cref="Form"/> name.</param>
    ''' <returns>The <see cref="FormDragInfo"/> instance that is associated with the specified <see cref="Form"/> reference.</returns>
    Public Function FindFormDragInfo(ByVal name As String,
                                     Optional stringComparison As StringComparison =
                                              StringComparison.OrdinalIgnoreCase) As FormDragInfo

        Return (From formInfo As FormDragger.FormDragInfo In Me.forms1
                Where formInfo.Name.Equals(name, stringComparison)).FirstOrDefault

    End Function

#End Region

#Region " Private Methods "

    ''' <summary>
    ''' Associates the <see cref="Form"/> handlers to enable draggable operations.
    ''' </summary>
    ''' <param name="form">The form.</param>
    Private Sub AssocHandlers(ByVal form As Form)

        AddHandler form.MouseDown, AddressOf Me.Form_MouseDown
        AddHandler form.MouseUp, AddressOf Me.Form_MouseUp
        AddHandler form.MouseMove, AddressOf Me.Form_MouseMove
        AddHandler form.MouseEnter, AddressOf Me.Form_MouseEnter
        AddHandler form.MouseLeave, AddressOf Me.Form_MouseLeave

    End Sub

    ''' <summary>
    ''' Deassociates the <see cref="Form"/> handlers to disable draggable operations.
    ''' </summary>
    ''' <param name="form">The form.</param>
    Private Sub DeassocHandlers(ByVal form As Form)

        If Not form.IsDisposed AndAlso Not form.Disposing Then

            RemoveHandler form.MouseDown, AddressOf Me.Form_MouseDown
            RemoveHandler form.MouseUp, AddressOf Me.Form_MouseUp
            RemoveHandler form.MouseMove, AddressOf Me.Form_MouseMove
            RemoveHandler form.MouseEnter, AddressOf Me.Form_MouseEnter
            RemoveHandler form.MouseLeave, AddressOf Me.Form_MouseLeave

        End If

    End Sub

    ''' <summary>
    ''' Return the new location.
    ''' </summary>
    ''' <param name="formInfo">
    ''' The <see cref="FormDragInfo"/> instance 
    ''' that contains the <see cref="Form"/> reference and its draggable info.
    ''' </param>
    ''' <param name="mouseCoordinates">The current mouse coordinates.</param>
    ''' <returns>The new location.</returns>
    Private Function GetNewLocation(ByVal formInfo As FormDragInfo,
                                    ByVal mouseCoordinates As Point) As Point

        Return New Point(formInfo.InitialLocation.X + (mouseCoordinates.X - formInfo.InitialMouseCoords.X),
                         formInfo.InitialLocation.Y + (mouseCoordinates.Y - formInfo.InitialMouseCoords.Y))

    End Function

#End Region

#Region " Hidden Methods "

    ''' <summary>
    ''' Serves as a hash function for a particular type.
    ''' </summary>
    <EditorBrowsable(EditorBrowsableState.Never)>
    Public Shadows Function GetHashCode() As Integer
        Return MyBase.GetHashCode
    End Function

    ''' <summary>
    ''' Gets the System.Type of the current instance.
    ''' </summary>
    ''' <returns>The exact runtime type of the current instance.</returns>
    <EditorBrowsable(EditorBrowsableState.Never)>
    Public Shadows Function [GetType]() As Type
        Return MyBase.GetType
    End Function

    ''' <summary>
    ''' Determines whether the specified System.Object instances are considered equal.
    ''' </summary>
    <EditorBrowsable(EditorBrowsableState.Never)>
    Public Shadows Function Equals(ByVal obj As Object) As Boolean
        Return MyBase.Equals(obj)
    End Function

    ''' <summary>
    ''' Determines whether the specified System.Object instances are the same instance.
    ''' </summary>
    <EditorBrowsable(EditorBrowsableState.Never)>
    Private Shadows Sub ReferenceEquals()
    End Sub

    ''' <summary>
    ''' Returns a String that represents the current object.
    ''' </summary>
    <EditorBrowsable(EditorBrowsableState.Never)>
    Public Shadows Function ToString() As String
        Return MyBase.ToString
    End Function

#End Region

#Region " Event Handlers "

    ''' <summary>
    ''' Handles the MouseEnter event of the Form.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
    Private Sub Form_MouseEnter(ByVal sender As Object, ByVal e As EventArgs)

        Dim formInfo As FormDragInfo = FindFormDragInfo(DirectCast(sender, Form))

        formInfo.OldCursor = formInfo.Form.Cursor

        If formInfo.Enabled Then
            formInfo.Form.Cursor = formInfo.Cursor
            ' Optional:
            ' formInfo.Form.BringToFront() 
        End If

    End Sub

    ''' <summary>
    ''' Handles the MouseLeave event of the Form.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
    Private Sub Form_MouseLeave(ByVal sender As Object, ByVal e As EventArgs)

        Dim formInfo As FormDragInfo = FindFormDragInfo(DirectCast(sender, Form))

        formInfo.Form.Cursor = formInfo.OldCursor

    End Sub

    ''' <summary>
    ''' Handles the MouseDown event of the Form.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
    Private Sub Form_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)

        Dim formInfo As FormDragInfo = FindFormDragInfo(DirectCast(sender, Form))

        If formInfo.Enabled Then
            formInfo.DragInfo = New FormDragger(formInfo, Form.MousePosition, formInfo.Form.Location)
        End If

    End Sub

    ''' <summary>
    ''' Handles the MouseMove event of the Form.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
    Private Sub Form_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)

        Dim formInfo As FormDragInfo = FindFormDragInfo(DirectCast(sender, Form))

        If formInfo.Enabled AndAlso (formInfo.DragInfo IsNot FormDragger.Empty) Then
            formInfo.Form.Location = formInfo.DragInfo.GetNewLocation(formInfo, Form.MousePosition)
        End If

    End Sub

    ''' <summary>
    ''' Handles the MouseUp event of the Form.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
    Private Sub Form_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)

        Dim formInfo As FormDragInfo = FindFormDragInfo(DirectCast(sender, Form))

        formInfo.DragInfo = FormDragger.Empty

    End Sub

#End Region

#Region " IDisposable "

    ''' <summary>
    ''' To detect redundant calls when disposing.
    ''' </summary>
    Private isDisposed As Boolean = False

    ''' <summary>
    ''' Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
    ''' </summary>
    Public Sub Dispose() Implements IDisposable.Dispose
        Me.Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub

    ''' <summary>
    ''' Releases unmanaged and - optionally - managed resources.
    ''' </summary>
    ''' <param name="IsDisposing">
    ''' <c>true</c> to release both managed and unmanaged resources; 
    ''' <c>false</c> to release only unmanaged resources.
    ''' </param>
    Protected Sub Dispose(ByVal isDisposing As Boolean)

        If Not Me.isDisposed Then

            If isDisposing Then

                For Each formInfo As FormDragInfo In Me.forms1

                    With formInfo

                        .Enabled = False
                        .OldCursor = Nothing
                        .DragInfo = FormDragger.Empty
                        .InitialMouseCoords = Point.Empty
                        .InitialLocation = Point.Empty

                        Me.DeassocHandlers(.Form)

                    End With ' form

                Next formInfo

                Me.forms1 = Nothing

            End If ' IsDisposing

        End If ' Not Me.IsDisposed

        Me.isDisposed = True

    End Sub

#End Region

End Class

#End Region

这篇关于如何在没有窗体边框的情况下移动窗体(visual studio)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆