Wpf 自定义窗口,Windows 边缘调整大小功能 [英] Wpf custom window, Windows edge resize feature

查看:24
本文介绍了Wpf 自定义窗口,Windows 边缘调整大小功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个自定义的 wpf 窗口(WindowStyle=None,AllowTransparancy=true),想知道如何让 Windows 边缘调整大小功能发挥作用.W10).

Got a custom wpf window (WindowStyle=None, AllowTransparancy=true) and wondering how to get Windows edge resize features to work.. you know when draging window and mouse touches left, right or top edge of screen (even corners in W10).

尝试查看 WM 通知 但似乎没有一个是我要找的..

Tried looking into WM notification but none of them seems to be what im looking for..

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

To clearify, not ordinary window resizeing.. but draging to screen edge and letting Windows resize it to half/full/quarter (think its called Aero Snap). And I can do it with ordinary resize calls but that doesnt show the transparent preview window or drop animation on mouse when touches edge.

谢谢

推荐答案

步骤 1

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

Step 1

Create a Style (in <Window1.Resources>) for rectangles as the Grip area around the window:

<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>

步骤 2

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

Step 2

Add these styled rectangles to your window. (You can add them inside a simple grid within your window)

<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}" />

步骤 3

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

#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

步骤 4

按 F5 享受吧!

Step 4

Press F5 and enjoy!

这 8 个矩形是透明的.如果你不能让它正常工作,只需将样式的 Fill 值更改为 Red 以查看它们的位置.

these 8 rectangles are transparent. if you can't make it properly work, just change the Fill value of the style to Red to see where are they positioned.

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

When maximized you might want to disable all these rectangles. well the easiest way is to handle the WindowStateChanged event and disable the grid containing them.

这篇关于Wpf 自定义窗口,Windows 边缘调整大小功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