WPF无边框窗口鼠标拖动缩放大小

通常,我们会 通过AllowsTransparency=”True”、 WindowStyle=”None” 这两个属性将wpf窗口的边框去掉,由于边框没了,我们就不能通过鼠标指针悬停在某一边上拖动改变窗口的大小,此时若要能调整窗口大小,官方倒也提供了个属性:ResizeMode=”CanResizeWithGrip”,这么一设置,我们会发现窗口右下角多出一个三角标记,此时可以将鼠标指正悬停在这个标记上,进而拖动改变窗口的大小,这确实能解决问题,但是产品经理那过不了啊= =,“别的软件都可以在边框上调整呀”,Orz…

百度一下,发现好多方案都是说监听WM_NCHITTEST类型的消息,然后提供了个鼠标位置的枚举,然后就没有然后了,这什么鬼,现在鼠标放到“边框”上都不会变样式唉,你监听那个有啥用= =然后又看到有兄弟在窗口周围添加4个边4个角来处理鼠标的拖动,感觉这个虽然听起来不高大上,但应该可行,开干……

首先,我们不可能在每一个窗口xaml里都加上边角,而是要写一个窗口模板,然后让需要能缩放的窗口继承这个模板就行了。
ok,建立一个窗口模板的资源文件:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ControlTemplate x:Key="CustomWindowTemplete" TargetType="Window">
        <Border BorderBrush="Transparent" BorderThickness="12" x:Name="outBorder">
            <Border.Effect>
                <DropShadowEffect BlurRadius="15" Color="#000000" Opacity=".25" Direction="90" ShadowDepth="1"/>
            </Border.Effect>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="1"/>
                    <RowDefinition/>
                    <RowDefinition Height="1"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1"/>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="1"/>
                </Grid.ColumnDefinitions>
                <Grid Grid.Row="1" Grid.Column="1" Background="White" >
                    <AdornerDecorator>
                        <ContentPresenter></ContentPresenter>
                    </AdornerDecorator>
                </Grid>
                <Rectangle Name="ResizeTopLeft" Fill="{StaticResource WindowBorderColor}" Grid.Row="0" Grid.Column="0" Opacity=".25"/>
                <Rectangle Name="ResizeTop" Fill="{StaticResource WindowBorderColor}" Grid.Row="0" Grid.Column="1" Opacity=".25"/>
                <Rectangle Name="ResizeTopRight" Fill="{StaticResource WindowBorderColor}" Grid.Row="0" Grid.Column="2" Opacity=".25"/>
                <Rectangle Name="ResizeLeft" Fill="{StaticResource WindowBorderColor}" Grid.Row="1" Grid.Column="0" Opacity=".25"/>
                <Rectangle Name="ResizeRight" Fill="{StaticResource WindowBorderColor}" Grid.Row="1" Grid.Column="2" Opacity=".25"/>
                <Rectangle Name="ResizeBottomLeft" Fill="{StaticResource WindowBorderColor}" Grid.Row="2" Grid.Column="0" Opacity=".25"/>
                <Rectangle Name="ResizeBottom" Fill="{StaticResource WindowBorderColor}" Grid.Row="2" Grid.Column="1" Opacity=".25"/>
                <Rectangle Name="ResizeBottomRight" Fill="{StaticResource WindowBorderColor}" Grid.Row="2" Grid.Column="2" Opacity=".25"/>
            </Grid>
        </Border>
    </ControlTemplate>

    <Style x:Key="CustomWindow" TargetType="Window">
        <Setter Property="AllowsTransparency" Value="True"/>
        <Setter Property="WindowStyle" Value="None"/>
        <Setter Property="Template" Value="{StaticResource CustomWindowTemplete}"></Setter>
    </Style>
</ResourceDictionary>

以上代码,在Grid外还套了一个Border,是为了实现边框阴影效果;Rectangle的Opacity属性都是0.25,设置这个透明度是为了让我们自定义的这个边框看起来不明显,如果不设这个透明度的话,窗口四周就会明显有一个像素为1的框,当然这个看你需求了,如果你窗口四周本来就是有线框而不是阴影,那就不用Border和这个Opacity了。
别忘了在App.xaml里要引入这个资源文件:

 <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/CustomColor.xaml"/>
                <ResourceDictionary Source="Resources/VcreditWindow.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

