Click here to Skip to main content
15,123,118 members
Articles / Desktop Programming / Windows Forms
Posted 24 Aug 2006


47 bookmarked

A .NET Snap To Screen Form

Rate me:
Please Sign up or sign in to vote.
4.81/5 (27 votes)
24 Aug 2006CPOL1 min read
An article on snapping a Windows Form to a desktop screen border.

Sample Image - SnapTo.png


This article demonstrates how to add a snap-to-screen feature to your application. This feature allows you to drag your window near the screen border and have the window automatically get pulled towards that border like a magnet. There is already a nice unmanaged C++ implementation here.


At TrayGames, we have a client that delivers chat and games to your computer. Our client is written completely in .NET 2.0, using a combination of C# and VB.NET. We wanted our client to have the snap-to-screen functionality that other popular applications such as Winamp and Skype have.

Using the Code

To add snap-to-screen functionality to your application, you have to override the Windows.Forms.Form.WndProc method and handle the WM_WINDOWPOSCHANGING message. Your handler will call my custom SnapToDesktopBorder method:

Imports system.Runtime.InteropServices

Public Class Form1
    Private Const mSnapOffset As Integer = 35
    Private Const WM_WINDOWPOSCHANGING As Integer = &H46

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure WINDOWPOS
        Public hwnd As IntPtr
        Public hwndInsertAfter As IntPtr
        Public x As Integer
        Public y As Integer
        Public cx As Integer
        Public cy As Integer
        Public flags As Integer
    End Structure

    Protected Overrides Sub WndProc(ByRef m As Message)
        ' Listen for operating system messages
        Select Case m.Msg
                SnapToDesktopBorder(Me, m.LParam, 0)
        End Select

    End Sub

