Click here to Skip to main content
11,929,924 members (60,009 online)
Click here to Skip to main content
Add your own
alternative version


116 bookmarked

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

, 8 Feb 2004
Rate this:
Please Sign up or sign in to vote.
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

You may also be interested in...

Comments and Discussions

QuestionRe: Have you considered writing a component that implements IExtenderProvider? Pin
Benjamin Liedblad5-Sep-05 21:01
memberBenjamin Liedblad5-Sep-05 21:01 
GeneralForm Dragging Capability Pin
jaxterama11-Feb-04 10:05
memberjaxterama11-Feb-04 10:05 
GeneralRe: Form Dragging Capability Pin
Tutu14-Feb-04 15:11
memberTutu14-Feb-04 15:11 
GeneralRe: Form Dragging Capability Pin
SpaceNuts16-Jun-04 11:44
memberSpaceNuts16-Jun-04 11:44 
GeneralRe: Form Dragging Capability Pin
Jedon10-Mar-05 7:41
memberJedon10-Mar-05 7:41 
GeneralRe: Form Dragging Capability Pin
zythum18-Mar-05 7:46
memberzythum18-Mar-05 7:46 
GeneralRe: Form Dragging Capability Pin
mickeymicks8-Sep-06 4:22
membermickeymicks8-Sep-06 4:22 
GeneralRe: Form Dragging Capability Pin
chr.dev26-Nov-07 5:34
memberchr.dev26-Nov-07 5:34 
thanks a lot for this great code and the additional code above!
after some hours of coding i could find a solution for only let the child window move if it is also sticked to parent window.

first do the steps above in this thread.
then create a third additional Property inside StickyWindow class:

private bool isSticked;

public bool IsSticked
get { return isSticked; }
set { isSticked = value; }

then modify the code above (the fixed one) inside "Move(...)" (see comment):

// move child windows in step with parent if childLock is on.
if (childLock && isParent)
Point childOffsetPoint = new Point(formOffsetPoint.X, formOffsetPoint.Y);
if (childOffsetPoint.X == stickGap + 1)
childOffsetPoint.X = 0;
if (childOffsetPoint.Y == stickGap + 1)
childOffsetPoint.Y = 0;
foreach (Form sw in GlobalStickyWindows)
//if not current window and only if window is not sticked
if (sw != this.originalForm && (((StickyWindow)(StickyWindow.FromHandle(sw.Handle))).IsSticked)) //<-- ADD THIS
sw.Left += p.X - originalForm.Left + childOffsetPoint.X;
sw.Top += p.Y - originalForm.Top + childOffsetPoint.Y;

now edit "private void Move_Stick ( Rectangle toRect, bool bInsideStick )":
at the beginning of this method set

this.isSticked = false; //very important!

now you can set

this.isSticked = true;
in all cases you want to let the window be sticked. for me, i set it inside the two if-clauses inside
if ( bInsideStick )

i.e. here:
if ( bInsideStick )
if ( (Math.Abs(formRect.Left - toRect.Right) <= Math.Abs(formOffsetPoint.X) ) )
{ // left 2 right
formOffsetPoint.X = toRect.Right - formRect.Left;
this.isSticked = true; //<-- ADD THIS
if ( (Math.Abs(formRect.Left + formRect.Width - toRect.Left) <= Math.Abs(formOffsetPoint.X) ) )
{ // right 2 left
formOffsetPoint.X = toRect.Left - formRect.Width - formRect.Left;
this.isSticked = true; //<-- ADD THIS

analogue at "//Stick Top to Bottom" and "// snap Bottom to Top".
so if i snapped my child window to the BORDER of the parent window (not inside), the child is sticked and moves also by the parent.

i tested this modification only with ONE child window!!! other experiences?

hope i could help you. Wink | ;)

best regards from germany,

GeneralRe: Form Dragging Capability Pin
Luka9-Sep-08 4:05
memberLuka9-Sep-08 4:05 
QuestionAttribute form? Pin
Keith Farmer9-Feb-04 16:13
memberKeith Farmer9-Feb-04 16:13 
AnswerRe: Attribute form? Pin
Tutu9-Feb-04 20:14
memberTutu9-Feb-04 20:14 
GeneralRe: Attribute form? Pin
Keith Farmer10-Feb-04 9:26
memberKeith Farmer10-Feb-04 9:26 
QuestionAdd band object capability? Pin
Keith Farmer9-Feb-04 15:22
memberKeith Farmer9-Feb-04 15:22 
AnswerRe: Add band object capability? Pin
Tutu9-Feb-04 20:18
memberTutu9-Feb-04 20:18 
GeneralRe: Add band object capability? Pin
Keith Farmer10-Feb-04 9:30
memberKeith Farmer10-Feb-04 9:30 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 9 Feb 2004
Article Copyright 2004 by Corneliu Tusnea
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid