c# - Wpf 自定义窗口,Windows 边缘调整大小功能

标签 c# wpf windows

有一个自定义的 wpf 窗口(WindowStyle=None,AllowTransparancy=true)并且想知道如何使 Windows 边缘调整大小功能起作用..你知道当拖动窗口和鼠标触摸屏幕的左、右或顶部边缘(甚至角落W10).

尝试查看 WM notification但他们似乎都不是我要找的..

要清楚,不是普通的窗口调整大小..而是拖动到屏幕边缘并让 Windows 将其调整为一半/全部/四分之一(认为它称为 Aero Snap)。我可以通过普通的调整大小调用来做到这一点,但这不会显示透明预览窗口,也不会在触摸边缘时在鼠标上放置动画。

谢谢

最佳答案

第一步

为矩形创建样式(在 <Window1.Resources> 中)作为窗口周围的 Grip 区域:

<Style x:Key="RectBorderStyle" TargetType="Rectangle">
    <Setter Property="Focusable" Value="False" />
    <Setter Property="Fill" Value="Transparent" />
    <Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource AncestorType=Window}}" />
    <EventSetter Event="MouseLeftButtonDown" Handler="Resize_Init"/>
    <EventSetter Event="MouseLeftButtonUp" Handler="Resize_End"/>
    <EventSetter Event="MouseMove" Handler="Resizeing_Form"/>
</Style>

第二步

将这些样式化的矩形添加到您的窗口中。 (您可以将它们添加到窗口内的简单网格中)

<Rectangle x:Name="leftSizeGrip"
    Width="7"
    HorizontalAlignment="Left"
    Cursor="SizeWE"
    Style="{StaticResource RectBorderStyle}" />
<Rectangle x:Name="rightSizeGrip"
    Width="7"
    HorizontalAlignment="Right"
    Cursor="SizeWE"
    Style="{StaticResource RectBorderStyle}" />
<Rectangle x:Name="topSizeGrip"
    Height="7"
    VerticalAlignment="Top"
    Cursor="SizeNS"
    Style="{StaticResource RectBorderStyle}" />
<Rectangle x:Name="bottomSizeGrip"
    Height="7"
    VerticalAlignment="Bottom"
    Cursor="SizeNS"
    Style="{StaticResource RectBorderStyle}" />
<!--  Corners  -->
<Rectangle Name="topLeftSizeGrip"
    Width="7"
    Height="7"
    HorizontalAlignment="Left"
    VerticalAlignment="Top"
    Cursor="SizeNWSE"
    Style="{StaticResource RectBorderStyle}" />
<Rectangle Name="bottomRightSizeGrip"
    Width="7"
    Height="7"
    HorizontalAlignment="Right"
    VerticalAlignment="Bottom"
    Cursor="SizeNWSE"
    Style="{StaticResource RectBorderStyle}" />
<Rectangle Name="topRightSizeGrip"
    Width="7"
    Height="7"
    HorizontalAlignment="Right"
    VerticalAlignment="Top"
    Cursor="SizeNESW"
    Style="{StaticResource RectBorderStyle}" />
<Rectangle Name="bottomLeftSizeGrip"
    Width="7"
    Height="7"
    HorizontalAlignment="Left"
    VerticalAlignment="Bottom"
    Cursor="SizeNESW"
    Style="{StaticResource RectBorderStyle}" />

第三步

将这些代码添加到窗口后面的代码 (window1.xaml.cs) (或者添加到 MyStyle.xaml.cs,如果您正在使用窗口模板并且您已经在模板中添加了 8 个矩形)

#region ResizeWindows
bool ResizeInProcess = false;
private void Resize_Init(object sender, MouseButtonEventArgs e)
{
    Rectangle senderRect = sender as Rectangle;
    if (senderRect != null)
    {
        ResizeInProcess = true;
        senderRect.CaptureMouse();
    }
}

private void Resize_End(object sender, MouseButtonEventArgs e)
{
    Rectangle senderRect = sender as Rectangle;
    if (senderRect != null)
    {
        ResizeInProcess = false; ;
        senderRect.ReleaseMouseCapture();
    }
}

private void Resizeing_Form(object sender, MouseEventArgs e)
{
    if (ResizeInProcess)
    {
        Rectangle senderRect = sender as Rectangle;
        Window mainWindow = senderRect.Tag as Window;
        if (senderRect != null)
        {
            double width = e.GetPosition(mainWindow).X;
            double height = e.GetPosition(mainWindow).Y;
            senderRect.CaptureMouse();
            if (senderRect.Name.ToLower().Contains("right"))
            {
                width += 5;
                if (width > 0)
                    mainWindow.Width = width;
            }
            if (senderRect.Name.ToLower().Contains("left"))
            {
                width -= 5;
                mainWindow.Left += width;
                width = mainWindow.Width - width;
                if (width > 0)
                {
                    mainWindow.Width = width;
                }
            }
            if (senderRect.Name.ToLower().Contains("bottom"))
            {
                height += 5;
                if (height > 0)
                    mainWindow.Height = height;
            }
            if (senderRect.Name.ToLower().Contains("top"))
            {
                height -= 5;
                mainWindow.Top += height;
                height = mainWindow.Height - height;
                if (height > 0)
                {
                    mainWindow.Height = height;
                }
            }
        }
    }
}
#endregion

第四步

按 F5 享受吧!

注意事项:

这8个矩形是透明的。如果你不能让它正常工作,只需更改 Fill风格的值(value)Red查看它们的位置。

另注:

最大化时,您可能希望禁用所有这些矩形。最简单的方法是处理 WindowStateChanged事件并禁用包含它们的网格。

关于c# - Wpf 自定义窗口,Windows 边缘调整大小功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27157390/

相关文章:

C# 计时器在一定数量的滴答后自动停止

c# - 我正在使用 tinyMCE mceInsertContent 为什么要删除前斜杠?

c# - WCF Json 数据返回

c# - 为 EntitySet<T>、IQueryable<T> 和 IEnumerable<T> 创建可重用谓词

wpf - 命令绑定(bind)可以作用于 IsEnabled 以外的目标 UIElement 属性吗?

wpf - 突出显示搜索文本 block

wpf - XAML:在 VisualStateManager 类型中找不到可附加属性 VisualStateGroups

c# - 如何安全地实现从Windows服务到公共(public)WebApi的 Multi-Tenancy 访问

java - 32 位 DLL 导致 UnsatisfiedLinkError

c++ - 通过管道发送回车按钮输入