Click here to Skip to main content
14,767,499 members
Articles » Languages » C# » Windows Forms
Posted 8 Feb 2004


120 bookmarked

Sticky Windows - How to make your (top-level) forms to stick one to the other or to the screen

Rate me:
Please Sign up or sign in to vote.
4.93/5 (34 votes)
8 Feb 2004
Nice class that makes your top-level forms stick when moved or resized. Makes it easy to arrange the windows on the desktop.

Sample Image - StickyWindows.jpg


Delphi has a nice way of displaying its forms: all forms are (by default) top-level forms. This makes it easy to develop applications that are not restricted to the "MainFrame" client area but to the all screen, without having to have your application maximized. However, visually organizing such windows is sometimes hard for the end-user if he wants to maximize the visible area and minimize the window overlaps.

The class presented here tries to overcome this "arrangement" issue helping the end-user better organize their forms by sticking them one to the other and/or to the screen when moved or resized. (Code also takes care of correct sticking in multi screen environments but I did not manage to test it.)

Using the Code

The class inherits from System.Windows.Forms.NativeWindow, thus no inheritance is required in order to make your class Stick able.

In order to make your top-windows stick-able, all you have to do is declare a member variable of type StickyWindow and initialize it in your form:

private Blue.Windows.StickyWindow stickyWindow;
Form2() // your constructor
stickyWindow = new 
Blue.Windows.StickyWindow ( this );

Now you have a form that will automatically stick on resize or move to all other forms that use the StickyWindow class and to the screen margins;


There are few options you can customize about a Sticky Window:

  1. public bool StickOnMove

    Allows the form to try to stick when the form is moved.

  2. public bool StickOnResize

    Allows the form to try to stick when the form is resized.

  3. public bool StickToOther

    Allows the form to try to stick to other stick-able forms.

  4. public bool StickToScreen

    Allows the form to try to stick to the screen margins.

  5. public int StickGap

    Distance in pixels between two forms or a form and the screen where the sticking should start.

  6. public static void RegisterExternalReferenceForm( Form externalForm )

    Registers an external form (not StickyWindow form) in the list of forms that StickyWindows can stick to.

    E.g.: If you don't want your MainFrame to be stick-able but you want child forms to try to stick to it, than you will register your MainFrame with RegisterExternalReferenceForm so that other forms will try to stick to it.

  7. public static void UnregisterExternalReferenceForm ( Form externalForm )

    Same as RegisterExternalReferenceForm but it un-registers the form.

These are all the defined possible customizations of the StickyWindow.

Implementation Details

The idea behind the implementation is to filter the NC (NonClient) messages from the Form's message queue and do the own Moving/Resizing of the form by overriding the WndProc of the NativeWindow.

When a WM_NCLBUTTONDOWN is received for the Form, the StickyWindow checks the required operation (Move/Resize) and starts the algorithm for the requested operation.

While mouse is moved, the algorithm computes the new Form's position/size and then tries to compute the distance to the closest Form or screen border. If the shortest distance is smaller than the StickGap, the form will be moved/resized to touch the margins of the form/screen, thus sticking taking place.

While resize takes place, the form will take care not to shrink below Form.MinimumSize or SystemInformation.MinWindowTrackSize or to grow above Form.MaximumSize or SystemInformation.MaxWindowTrackSize.


  • The main credits go to WinAmp's team who made the idea of sticking windows popular.
  • The second credits go to Infopulse, the place where I've developed the first StickyWindows implementation in C++.


  • 09.Feb.2004: version 1.0

    First released version.


You can use these sources for absolutely free.


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

Corneliu Tusnea
Technical Lead OneSaas - Cloud Integrations Made Easy
Australia Australia

Comments and Discussions

Questioncool stuff Pin
MerlinEl10-Jul-19 6:06
MemberMerlinEl10-Jul-19 6:06 
QuestionCan this sample use for other user controls? Pin
Member 1050814011-Jul-17 20:06
MemberMember 1050814011-Jul-17 20:06 
GeneralMy vote of 5 Pin
Maxwolf Goodliffe6-Dec-14 23:21
MemberMaxwolf Goodliffe6-Dec-14 23:21 
GeneralExactly what I was looking for. My vote of 5 Pin
ZeedijkMike24-Oct-14 10:02
MemberZeedijkMike24-Oct-14 10:02 
QuestionClick-event Pin
klaus meierr30-Sep-13 4:31
Memberklaus meierr30-Sep-13 4:31 
GeneralMy vote of 5 Pin
ibyk302-May-13 12:31
Memberibyk302-May-13 12:31 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey28-Nov-12 20:13
professionalManoj Kumar Choubey28-Nov-12 20:13 
SuggestionEasy Implementation Pin
AlexKven31-Aug-12 7:05
MemberAlexKven31-Aug-12 7:05 
GeneralMy vote of 5 Pin
AlexKven31-Aug-12 7:02
MemberAlexKven31-Aug-12 7:02 
GeneralWinamp-like main window behavior Pin
PHANT0Mas25-Oct-10 3:51
MemberPHANT0Mas25-Oct-10 3:51 
GeneralIssues with FormBorderStyle Pin
Padgett777-Mar-10 20:53
MemberPadgett777-Mar-10 20:53 
GeneralVB .Net Version Pin
vaduganathan24-Nov-09 0:58
Membervaduganathan24-Nov-09 0:58 
GeneralRe: VB .Net Version Pin
Mr.PoorEnglish15-Feb-10 5:11
MemberMr.PoorEnglish15-Feb-10 5:11 
GeneralWPF Adapted Project Download Pin
ricibald18-Oct-09 22:55
Memberricibald18-Oct-09 22:55 
QuestionRe: WPF Adapted Project Download Pin
Thomas Freudenberg5-Feb-17 5:07
MemberThomas Freudenberg5-Feb-17 5:07 
AnswerRe: WPF Adapted Project Download Pin
Corneliu Tusnea5-Feb-17 13:44
MemberCorneliu Tusnea5-Feb-17 13:44 
GeneralThanks! Pin
biocomp6-Mar-09 3:39
Memberbiocomp6-Mar-09 3:39 
Questionhow to make MDI childs to be sticky? Pin
Gilvinit25-Feb-09 23:42
MemberGilvinit25-Feb-09 23:42 
Generalvery nice ! Pin
J1mp4n5313-Nov-08 0:37
MemberJ1mp4n5313-Nov-08 0:37 
GeneralSticking doesn't work for Forms that are not shown in the Taskbar Pin
texor12-Nov-08 9:31
Membertexor12-Nov-08 9:31 
AnswerRe: Sticking doesn't work for Forms that are not shown in the Taskbar Pin
texor13-Nov-08 20:25
Membertexor13-Nov-08 20:25 
GeneralCustom Titelbar FormBorderStyle Pin
Babybaer21-Oct-08 4:01
MemberBabybaer21-Oct-08 4:01 
GeneralRe: Custom Titelbar FormBorderStyle Pin
Babybaer22-Oct-08 1:25
MemberBabybaer22-Oct-08 1:25 
GeneralDo i need Pin
C# Beginner Nick30-Oct-07 11:55
MemberC# Beginner Nick30-Oct-07 11:55 
GeneralKeep Children Inside Parent Pin
clintonpatrick18-Nov-06 13:12
Memberclintonpatrick18-Nov-06 13:12 

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.