再新建一个基类窗口:

 public class VcreditWindowBehindCode : Window  
    {
        public const int WM_SYSCOMMAND = 0x112;
        public HwndSource HwndSource;

public Dictionary<ResizeDirection, Cursor> cursors = new Dictionary<ResizeDirection, Cursor>
{
{ResizeDirection.Top, Cursors.SizeNS},
{ResizeDirection.Bottom, Cursors.SizeNS},
{ResizeDirection.Left, Cursors.SizeWE},
{ResizeDirection.Right, Cursors.SizeWE},
{ResizeDirection.TopLeft, Cursors.SizeNWSE},
{ResizeDirection.BottomRight, Cursors.SizeNWSE},
{ResizeDirection.TopRight, Cursors.SizeNESW},
{ResizeDirection.BottomLeft, Cursors.SizeNESW}
};

        public enum ResizeDirection
        {
            Left = 1,
            Right = 2,
            Top = 3,
            TopLeft = 4,
            TopRight = 5,
            Bottom = 6,
            BottomLeft = 7,
            BottomRight = 8,
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);


        public VcreditWindowBehindCode()
        {
            this.SourceInitialized += VcreditWindowBehindCode_SourceInitialized;
            this.Loaded += VcreditWindowBehindCode_Loaded;
            this.MouseMove += VcreditWindowBehindCode_MouseMove;
        }

        void VcreditWindowBehindCode_MouseMove(object sender, MouseEventArgs e)
        {
            if (Mouse.LeftButton != MouseButtonState.Pressed)
            {
                FrameworkElement element = e.OriginalSource as FrameworkElement;
                if (element != null && !element.Name.Contains("Resize"))
                {
                    this.Cursor = Cursors.Arrow;
                }
            }
        }

        void VcreditWindowBehindCode_SourceInitialized(object sender, EventArgs e)
        {
            this.HwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
        }

        void VcreditWindowBehindCode_Loaded(object sender, RoutedEventArgs e)
        {
            ControlTemplate customWindowTemplate = App.Current.Resources["CustomWindowTemplete"] as ControlTemplate;
            if (customWindowTemplate!=null)
            {
                var TopLeft = customWindowTemplate.FindName("ResizeTopLeft", this) as Rectangle;
                TopLeft.MouseMove += ResizePressed;
                TopLeft.MouseDown += ResizePressed;
                var Top = customWindowTemplate.FindName("ResizeTop", this) as Rectangle;
                Top.MouseMove += ResizePressed;
                Top.MouseDown += ResizePressed;
                var TopRight = customWindowTemplate.FindName("ResizeTopRight", this) as Rectangle;
                TopRight.MouseMove += ResizePressed;
                TopRight.MouseDown += ResizePressed;
                var Left = customWindowTemplate.FindName("ResizeLeft", this) as Rectangle;
                Left.MouseMove += ResizePressed;
                Left.MouseDown += ResizePressed;
                var Right = customWindowTemplate.FindName("ResizeRight", this) as Rectangle;
                Right.MouseMove += ResizePressed;
                Right.MouseDown += ResizePressed;
                var BottomLeft = customWindowTemplate.FindName("ResizeBottomLeft", this) as Rectangle;
                BottomLeft.MouseMove += ResizePressed;
                BottomLeft.MouseDown += ResizePressed;
                var Bottom = customWindowTemplate.FindName("ResizeBottom", this) as Rectangle;
                Bottom.MouseMove += ResizePressed;
                Bottom.MouseDown += ResizePressed;
                var BottomRight = customWindowTemplate.FindName("ResizeBottomRight", this) as Rectangle;
                BottomRight.MouseMove += ResizePressed;
                BottomRight.MouseDown += ResizePressed;
            }
        }

        public void ResizePressed(object sender, MouseEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            ResizeDirection direction = (ResizeDirection)Enum.Parse(typeof(ResizeDirection), element.Name.Replace("Resize", ""));
            this.Cursor = cursors[direction];
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                ResizeWindow(direction);
            }
        }

        public void ResizeWindow(ResizeDirection direction)
        {
            SendMessage(HwndSource.Handle, WM_SYSCOMMAND, (IntPtr)(61440 + direction), IntPtr.Zero);
        }
    }

注意构造函数里的3个事件,在这3个事件里分别做了一系列的初始化工作。这里真正改变窗口大小的代码其实是user32里的SendMessage方法。

下来就到了具体的窗口了:
在窗口属性里引入命名空间: xmlns:vcredit=”clr-namespace:VcreditChat.Resources”
然后把窗口最外层的标签Window替换成vcredit:VcreditWindowBehindCode,也就是刚刚那个基类窗口
再引用一下刚刚资源里定义的样式:Style=”{StaticResource CustomWindow}”
最后在窗口的后台代码里继承一下基类窗口类: public partial class ChatSingle : VcreditWindowBehindCode

这里写图片描述

以上就完成了鼠标在边框拖动调整无边框窗口大小的功能了,但是还有个问题,就是我们将Border的BorderThickness设置成了12,如果最大化窗口会发现,窗口离电脑屏幕边框还有12像素的空白,此时我们就要在最大化时通过后台代码动态设置BorderThickness=0,在窗口正常化时再恢复12。

猜你喜欢

转载自blog.csdn.net/leebin_20/article/details/78331604
今日推荐