Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
664 views
in Technique[技术] by (71.8m points)

vb.net - How to move form without form border (visual studio)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 <

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...