• C# wpf 使用DockPanel实现截屏框



    前言

    做桌面客户端的时候有时需要实现截屏功能,能够在界面上框选截屏,做一个蒙版然后中间选框透明可以移动和改变大小。这个功能是不太好实现的,需要一定的方法,其中使用DockPanel是相对简单直接的实现。


    一、如何实现?

    我们按照如下步骤即可实现一个截屏窗口

    1、设置透明窗口

    首先窗口必须是无边框的透明窗口,我们这里不使用Transparency,因为对性能影响比较大。我们使用WindowChrome实现无边框透明窗口。

    <Window x:Class="WpfApp4.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp4"
            mc:Ignorable="d"
            Title="MainWindow" Height="720" Width="1280"
            Background="{x:Null}"
            ResizeMode="NoResize"
            WindowStyle="None"
            WindowState="Maximized"
            >
        <WindowChrome.WindowChrome>
            <WindowChrome GlassFrameThickness="-1"   CaptionHeight="0"   />
        </WindowChrome.WindowChrome>
    </Window>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    2、使用DockPanel

    在窗口中定义一个DockPanel控件作为父级,定义4个方位填充控件以及中间截屏框。

    <DockPanel>
        <Grid x:Name="leftPanel"     Width="400"   DockPanel.Dock="Left" Background="#80000000"></Grid>
        <Grid x:Name="topPanel"      Height="200"  DockPanel.Dock="Top" Background="#80000000"></Grid>
        <Grid x:Name="rightPanel"    Width="400"   DockPanel.Dock="Right" Background="#80000000"></Grid>
        <Grid x:Name="bottomPanel"   Height="200" DockPanel.Dock="Bottom" Background="#80000000"></Grid>
        <Grid x:Name="clipRect"  MouseDown="Button_MouseDown"  MouseMove="Button_MouseMove"   MouseUp="Button_MouseUp" Background="Transparent">
            <Grid.Resources>
                <Style TargetType="Thumb">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="Thumb">
                                <Border BorderBrush="Gray" BorderThickness="2" CornerRadius="8"  Background="White"></Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Grid.Resources>
            <!--左-->
            <Thumb  Margin="-8,0,0,0" Width="16" Height="16" HorizontalAlignment="Left" VerticalAlignment="Center" Cursor="SizeWE"   DragDelta  ="Thumb_DragDelta"/>
            <!--上-->
            <Thumb  Margin="0,-8,0,0" Width="16" Height="16" HorizontalAlignment="Center" VerticalAlignment="Top" Cursor="SizeNS" DragDelta  ="Thumb_DragDelta"/>
            <!--右-->
            <Thumb  Margin="0,0,-8,0" Width="16" Height="16" HorizontalAlignment="Right" VerticalAlignment="Center" Cursor="SizeWE" DragDelta  ="Thumb_DragDelta"/>
            <!--下-->
            <Thumb  Margin="0,0,0,-8" Width="16" Height="16" HorizontalAlignment="Center" VerticalAlignment="Bottom" Cursor="SizeNS" DragDelta  ="Thumb_DragDelta"/>
            <!--左上-->
            <Thumb  Margin="-8,-8,0,0" Width="16" Height="16" HorizontalAlignment="Left" VerticalAlignment="Top" Cursor="SizeNWSE" DragDelta  ="Thumb_DragDelta"/>
            <!--右上-->
            <Thumb  Margin="0,-8,-8,0" Width="16" Height="16" HorizontalAlignment="Right" VerticalAlignment="Top"  Cursor="SizeNESW" DragDelta  ="Thumb_DragDelta"/>
            <!--右下-->
            <Thumb  Margin="0,0,-8,-8" Width="16" Height="16" HorizontalAlignment="Right" VerticalAlignment="Bottom"  Cursor="SizeNWSE"  DragDelta  ="Thumb_DragDelta"/>
            <!--左下-->
            <Thumb  Margin="-8,0,0,-8" Width="16" Height="16" HorizontalAlignment="Left" VerticalAlignment="Bottom" Cursor="SizeNESW" DragDelta  ="Thumb_DragDelta"/>
        </Grid>
    </DockPanel>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    效果预览
    在这里插入图片描述

    3、实现拖动逻辑

    (1)注册事件

    与Grid的拖动实现非常类似,4个方位的控件可以当成margin使用,上一步的截屏框注册3个鼠标事件

     <Grid x:Name="clipRect"  MouseDown="Button_MouseDown"  MouseMove="Button_MouseMove"   MouseUp="Button_MouseUp" Background="Transparent">
    
    • 1

    (2)拖动逻辑

    将4个方位的控件的宽高组成一个margin,代入《C# wpf 实现Grid内控件拖动》即可,只需要注意边界处理(宽高不能为负数),此处不贴具体代码。下面是Button_MouseDown的部分代码示例,以此类推即可。

    _mouseDownMargin = new Thickness(leftPanel.ActualWidth, topPanel.ActualHeight, rightPanel.ActualWidth, bottomPanel.ActualHeight);
    
    • 1

    4、实现拖动调大小逻辑

    (1)注册事件

    给界面中的8个Thumb注册同一个Thumb_DragDelta事件。

    <Thumb  Margin="-8,0,0,0" Width="16" Height="16" HorizontalAlignment="Left" VerticalAlignment="Center" Cursor="SizeWE"   DragDelta  ="Thumb_DragDelta"/>
    
    • 1

    (2)实现逻辑

    将4个方位的控件的宽高组成一个margin,代入《C# wpf Grid中实现控件拖动调整大小》的Thumb_DragDelta即可,只需要注意边界处理(宽高不能为负数)。此处不贴具体代码。下面是Thumb_DragDelta的部分代码示例,以此类推即可。

    if (thumb.HorizontalAlignment == HorizontalAlignment.Left)
    {
        right = rightPanel.ActualWidth;
        left = leftPanel.ActualWidth + e.HorizontalChange;
        width = (double.IsNaN(c.Width) ? c.ActualWidth : c.Width) - e.HorizontalChange;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    二、完整代码

    https://download.csdn.net/download/u013113678/85841950


    三、效果预览

    在这里插入图片描述


    总结

    以上就是今天要讲的内容,本文简单介绍截屏框的实现,曾经为了事件这个界面功能花了不少精力,尤其是计算控件宽度以及位置关系,需要仔细计算。本文实现的截屏界面效果是可以的,拖动也是流畅的,完全满足一般项目的使用。

  • 相关阅读:
    矿区安全检查VR模拟仿真培训系统更全面、生动有效
    Vulnhub DC2
    【一种使用浏览器读取本地excel、josn等数据文件的方法】Python+JavaScript+HTML实现
    FPGA 20个例程篇:9.DDR3内存颗粒初始化写入并通过RS232读取(下)
    017-$route、$router
    chown命令应用和chmod命令应用
    基于Expression Lambda表达式树的通用复杂动态查询构建器——《原型篇一》[已开源]
    vue:write-excel-file页面文字转为xlsx文件
    1688阿里巴巴中国站电商数据官方平台API接口按图搜索1688商品(拍立淘)响应示例说明
    k8s-集群里的三种IP(NodeIP、PodIP、ClusterIP)
  • 原文地址:https://blog.csdn.net/u013113678/article/details/125550292