Is it possible to disable the automatic window-docking feature of Windows 7 in a WPF application?
I was using anthony's solution for awhile, but if you switch ResizeMode the window would temporary remove the sizing border, which is a little anoying. Here's another solution. By setting the WS_OVERLAPPEDWINDOW flag and removing the WS_THICKFRAME flag will disable Aero Snap feature for the window while not temporary removing the sizing border. You can play around with the styles to get the exact style you need, but key is removing the WS_THICKFRAME flag.
public enum WindowStyles: int
{
WS_BORDER = 0x00800000,
WS_CAPTION = 0x00C00000,
WS_CHILD = 0x40000000,
WS_CHILDWINDOW = 0x40000000,
WS_CLIPCHILDREN = 0x02000000,
WS_CLIPSIBLINGS = 0x04000000,
WS_DISABLED = 0x08000000,
WS_DLGFRAME = 0x00400000,
WS_GROUP = 0x00020000,
WS_HSCROLL = 0x00100000,
WS_ICONIC = 0x20000000,
WS_MAXIMIZE = 0x01000000,
WS_MAXIMIZEBOX = 0x00010000,
WS_MINIMIZE = 0x20000000,
WS_MINIMIZEBOX = 0x00020000,
WS_OVERLAPPED = 0x00000000,
WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
WS_POPUP = unchecked((int)0x80000000),
WS_POPUPWINDOW = WS_POPUP | WS_BORDER | WS_SYSMENU,
WS_SIZEBOX = 0x00040000,
WS_SYSMENU = 0x00080000,
WS_TABSTOP = 0x00010000,
WS_THICKFRAME = 0x00040000,
WS_TILED = 0x00000000,
WS_TILEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
WS_VISIBLE = 0x10000000,
WS_VSCROLL = 0x00200000,
}
int newWinLongStyle = 0;
newWinLongStyle |= (int)WindowStyles.WS_OVERLAPPEDWINDOW;
newWinLongStyle ^= (int)WindowStyles.WS_THICKFRAME;
WindowInteropHelper helper = new WindowInteropHelper(this);
NativeMethods.SetWindowLong(helper.Handle, (int)WindowStyles.GWL_STYLE, newWinLongStyle);
Setting "this.MaximizeBox = false" actually breaks Snap for me on Windows 10. Maybe this will work for you?
I needed to detect Windows 7 Aero snaps/docks to prevent window-size changes on a WPF application. During my search, I stumbled upon this post and found the answer given by anthony very helpful.
Following is what worked for me.
private void DisplayWindow_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Released)
{
this.ResizeMode = System.Windows.ResizeMode.CanResizeWithGrip;
}
}
private void DisplayWindow_LocationChanged(object sender, EventArgs e)
{
this.ResizeMode = System.Windows.ResizeMode.NoResize;
}
The window's XAML had the ResizeMode="CanResizeWithGrip"
setting.
Edit:
My response was not handle the Windows 7 Aero snap properly. bjo's response elegantly solved the problem for me.
In Ease of Access in control panel, choose
Make it easier to focus on tasks
and tick
Prevent windows from being automatically arranged when moved to the edge of the screen
DragMove suspends the UI Thread. That code also works.
void Window1_MouseDown(object sender, MouseButtonEventArgs e)
{
if( e.LeftButton == MouseButtonState.Pressed )
{
// this prevents win7 aerosnap
this.ResizeMode = System.Windows.ResizeMode.NoResize;
this.UpdateLayout();
DragMove();
// restore resize grips
this.ResizeMode = System.Windows.ResizeMode.CanResizeWithGrip;
this.UpdateLayout();
}
}