Click here to Skip to main content
6,595,854 members and growing! (18,706 online)
Email Password   helpLost your password?
Languages » VB.NET » HowTo License: The Code Project Open License (CPOL)

A simple way to snap a form to screen borders

By Hamed.M

Simple way to snap a form without borders to screen
VB (VB 7.x, VB 8.0, VB 9.0, VB 6)
Posted:19 Dec 2007
Updated:22 Dec 2007
Views:9,743
Bookmarked:24 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 3.95 Rating: 3.95 out of 5
1 vote, 10.0%
1

2
2 votes, 20.0%
3
3 votes, 30.0%
4
4 votes, 40.0%
5

Background

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 the forms without border. It also seemed like a little complicated for the 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.

Code

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 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
   Else
      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
   Else
      Me.Left = NewX
   End If
End Sub


This code calculates the new position of form when you move the mouse with left click 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 the runtime.

Significance

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

License

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

About the Author

Hamed.M


Member

Occupation: Other
Location: Iran, Islamic Republic Of Iran, Islamic Republic Of

Other popular VB.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralThx for the port and thax for the original code PinmemberMister039:25 29 Nov '08  
GeneralPort to C# Pinmemberhayes.adrian13:53 18 Sep '08  
GeneralChange for Multiple Monitor screens and disabling Snap Pinmemberhayes.adrian13:35 18 Sep '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 22 Dec 2007
Editor:
Copyright 2007 by Hamed.M
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project