The heart of the functionality is in the implmentation of the SnapToDesktopBorder method. It calculates the distance to the desktop screen edges, and repositions the window as necessary:

    Public Shared Sub SnapToDesktopBorder(ByVal clientForm _
           As Form, ByVal LParam As IntPtr, ByVal widthAdjustment As Integer)
        If clientForm Is Nothing Then
            ' Satisfies rule: Validate parameters
            Throw New ArgumentNullException("clientForm")
        End If

        ' Snap client to the top, left, bottom or right desktop border
        ' as the form is moved near that border.

            ' Marshal the LPARAM value which is a WINDOWPOS struct
            Dim NewPosition As New WINDOWPOS
            NewPosition = CType(Runtime.InteropServices.Marshal.PtrToStructure( _
                LParam, GetType(WINDOWPOS)), WINDOWPOS)

            If NewPosition.y = 0 OrElse NewPosition.x = 0 Then
                Return ' Nothing to do!
            End If

            ' Adjust the client size for borders and caption bar
            Dim ClientRect As Rectangle = _
            ClientRect.Width += _
                SystemInformation.FrameBorderSize.Width - widthAdjustment
            ClientRect.Height += (SystemInformation.FrameBorderSize.Height + _

            ' Now get the screen working area (without taskbar)
            Dim WorkingRect As Rectangle = _

            ' Left border
            If NewPosition.x >= WorkingRect.X - mSnapOffset AndAlso _
                NewPosition.x <= WorkingRect.X + mSnapOffset Then
                NewPosition.x = WorkingRect.X
            End If

            ' Get screen bounds and taskbar height
            ' (when taskbar is horizontal)
            Dim ScreenRect As Rectangle = _
            Dim TaskbarHeight As Integer = _
                ScreenRect.Height - WorkingRect.Height

            ' Top border (check if taskbar is on top
            ' or bottom via WorkingRect.Y)
            If NewPosition.y >= -mSnapOffset AndAlso _
                 (WorkingRect.Y > 0 AndAlso NewPosition.y <= _
                 (TaskbarHeight + mSnapOffset)) OrElse _
                 (WorkingRect.Y <= 0 AndAlso NewPosition.y <= _
                 (mSnapOffset)) Then
                If TaskbarHeight > 0 Then
                    NewPosition.y = WorkingRect.Y ' Horizontal Taskbar
                    NewPosition.y = 0 ' Vertical Taskbar
                End If
            End If

            ' Right border
            If NewPosition.x + ClientRect.Width <= _
                 WorkingRect.Right + mSnapOffset AndAlso _
                 NewPosition.x + ClientRect.Width >= _
                 WorkingRect.Right - mSnapOffset Then
                NewPosition.x = WorkingRect.Right - (ClientRect.Width + _
            End If

            ' Bottom border
            If NewPosition.y + ClientRect.Height <= _
                   WorkingRect.Bottom + mSnapOffset AndAlso _
                   NewPosition.y + ClientRect.Height >= _
                   WorkingRect.Bottom - mSnapOffset Then
                NewPosition.y = WorkingRect.Bottom - (ClientRect.Height + _
            End If

            ' Marshal it back
            Runtime.InteropServices.Marshal.StructureToPtr(NewPosition, _
                                                           LParam, True)
        Catch ex As ArgumentException
        End Try
    End Sub
End Class    

You can adjust the mSnapOffset member variable to adjust the distance between your window and the screen border where the snap will happen. Note that the widthAdjustment parameter is available as a fudge factor in case your client has special width requirements. That's all there is to it!

Points of Interest

This article is interesting if you want to learn some creative ways to position Windows Forms. Although this wasn't the most complicated topic, hopefully, I've saved someone a little time figuring this out! If you are interested in creating your own multi-player online games, you should check out the TGSDK which is downloadable from the TrayGames web site.


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


About the Author

Perry Marchant
United States United States
I've authored many articles that tackle real-world issues to save my peers in the development community valuable time. For example I've written articles that: show how to decode Ogg Vorbis audio files using the .NET Framework; describe best practices for Improving Entity Framework performance; and demonstrate step-by-step how to create a multi-player game.

Comments and Discussions

GeneralMy vote of 4 Pin
AAv.Soft21-Aug-14 0:11
MemberAAv.Soft21-Aug-14 0:11 
QuestionProblem with big form with NoBorder style Pin
AAv.Soft21-Aug-14 0:08
MemberAAv.Soft21-Aug-14 0:08 
GeneralMy vote of 5 Pin
Jαved12-Jun-12 23:58
professionalJαved12-Jun-12 23:58 
GeneralHow to snap to another form ? or instance of same form? Pin
Member 44231544-May-11 22:35
MemberMember 44231544-May-11 22:35 
Generalif you'd like a c# version implementing optional forcing all of form to stay on screen Pin
BillWoodruff6-Aug-09 5:00
mveBillWoodruff6-Aug-09 5:00 
GeneralSimplier Pin
Ky Nam8-Nov-08 6:13
MemberKy Nam8-Nov-08 6:13 
Hamed.M16-Dec-07 7:34
MemberHamed.M16-Dec-07 7:34 
QuestionHow would one change the mSnapoffset on the fly? Pin
danielj2323-Nov-07 4:51
Memberdanielj2323-Nov-07 4:51 
AnswerRe: How would one change the mSnapoffset on the fly? Pin
danielj2323-Nov-07 4:53
Memberdanielj2323-Nov-07 4:53 
JokeGreat code Pin
danielj2323-Nov-07 4:11
Memberdanielj2323-Nov-07 4:11 
QuestionCould this be used to do docking? Pin
ToshRa8-Nov-07 12:29
MemberToshRa8-Nov-07 12:29 
GeneralThanks Pin
pixeldoc200022-May-07 14:34
Memberpixeldoc200022-May-07 14:34 
GeneralOh, snap!! Pin
mikeOA17-Oct-06 12:26
MembermikeOA17-Oct-06 12:26 
GeneralRe: Oh, snap!! Pin
moneyshot29-Nov-06 10:44
Membermoneyshot29-Nov-06 10:44 
GeneralRe: Oh, snap!! Pin
mikeOA29-Nov-06 15:40
MembermikeOA29-Nov-06 15:40 
GeneralRe: Oh, snap!! Pin
moneyshot29-Nov-06 17:05
Membermoneyshot29-Nov-06 17:05 
GeneralRe: Oh, snap!! Pin
mikeOA30-Nov-06 7:02
MembermikeOA30-Nov-06 7:02 
GeneralRe: Oh, snap!! Pin
Cesa3724-Apr-08 4:20
MemberCesa3724-Apr-08 4:20 
GeneralGood Work Pin
Polymorpher12-Sep-06 12:32
MemberPolymorpher12-Sep-06 12:32 
GeneralNice, but doesn't work correctly with multiple desktops Pin
mav.northwind24-Aug-06 20:49
Membermav.northwind24-Aug-06 20:49 
AnswerRe: Nice, but doesn't work correctly with multiple desktops [modified] Pin
Richard Rathmann29-Aug-06 13:39
MemberRichard Rathmann29-Aug-06 13:39 
GeneralRe: Nice, but doesn't work correctly with multiple desktops Pin
Yep-ItsMe31-Aug-06 15:09
MemberYep-ItsMe31-Aug-06 15:09 
GeneralFantastic stuff. [modified] Pin
nano_electronix24-Aug-06 13:48
Membernano_electronix24-Aug-06 13:48 
GeneralVery cool. Pin
Marc Leger24-Aug-06 11:59
MemberMarc Leger24-Aug-06 11:59 

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.