bj0*_*bj0 17
我最近需要这样做一个自定义的,可调整大小的ResizeMode = CanResizeWithGripWPF窗口,没有窗口装饰(没有标题栏和按钮).我曾经DragMove()移动窗口,当它被AeroSnap最大化时,窗口变得不可移动,因此锁定到位.
我尝试了部分工作的Barn Monkey的解决方案,但它仍会显示AeroSnap图形并将应用程序调整为全屏尺寸.我在下面进行了修改,现在它按预期工作:仍可调整大小,但根本没有AeroSnap.
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();
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我写这篇文章已经有一段时间了,但是由于人们仍然看着这个,我会用我现在使用的东西来更新它.我仍然使用基本相同的方法来防止边缘捕捉和移动我的窗口,但我现在将它们打包到自定义Behavior<>类中,我可以附加到Window或UserControl.这使得它们很容易与MVVM一起使用(我使用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> { }
Run Code Online (Sandbox Code Playgroud)
然后我将它们附加到我的对话框中查看:
<UserControl ...
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:util:="...">
<i:Interaction.Behaviors>
<util:UCNoSnapBehavior/>
<util:UCDragMoveBehavior/>
</i:Interaction.Behaviors>
...
</UserControl>
Run Code Online (Sandbox Code Playgroud)
它只是有效!
如果您给出Win7的“粘滞便笺”示例,则可能已经注意到它没有标准的窗口边框。在此基础上,我只能告诉您,除了ResizeMode="NoResize"手动设置和处理调整大小行为外,没有其他直接方法。以下是我为快速入门而创建的一个非常基本的非专业解决方案,但是如果您愿意,可以附加更多功能:)
<Window
x:Class="WpfApplication1.Window1"
x:Name="window"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1"
Width="300"
Height="300"
ResizeMode="NoResize"
WindowStyle="None"
AllowsTransparency="True"
Background="Transparent"
WindowState="Maximized">
<Window.Resources>
<x:Array
x:Key="TextBlockList"
Type="{x:Type TextBlock}">
<TextBlock
Text="? Resize Horizontally by dragging right grip" />
<TextBlock
Text="? Resize Vertically by dragging bottom grip" />
<TextBlock
Text="? Move Horizontally by dragging left grip" />
<TextBlock
Text="? Move Verticallyby dragging top grip" />
</x:Array>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition
Height="Auto" />
<RowDefinition
Height="{Binding Height, Mode=OneWay, ElementName=window}" />
<RowDefinition
Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="Auto" />
<ColumnDefinition
Width="{Binding Width, Mode=OneWay, ElementName=window}" />
<ColumnDefinition
Width="Auto" />
</Grid.ColumnDefinitions>
<GridSplitter
Grid.Column="1"
Grid.Row="1"
HorizontalAlignment="Left"
MinWidth="5" />
<GridSplitter
Grid.Column="1"
Grid.Row="1"
HorizontalAlignment="Right"
MinWidth="5" />
<GridSplitter
Grid.Column="1"
Grid.Row="1"
VerticalAlignment="Top"
MinHeight="5"
ResizeDirection="Rows"
HorizontalAlignment="Stretch" />
<GridSplitter
Grid.Column="1"
Grid.Row="1"
VerticalAlignment="Bottom"
MinHeight="5"
ResizeDirection="Rows"
HorizontalAlignment="Stretch" />
<Border
Grid.Column="1"
Grid.Row="1"
Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"
Margin="5">
<Grid x:Name="root">
<ItemsControl
ItemsSource="{StaticResource TextBlockList}" />
</Grid>
</Border>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
您甚至可以制作一个控件(基本上是一个面板),该控件可以在其父画布中调整大小并移动。现在,可以将该控件填充到透明的最大化窗口中。这会让您对控件是一个不响应“窗口捕捉”并且不会停靠的窗口有错觉!
希望这可以帮助。
问候,
Mihir Gokani
我使用安东尼的解决方案有一段时间了,但是如果你切换 ResizeMode,窗口会暂时删除大小边框,这有点烦人。这是另一个解决方案。通过设置 WS_OVERLAPPEDWINDOW 标志并删除 WS_THICKFRAME 标志将禁用窗口的 Aero Snap 功能,同时不会临时删除尺寸边框。您可以尝试使用样式来获得所需的确切样式,但关键是删除 WS_THICKFRAME 标志。
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);
Run Code Online (Sandbox Code Playgroud)