Click here to Skip to main content
13,736,547 members
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 19 Dec 2007
Licenced CPOL

A simple way to snap a form to screen borders

, 22 Dec 2007
Rate this:
Please Sign up or sign in to vote.
A simple way to snap a form without borders to the screen.


I was searching for a way to snap a form to the borders of the screen, like what WinAmp does, and found one here. But my form was without border (FormBorderStyle = None), and the code I found here didn't work great for forms without border. It also seemed like a little complicated for beginners (almost like me!), so I tried to write it myself, and wrote a more simple and easy to understand code. This is also a very simple way to move forms without border.


The following variables should be declared right after Public Class ... so that other procedures can access them.

Dim X1 As Integer, Y1 As Integer, WR As Rectangle

Now put the following code in the MouseDown event of the form:

Private Sub Form1_MouseDown(ByVal sender As Object, _
            ByVal e As System.Windows.Forms.MouseEventArgs) _
            Handles Me.MouseDown
   X1 = e.X
   Y1 = e.Y
   WR = Screen.GetWorkingArea(Me)
End Sub

This piece of code records the point where the mouse was clicked and also the working area of the desktop. Now the main part of the code should be put in the MouseMove event of the form.

Private Sub Form1_MouseMove(ByVal sender As Object, _
            ByVal e As System.Windows.Forms.MouseEventArgs) _
            Handles Me.MouseMove
   If Not e.Button = Windows.Forms.MouseButtons.Left Then Exit Sub
   Dim NewX As Integer = Me.Left + (e.X - X1)
   Dim NewY As Integer = Me.Top + (e.Y - Y1)
   Dim W As Integer = Me.Width
   Dim H As Integer = Me.Height
   If NewY >= WR.Top - 15 And NewY <= WR.Top + 15 Then
      Me.Top = WR.Top
   ElseIf NewY + H > WR.Bottom - 15 And NewY + H < WR.Bottom + 15 Then
      Me.Top = WR.Bottom - H
      Me.Top = NewY
   End If
   If NewX >= WR.Left - 15 And NewX <= WR.Left + 15 Then
      Me.Left = WR.Left
   ElseIf NewX + W > WR.Right - 15 And NewX + W < WR.Right + 15 Then
      Me.Left = WR.Right - W
      Me.Left = NewX
   End If
End Sub

This code calculates the new position of the form when you move the mouse with left button pressed down, and snaps it when it gets near the borders of the screen. The default distance is 15, but you can change it, or replace it with a variable to change it dynamically during runtime.


It is a very easy to understand code and has no complications. It also uses a very simple method to move a form which has no border.


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


About the Author

Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralThx for the port and thax for the original code Pin
Mister0329-Nov-08 8:25
memberMister0329-Nov-08 8:25 
GeneralPort to C# Pin
hayes.adrian18-Sep-08 12:53
memberhayes.adrian18-Sep-08 12:53 
GeneralChange for Multiple Monitor screens and disabling Snap Pin
hayes.adrian18-Sep-08 12:35
memberhayes.adrian18-Sep-08 12:35 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05-2016 | 2.8.180920.1 | Last Updated 22 Dec 2007
Article Copyright 2007 by Hamed.M
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid