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!
January 7th, 2010 at 0:00
Very nice…. well done.
March 2nd, 2010 at 23:25
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.
June 4th, 2010 at 22:22
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.
July 28th, 2010 at 0:53
Snapping/Sticking working fine when moving the window.
It doesn’t work when resizing window.
July 28th, 2010 at 1:20
Try to maximize windows
June 1st, 2011 at 11:12
Ok, It’s great, but how can I hack it to get it working with borderless WPF Windows?
Logan?
June 2nd, 2011 at 11:17
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
June 3rd, 2011 at 11:27
Thanks Gawi for your contribution!
July 1st, 2011 at 11:45
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.
July 25th, 2011 at 7:56
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.
September 21st, 2011 at 19:11
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.
November 18th, 2011 at 1:27
[...] Snapping / Sticky / Magnetic Windows for WPF http://programminghacks.net/2009/10/19/download-snapping-sticky-magnetic-windows-for-wpf/ [...]
February 13th, 2012 at 15:45
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
July 12th, 2012 at 10:15
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.
July 18th, 2012 at 6:48
The solution proposed by Gawi is not working for borderless windows in WPF.
August 29th, 2012 at 9:30
INCHU chstextsel hatuk PETAKAN bank ayspisi PETAKAN varkeri hamar?
April 17th, 2013 at 4:57
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.