Click here to Skip to main content
15,906,628 members
Articles / Programming Languages / Visual Basic 10

Move a Form whose Border Isn't Visible

Rate me:
Please Sign up or sign in to vote.
3.75/5 (3 votes)
22 Feb 2010CPOL1 min read 17.3K   354   9   3
A class which enables the user to Drag the form by clicking Form's Surface

Introduction

I recently wrote an application where I needed to use a Skinned interface, and the first thing to do when skinning an application is to remove the Border of a Form by setting:

VB.NET
FormBorderStyle = Windows.Forms.FormBorderStyle.None  

Without Border, the main form can attain any shape. But this leaves us with one problem, which is How is the user going to move the Form? So we have to write some custom code to do this, and this is the reason why I wrote a class called MoveFormWithoutBorder.

A Little Bit About the Code

Global variables which hold the necessary information for moving the form:

VB.NET
'Control which will be used to drag	the Form	   
Dim WithEvents HookedControl As Windows.Forms.Control
    
'Base Form
Dim BaseControlToMove As Control

Events

We mainly handle three events of the HookedControl:

  • MouseDown
  • MouseMove
  • MouseUp

1. MouseDown

At this point, we save the initial location of our form in a global var OffsetLocation, and then set MoveHook Boolean Var to True:

VB.NET
Private Sub Control_MouseDown(ByVal sender As System.Object, _
                                    ByVal e As System.Windows.Forms.MouseEventArgs) _
                                    Handles HookedControl.MouseDown

        If Disable = True Then Exit Sub

        If e.Button = Windows.Forms.MouseButtons.Left Then
            MoveHook = True
            OffsetLocation = New Point(e.Location.X, e.Location.Y)
        End If

    End Sub 

2. MouseMove

During this event, the difference is calculated and then added to the Location of BaseControlToMove:

VB.NET
Private Sub Control_MouseMove(ByVal sender As Object, _
   ByVal e As System.Windows.Forms.MouseEventArgs) Handles HookedControl.MouseMove

       If Disable = True Then Exit Sub

       If MoveHook = True Then

           Dim Newx, NewY As Integer

           Newx = e.Location.X - OffsetLocation.X
           NewY = e.Location.Y - OffsetLocation.Y

           BaseControlToMove.Location = New Point(BaseControlToMove.Location.X + Newx, _
                   BaseControlToMove.Location.Y + NewY)

       End If

   End Sub

3. MouseUp

This is the third event that is fired when the user releases the Left Mouse Button. At this point, we clear the Flags MoveHook and OffsetLocation, so that the process of moving a form can start all over again.

VB.NET
Private Sub Control_MouseUp(ByVal sender As Object, _
   ByVal e As System.Windows.Forms.MouseEventArgs) Handles HookedControl.MouseUp

       If Disable = True Then Exit Sub

       If e.Button = Windows.Forms.MouseButtons.Left Then
           MoveHook = False
           OffsetLocation = Nothing

       End If

   End Sub

Methods

The class exposes two methods to disable or enable the Hook so that the movement can be disabled as required:

VB.NET
Public Sub DisableHook()

    Disable = True
    MoveHook = False
    OffsetLocation.X = 0
    OffsetLocation.Y = 0

End Sub

Public Sub EnableHook()

    Disable = False

End Sub

Usage

VB.NET
'Create a Instance of this class and pass 2 Controls to Constructor 
Dim FormMover As MoveFormWithoutBorder = New MoveFormWithoutBorder(Me, Me) 

Points of Interest

This class definitely comes in handy when removing the border, and also saves from writing the same code all over again.

History

  • 22nd February, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer Edifecs
India India
http://in.linkedin.com/pub/damanjeet-singh/35/2a0/195/

Comments and Discussions

 
GeneralAn easier way Pin
pt14012-Mar-10 23:05
pt14012-Mar-10 23:05 
GeneralRe: An easier way Pin
Damon882-Mar-10 23:19
Damon882-Mar-10 23:19 
JokeOptimisation Pin
PapyRef22-Feb-10 6:14
professionalPapyRef22-Feb-10 6:14 
in Control_MouseMove function, you can do this

If MoveHook = True Then
    Dim NewX As Integer = e.Location.X - OffsetLocation.X
    Dim NewY As Integer = e.Location.Y - OffsetLocation.Y

    BaseControlToMove.Location = New Point(BaseControlToMove.Location.X + NewX, BaseControlToMove.Location.Y + NewY)
End If


Rem : Excuse me for my poor english, I'm french Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.