Page 1 of 1

Moving a Borderless Form Rate Topic: -----

#1 Sethro117  Icon User is offline

  • Still the sexiest mofo.
  • member icon

Reputation: 236
  • View blog
  • Posts: 2,378
  • Joined: 14-January 09

Posted 31 October 2009 - 07:58 PM

Ive seen this topic quite a bit in the VB.NET Forums; people wanting to move Borderless Forms and such. So here's a tutorial to simply show you how easy it is.

First, we need to declare our variables:
	Dim drag As Boolean 
	Dim mousex As Integer 
	Dim mousey As Integer


Next we need to tell the application its time to drag by setting drag to true and then letting the form know which way or location to head by getting the position of our Cursor:
	Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
		drag = True
		mousex = Windows.Forms.Cursor.Position.X - Me.Left
		mousey = Windows.Forms.Cursor.Position.Y - Me.Top
	End Sub


The code below actually does the moving of the Form for us. Since we clicked on the Form, drag was set to true so now as we move our mouse the Form will follow:
	Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
		If drag Then
			Me.Top = Windows.Forms.Cursor.Position.Y - mousey
			Me.Left = Windows.Forms.Cursor.Position.X - mousex
		End If
	End Sub


Finally, its time to stop dragging the form so as we release our mouse from the Form, drag needs to be set to false so it wont move anywhere until its clicked again:
	Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
		drag = False
	End Sub



Pretty simple, no?

Is This A Good Question/Topic? 3
  • +

Replies To: Moving a Borderless Form

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,466
  • Joined: 29-May 08

Posted 01 November 2009 - 09:05 AM

Good Tutorial.

The form can be dragged with any mouse key press, personally I prefer for the dragging to happen with a left button which gices the possibility of canceling the dragging with the right.
Was This Post Helpful? 1
  • +
  • -

#3 ???  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 48
  • Joined: 25-November 10

Posted 11 December 2010 - 11:53 AM

Thanks! I've been looking for a way to do that!
The only flaw with your code it that the entire form can be dragged, but if you add a panel and make it so that the events are for the panel it works just fine. B)
Was This Post Helpful? 0
  • +
  • -

#4 JizzaDaMan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 139
  • Joined: 23-May 11

Posted 06 July 2011 - 01:01 PM

brilliant tutorial really helpful and simple!!!
Was This Post Helpful? 0
  • +
  • -

#5 The Prodigy  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 03-October 11

Posted 11 October 2011 - 10:38 AM

View PostAdamSpeight2008, on 01 November 2009 - 11:05 AM, said:

Good Tutorial.

The form can be dragged with any mouse key press, personally I prefer for the dragging to happen with a left button which gices the possibility of canceling the dragging with the right.


Well I've thought of a way to fix this! If you want it to only drag when the left mouse button is clicked, use this code in the MouseDown event of the form:
If e.Button = Windows.Forms.MouseButtons.Left Then
    drag = True
    mousex = Windows.Forms.Cursor.Position.X - Me.Left
    mousey = Windows.Forms.Cursor.Position.Y - Me.Top
End If

Was This Post Helpful? 1
  • +
  • -

#6 soviet87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-December 11

Posted 20 December 2011 - 02:32 PM

Fantastic tutorial! I really appreciate you posting this!
Was This Post Helpful? 0
  • +
  • -

#7 diablos code  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-January 12

Posted 25 January 2012 - 03:00 AM

View Postsoviet87, on 20 December 2011 - 02:32 PM, said:

Fantastic tutorial! I really appreciate you posting this!

Much easier method i think anyway.
    Const WM_NCHITTEST As Integer = &H84
    Const HTCLIENT As Integer = &H1
    Const HTCAPTION As Integer = &H2
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case WM_NCHITTEST
                MyBase.WndProc(m)
                If m.Result = HTCLIENT Then m.Result = HTCAPTION
            Case Else
                MyBase.WndProc(m)
        End Select
    End Sub



Test it

Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.Panel1 = New System.Windows.Forms.Panel()
        Me.SuspendLayout()
        '
        'Panel1
        '
        Me.Panel1.Location = New System.Drawing.Point(26, 10)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(308, 37)
        Me.Panel1.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(349, 303)
        Me.Controls.Add(Me.Panel1)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    End class


Was This Post Helpful? 0
  • +
  • -

#8 borjalan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 31-July 14

Posted 31 July 2014 - 05:05 AM

Hi, i found this code really usefull in all my projects but today i begin a new project and i donīt understand what is going wrong.

I know where is the problem, but actually i donīt know how to solve it. I have a form with a PictureBox, and The background color of the box is Control. I set Transparency Key as Control to just have the picturebox on the screen. Now the code donīt work because the form1 is invisible. When I change The Form1_MouseDown event for PictureBox1_MouseDown it also didnīt work.



Any idea of how to fix it? (And sorry for necropost)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1