Programming Languages Hacks

Importanti regole per linguaggi di programmazione rilevanti come Java, C, C++, C#…

  • Subscribe

  • Lettori

    I miei lettori abituali

  • Twitter

Download Snapping / Sticky / Magnetic Windows for WPF

Posted by Ricibald on October 19th, 2009

Starting from Sticky Windows on CodeProject (makes your windows snapping like winamp or google talk) I adapted the project developed for standard WinForm in Wpf Application. It works with multiple monitor and with windows vista/7.


I adapted the following interface to get the app work with WPF App:

    public interface IFormAdapter
        IntPtr Handle { get; }
        Rectangle Bounds { get; set; }
        Size MaximumSize { get; set; }
        Size MinimumSize { get; set; }
        bool Capture { get; set; }
        void Activate();
        Point PointToScreen(Point point);

    public class WinFormAdapter : IFormAdapter
        // Original Code Implementation

    public class WpfFormAdapter : IFormAdapter
        // My Implementation

Enjoy and please give me feedback!


21 Responses to “Download Snapping / Sticky / Magnetic Windows for WPF”

  1. Kurt Says:

    Very nice…. well done.

  2. Vijay Says:

    I just implemented the solution that you have given here above. Once done, I found that the Windows 7 behavior of snapping does not work anymore. I know this post was posted a long back, but do you have any solution to get the windows 7 behavior along with the snapping you implemented here.
    Your help is appreciated.

  3. Logan Says:

    Thanks a lot for this! I had to hack it a bit to get it working with my borderless WPF windows, but I’ve got it mostly working now. :)

  4. Sigi Says:

    Snapping/Sticking working fine when moving the window.
    It doesn’t work when resizing window.

  5. Sigi Says:

    Try to maximize windows

  6. Gawi Says:

    Ok, It’s great, but how can I hack it to get it working with borderless WPF Windows?

  7. Gawi Says:

    Borderless windows & WPF? No problem!

    Yo just have to add few simple lines to Your code and booya!

    in StickyWindow.cs in Win32 class add:

    public static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, IntPtr lParam);

    public static IntPtr MakeLParam(int x, int y)
    return (IntPtr)((y << 16) | (x & 0xffff));

    Then in your App class in Loaded Event (where StickyWindow class being constructed) hook to your window LocationChanged Event like this:

    this.LocationChanged += new EventHandler(YourWindow_LocationChanged);

    and the last thing:
    add this method

    void YourWindow_LocationChanged(object sender, EventArgs e)
    System.Windows.Point MousePoint = Mouse.GetPosition(this);
    System.Windows.Point ScreenPoint = this.PointToScreen(MousePoint);

    Win32.SendMessage(PhoneCTIStickyWindow.Handle, Win32.WM.WM_NCLBUTTONDOWN, Win32.HT.HTCAPTION, Win32.MakeLParam(Convert.ToInt32(ScreenPoint.X), Convert.ToInt32(ScreenPoint.Y)));
    Win32.SendMessage(PhoneCTIStickyWindow.Handle, Win32.WM.WM_MOUSEMOVE, Win32.HT.HTCAPTION, Win32.MakeLParam(Convert.ToInt32(MousePoint.X), Convert.ToInt32(MousePoint.Y)));

    DONE! Works fine for me. Resizing (borderless or not) worked great so there was no need to fixing it.


  8. Ricibald Says:

    Thanks Gawi for your contribution!

  9. Gawi Says:

    Hi again!

    I have another fix for sticky borderless windows under WPF.
    We had two windows where 1st was a main, and the 2nd was a child. The child stickiness was applied as usual (see my previous response) and it snapped to the others/screen edges properly but there was a problem with resizing it. As in my previous response we had to call resize event manually and WPF provides such event – SizeChanged.


    this.SizeChanged += new SizeChangedEventHandler(YourWindow_SizeChanged);

    And the same as before (with different parameters):

    void YourWindow_SizeChanged(object sender, SizeChangedEventArgs e)
    if (Mouse.LeftButton == MouseButtonState.Pressed)
    System.Windows.Point MousePoint = Mouse.GetPosition(this);
    System.Windows.Point ScreenPoint = this.PointToScreen(MousePoint);

    Win32.SendMessage(stickyWindow.Handle, Win32.WM.WM_NCLBUTTONDOWN, Win32.HT.HTBOTTOMRIGHT, Win32.MakeLParam(Convert.ToInt32(ScreenPoint.X), Convert.ToInt32(ScreenPoint.Y)));
    Win32.SendMessage(stickyWindow.Handle, Win32.WM.WM_MOUSEMOVE, Win32.HT.HTCAPTION, Win32.MakeLParam(Convert.ToInt32(MousePoint.X), Convert.ToInt32(MousePoint.Y)));

    And it works :)

    We had another problem with maximizing the child window and with hiding it.

    The solution for hiding/showing child window is to disable stickywindow stickiness parameters on hide and enable them on show. If not, our other sticky visible windows will snap to currently hidden windows borders.

    And with maximizing the minimized window remember to have

    if (Mouse.LeftButton == MouseButtonState.Pressed) {

    condition in every manually called resize/change_location event, or it will fly on your screen madly.

    Hope that helps.

  10. Jonah Says:

    Don’t forget to add

    if (widthHeightPointConverted.X >= 0.0f && widthHeightPointConverted.Y >= 0.0f)
    _window.Width = widthHeightPointConverted.X;
    _window.Height = widthHeightPointConverted.Y;

    in WpfFormAdapter.cs Bounds Set attribute or it will crash occasionally.

  11. Faz Says:

    Does this work? Just downloaded and ran it up and when I snap the child window to the parent, resizing/moving only affects the selected windows, they don’t resize/move in sync.

  12. how can i move sticky / snapping wpf windows | Says:

    [...] Snapping / Sticky / Magnetic Windows for WPF [...]

  13. Dave Says:

    Hi i added this to my project with the extra tweaks it works nicely except for a little jumpiness but my problem is after minimizing my app then restoring it when i hover over window it fly’s off the screen.

    Gawi Says: condition in every manually called resize/change_location event, or it will fly on your screen madly.

    this is what its doing but yet ive added the if (Mouse.LeftButton == MouseButtonState.Pressed) to all my main controls and also added a winproc to detect the changes and inside those told set sticky to false what else do i need to do to get it to not fly around

  14. Alexandru Says:

    I was lloking to find a way to disable aero snap for a wpf window for 3 days. And now I have found your example. Your solution is awesome ! Congratulations.

  15. Alexandru Says:

    The solution proposed by Gawi is not working for borderless windows in WPF. :(

  16. Susi Says:

    I am not certain the place you are getting your info, however good topic. I must spend some time learning much more or working out more. Thank you for great information I was looking for this information for my mission.

  17. Rush Says:

    There is a bug in WpfFormAdapter when getting the MaximumSize. If MaxHeight or MaxWidth are Infinity a StackOverflow exception occurs. Fix:

    public Size MaximumSize
    var width = _window.MaxWidth;
    var height = _window.MaxHeight;
    if (double.IsInfinity(width))
    width = int.MaxValue;
    if (double.IsInfinity(height))
    height = int.MaxValue;
    return new Size(Convert.ToInt32(width), Convert.ToInt32(height));
    set {
    _window.MaxWidth = value.Width;
    _window.MaxHeight = value.Height;

  18. how to use whatsapp Says:

    What’s up, I desire to subscribe for this website to obtain most up-to-date updates, therefore where can i do it please help.

  19. kik for computer Says:

    hey there and thank you for your info – I’ve certainly picked up anything new from right here. I did however expertise a few technical points using this web site, since I experienced to reload the website many times previous to I could get it to load correctly. I had been wondering if your web hosting is OK? Not that I’m complaining, but sluggish loading instances
    times will very frequently affect your placement in
    google and can damage your high quality score if ads and marketing
    with Adwords. Anyway I am adding this RSS to my email and can look out for much more of
    your respective exciting content. Make sure you update this again soon.

  20. adult party swinger Says:

    must be ddf as i’m and favor you to become shaved or trimmed, yet if not its no large any price attraction may be the crucial including always.We can see exactly where it leads yet I hope that you’re into exploring our bodies and attempting new things.D/d totally free 420 friendly, Hmu and we’ll develop it happenCheers.However, I simply possess a thing for females in stockings / tights / thigh highs.

  21. descargar wasap Says:

    I’ll right away grasp your rss as I can’t find your email subscription link or newsletter service. Do you have any? Kindly permit me recognise so that I may subscribe. Thanks.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>