你如何在应用程序中禁用Aero对齐? [英] How do you disable Aero Snap in an application?
问题描述
是否有可能取消在WPF应用程序的Windows 7的自动窗口对接功能?
Is it possible to disable the automatic window-docking feature of Windows 7 in a WPF application?
推荐答案
最近,我需要做的这一个自定义的,可调整大小的 ResizeMode = CanResizeWithGrip
WPF无窗口装饰窗口(没有标题栏和按钮)。我用 DragMove()
来移动窗口,当它由AeroSnap最大化,窗口变得不可移动,从而锁定到位。
I recently needed to do this to a custom, resizable ResizeMode = CanResizeWithGrip
WPF window with no window decorations (no title bar and buttons). I used DragMove()
to move the window, and when It is maximized by AeroSnap, the window becomes unmovable and hence locked in place.
我试过谷仓猴子的解决方案,该部分工作,但它仍然会表现出AeroSnap图形和调整应用全屏大小。我修改了它的下方,现在它的工作原理期望:还可以调整大小,但没有AeroSnap可言
I tried Barn Monkey's solution, which partially worked, but it would still show the AeroSnap graphic and resize the app to fullscreen size. I modified it below and now it works as expect: still resizable, but no AeroSnap at all.
void Window1_MouseDown(object sender, MouseButtonEventArgs e)
{
if( e.LeftButton == MouseButtonState.Pressed )
{
// this prevents win7 aerosnap
if( this.ResizeMode != System.Windows.ResizeMode.NoResize )
{
this.ResizeMode = System.Windows.ResizeMode.NoResize;
this.UpdateLayout();
}
DragMove();
}
}
void Window1_MouseUp( object sender, MouseButtonEventArgs e )
{
if( this.ResizeMode == System.Windows.ResizeMode.NoResize )
{
// restore resize grips
this.ResizeMode = System.Windows.ResizeMode.CanResizeWithGrip;
this.UpdateLayout();
}
}
编辑:
这已经有一段时间,因为我写了这个,但因为人们还是看这个我会与我现在用的更新。我仍然使用基本为preventing边缘贴紧同样的方法和移动我的窗口,但我现在让他们挤进定制行为<>
我可以连接到班一个窗口
或用户控件
。这使得他们很容易与MVVM(我用微卡利)。
It's been a while since I wrote this, but since people still look at this I'll update it with what I use now. I still use basically the same method for preventing edge snapping and moving my windows, but I now have them packed into custom Behavior<>
classes that I can attach to a Window
or UserControl
. This makes them very easy to use with MVVM (I use Caliburn Micro).
行为类是:
/// <summary>
/// behavior that makes a window/dialog draggable by clicking anywhere
/// on it that is not a control (ie, button)
/// </summary>
public class DragMoveBehavior<T> : Behavior<T> where T : FrameworkElement
{
protected override void OnAttached()
{
AssociatedObject.MouseLeftButtonDown += MouseDown;
base.OnAttached();
}
protected override void OnDetaching()
{
AssociatedObject.MouseLeftButtonDown -= MouseDown;
base.OnDetaching();
}
void MouseDown( object sender, EventArgs ea ) => Window.GetWindow( sender as T )?.DragMove();
}
public class WinDragMoveBehavior : DragMoveBehavior<Window> { }
public class UCDragMoveBehavior : DragMoveBehavior<UserControl> { }
/// <summary>
/// behavior that makes a window/dialog not resizable while clicked. this prevents
/// the window from being snapped to the edge of the screen (AeroSnap). if DragMoveBehavior
/// is also used, this must be attached first.
/// </summary>
/// <typeparam name="T"></typeparam>
public class NoSnapBehavior<T> : Behavior<T> where T : FrameworkElement
{
ResizeMode lastMode = ResizeMode.NoResize;
protected override void OnAttached()
{
AssociatedObject.MouseLeftButtonDown += MouseDown;
AssociatedObject.MouseLeftButtonUp += MouseUp;
base.OnAttached();
}
protected override void OnDetaching()
{
AssociatedObject.MouseLeftButtonDown -= MouseDown;
AssociatedObject.MouseLeftButtonUp -= MouseUp;
base.OnDetaching();
}
/// <summary>
/// make it so the window can be moved by dragging
/// </summary>
void MouseDown( object sender, EventArgs ea )
{
var win = Window.GetWindow( sender as T );
if( win != null && win.ResizeMode != ResizeMode.NoResize )
{
lastMode = win.ResizeMode;
win.ResizeMode = ResizeMode.NoResize;
win.UpdateLayout();
}
}
void MouseUp( object sender, EventArgs ea )
{
var win = Window.GetWindow( sender as T );
if( win != null && win.ResizeMode != lastMode )
{
win.ResizeMode = lastMode;
win.UpdateLayout();
}
}
}
public class WinNoSnapBehavior : NoSnapBehavior<Window> { }
public class UCNoSnapBehavior : NoSnapBehavior<UserControl> { }
然后我将它们连接到我的对话框中查看与:
I then attach them to my dialog box Views with:
<UserControl ...
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:utli:="...">
<i:Interaction.Behaviors>
<util:UCNoSnapBehavior/>
<util:UCDragMoveBehavior/>
</i:Interaction.Behaviors>
...
</UserControl>
和它只是工作!
这篇关于你如何在应用程序中禁用Aero对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!