5,427,303 members and growing! (20,384 online)
Email Password   helpLost your password?
Languages » VB.NET » Windows Forms     Intermediate

A .NET Snap To Screen Form

By Perry Marchant

An article on snapping a Windows Form to a desktop screen border.
VB.NET 2.0, WinXP, Windows, .NETVisual Studio, VS2005, Dev

Posted: 24 Aug 2006
Updated: 24 Aug 2006
Views: 22,805
Bookmarked: 25 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
19 votes for this Article.
Popularity: 5.72 Rating: 4.47 out of 5
1 vote, 5.3%
1
0 votes, 0.0%
2
1 vote, 5.3%
3
0 votes, 0.0%
4
17 votes, 89.5%
5

Sample Image - SnapTo.png

Introduction

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.

Background

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
            Case WM_WINDOWPOSCHANGING
                SnapToDesktopBorder(Me, m.LParam, 0)
        End Select

        MyBase.WndProc(m)
    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.


        Try
            ' 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 = _
                clientForm.RectangleToScreen(clientForm.ClientRectangle)
            ClientRect.Width += _
                SystemInformation.FrameBorderSize.Width - widthAdjustment
            ClientRect.Height += (SystemInformation.FrameBorderSize.Height + _
                                  SystemInformation.CaptionHeight)

            ' Now get the screen working area (without taskbar)

            Dim WorkingRect As Rectangle = _
                Screen.GetWorkingArea(clientForm.ClientRectangle)

            ' 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 = _
                Screen.GetBounds(Screen.PrimaryScreen.Bounds)
            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

                Else
                    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 + _
                                SystemInformation.FrameBorderSize.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 + _
                                SystemInformation.FrameBorderSize.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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Perry Marchant


A results oriented professional building on 10 years of progressive accomplishments in Software Development and Information Technology, Perry joined TrayGames in 2005 to deliver an innovative online games platform for the consumer market. He is an MCSD and has a B.S. Degree in Computer & Information Science from Brooklyn College.
Occupation: Software Developer (Senior)
Location: United States United States

Other popular VB.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 18 of 18 (Total in Forum: 18) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralA MUCH EASIER WAY TO DO THAT....memberHamed.M7:34 16 Dec '07  
QuestionHow would one change the mSnapoffset on the fly?memberdanielj234:51 23 Nov '07  
AnswerRe: How would one change the mSnapoffset on the fly?memberdanielj234:53 23 Nov '07  
JokeGreat codememberdanielj234:11 23 Nov '07  
GeneralCould this be used to do docking?memberToshRa12:29 8 Nov '07  
GeneralThanksmemberpixeldoc200014:34 22 May '07  
GeneralOh, snap!!membermikeOA12:26 17 Oct '06  
GeneralRe: Oh, snap!!membermoneyshot10:44 29 Nov '06  
GeneralRe: Oh, snap!!membermikeOA15:40 29 Nov '06  
GeneralRe: Oh, snap!!membermoneyshot17:05 29 Nov '06  
GeneralRe: Oh, snap!!membermikeOA7:02 30 Nov '06  
GeneralRe: Oh, snap!!memberCesa374:20 24 Apr '08  
GeneralGood WorkmemberPolymorpher12:32 12 Sep '06  
GeneralNice, but doesn't work correctly with multiple desktopsmembermav.northwind20:49 24 Aug '06  
AnswerRe: Nice, but doesn't work correctly with multiple desktops [modified]memberRichard Rathmann13:39 29 Aug '06  
GeneralRe: Nice, but doesn't work correctly with multiple desktopsmemberYep-ItsMe15:09 31 Aug '06  
GeneralFantastic stuff. [modified]membernano_electronix13:48 24 Aug '06  
GeneralVery cool.memberMarc Leger11:59 24 Aug '06  

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

PermaLink | Privacy | Terms of Use
Last Updated: 24 Aug 2006
Editor: Smitha Vijayan
Copyright 2006 by Perry Marchant
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project