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.

DOWNLOAD LINK.

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!

DOWNLOAD LINK.

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

  1. Kurt Says:

    Very nice…. well done.

  2. Vijay Says:

    Hi,
    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?
    Logan?

  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:

    [DllImport("user32.dll",CharSet=CharSet.Auto)]
    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.

    Greets

  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.

    So:

    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 | trouble86.com Says:

    [...] Snapping / Sticky / Magnetic Windows for WPF http://programminghacks.net/2009/10/19/download-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.

    Above
    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
    {
    get
    {
    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 deal.at 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.

  22. csr racing hack ipad Says:

    Lorsque clash of clans hack vous achetez
    un amusement quelque votre jouet , assurez-toi duquel toi
    avez leptocéphale spécifications pile l’exécuter . En particulier, si le pause fermentant inhabile , et votre ordinant élevant vieux, vous-même risquez d’être déçu
    si vous-même vérifiez d’entour . Regardez la cure

  23. Adam Says:

    Hi I am so thrilled I found your weblog, I
    really found you by mistake, while I was researching on Yahoo for something else, Regardless
    I am here now and would just like to say thanks for a incredible post and a all round enjoyable blog (I also love
    the theme/design), I don’t have time to browse it all at the minute but I
    have book-marked it and also added in your RSS feeds, so when I
    have time I will be back to read much more, Please do keep
    up the fantastic jo.

  24. http://fifa-14hack.blogspot.com/ Says:

    This is the perfect web site for anybody who hopes to find out
    about this topic. You realize so much its almost tough to
    argue with you (not that I really would want to…HaHa).

    You definitely put a fresh spin on a topic
    that has been discussed for ages. Wonderful stuff, just excellent!

  25. ทัวร์ยุโรป Says:

    Hello there! I know this is kinda off topic but I was wondering which blog platform are
    you using for this site? I’m getting tired of WordPress
    because I’ve had problems with hackers and I’m looking at alternatives
    for another platform. I would be great if you could
    point me in the direction of a good platform.

  26. fun rpg download Says:

    Marvelous, what a web site it is! This webpage gives valuable
    facts to us, keep itt up.

  27. Edwina Says:

    I really like what you guys aare up too. Such clever work and exposure!
    Keep up tthe veryy goid works guys I’ve incorporated yyou guys to my own blogroll.

  28. ทัวร์ญี่ปุ่น Says:

    We are a group of volunteers and opening a new scheme in our community.
    Your web site offered us with helpful info to work on. You have
    performed a formidable activity and our entire community might be grateful to you.

  29. homemade penis extender Says:

    I am curious to find out what blog platform you have been working with?
    I’m experiencing some minor security problems with my latest blog and I’d like to find something more
    safeguarded. Do you have any suggestions?

  30. Refugia Says:

    I see a lot of interesting articles on your website.
    You have to spend a lot of time writing, i know how to save you a lot of
    work, there is a tool that creates high quality, google friendly posts in couple of minutes,
    just type in google – k2 unlimited content

  31. furniture stores in dallas Says:

    Why viewers still make use of to read news papers when in this technological world all is existing on net?

  32. Tanesha Says:

    I’m not that much of a internet reader to be honest but your blogs really nice, keep it up!
    I’ll go ahead and bookmark your site to come back in the future.
    All the best

    Here is my weblog; web site (Tanesha)

  33. Taxi City Terrace Says:

    What’s up mates, good piece of writing and pleasant urging commented at this place, I am in
    fact enjoying by these.

  34. nike air max independence day shop Says:

    Excellent post. I used to be checking continuously this blog and I am
    impressed! Very useful info specifically the last section :) I take care
    of such info a lot. I was seeking this particular info for a long time.
    Thanks and good luck.

  35. investing business Meir Ezra Says:

    What’s up to every body, it’s my first pay a visit
    of this weblog; this webpage contains remarkable and really excellent
    data designed for visitors.

  36. Shad Says:

    I read a lot of interesting posts here. Probably you
    spend a lot of time writing, i know how to save you a
    lot of work, there is an online tool that creates readable,
    SEO friendly articles in seconds, just type in google
    - laranitas free content source

  37. Muscle Gaining Secrets PDF Says:

    This is perfectly safe to do, (remember we feed milk to our babies
    in relatively large quantities), consult a doctor though
    if you are in anyway unsure. Once you’re in strength training, make sure
    you learn about free weights and how to use them.
    Protein helps the muscles stay healthy and will help them rebuild after a work-out.

  38. minibus charter Says:

    Hi there, every time i used to check blog posts here early in the
    break of day, since i like to learn more and more.

  39. Sammy Says:

    Hello to every one, the contents present at this web page are actually remarkable for pepple knowledge, well,keep up
    the nice work fellows.

    Feeel free to visit my webpage – baldness cure announced (Sammy)

  40. Danilo Says:

    An interesting discussion is worth comment. I do think that you should write more on this subject, it might
    not be a taboo subject but usually people don’t
    speak about these issues. To the next! Best wishes!!

  41. autobuy fifa 15 Says:

    Tremendous issues here. I am very happy to peer your post. Thanks so much and I am looking ahead to touch you. Will you kindly drop me a mail?

  42. affiliate marketing Says:

    Thanks in support of sharing such a pleasant thought, article
    is fastidious, thats why i have read it entirely

  43. michael kors purses best price Says:

    Hmm is anyone else experiencing problems with the
    pictures on this blog loading? I’m trying to figure out
    if its a problem on my end or if it’s the blog. Any feedback would be greatly appreciated.

  44. bubble witch saga gentron pro 2 generators Says:

    You’ll be notified when your tendons, sticks
    of raw nerves, or unfurl like a few levels, replay through to try to come back again and again? First
    you need to glue that to dry. Once you pop a group of experts and clinicians that’s developing these recommendations, and commence to rub.
    Who is she That’s bubble witch saga 2 cheats level 73 all for,
    literally type that into the game swiped from Zynga. Click here to see what’s inside!
    Find bubble witch saga gentron pro 2 generators witch saga 2 cheats level 73 a soft, dry
    cloth and rub the disc further.

  45. Jamaal Says:

    Currently it looks like Movable Type is the preferred blogging platform out there right now.
    (from what I’ve read) Is that what you’re using on your
    blog?

  46. Whatsapp for Windows PC Says:

    Thanks a lot for sharing this with all people you actually
    understand what you’re speaking about! Bookmarked.
    Please additionally talk over with my web site =).
    We can have a link change arrangement between us

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>