最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#?wpf使用DockPanel實現(xiàn)制作截屏框

 更新時間:2023年09月07日 08:44:41   作者:CodeOfCC  
做桌面客戶端的時候有時需要實現(xiàn)截屏功能,能夠在界面上框選截屏,本文就來為大家介紹一下wpf如何使用DockPanel制作截屏框吧,感興趣的可以了解下

前言

做桌面客戶端的時候有時需要實現(xiàn)截屏功能,能夠在界面上框選截屏,做一個蒙版然后中間選框透明可以移動和改變大小。這個功能是不太好實現(xiàn)的,需要一定的方法,其中使用DockPanel是相對簡單直接的實現(xiàn)。

一、如何實現(xiàn)

我們按照如下步驟即可實現(xiàn)一個截屏窗口

1、設置透明窗口

首先窗口必須是無邊框的透明窗口,我們這里不使用Transparency,因為對性能影響比較大。我們使用WindowChrome實現(xiàn)無邊框透明窗口。

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

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>

效果預覽

3、實現(xiàn)拖動邏輯

(1)注冊事件

與Grid的拖動實現(xiàn)非常類似,4個方位的控件可以當成margin使用,上一步的截屏框注冊3個鼠標事件

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

(2)拖動邏輯

將4個方位的控件的寬高組成一個margin,代入《C# wpf 實現(xiàn)Grid內(nèi)控件拖動》即可,只需要注意邊界處理(寬高不能為負數(shù)),此處不貼具體代碼。下面是Button_MouseDown的部分代碼示例,以此類推即可。

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

4、實現(xiàn)拖動調(diào)大小邏輯

(1)注冊事件

給界面中的8個Thumb注冊同一個Thumb_DragDelta事件。

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

(2)實現(xiàn)邏輯

將4個方位的控件的寬高組成一個margin,代入《C# wpf Grid中實現(xiàn)控件拖動調(diào)整大小》的Thumb_DragDelta即可,只需要注意邊界處理(寬高不能為負數(shù))。此處不貼具體代碼。下面是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;
}

二、效果預覽

三、總結

本文簡單介紹截屏框的實現(xiàn),曾經(jīng)為了事件這個界面功能花了不少精力,尤其是計算控件寬度以及位置關系,需要仔細計算。本文實現(xiàn)的截屏界面效果是可以的,拖動也是流暢的,完全滿足一般項目的使用。

到此這篇關于C# wpf使用DockPanel實現(xiàn)制作截屏框的文章就介紹到這了,更多相關C# wpf DockPanel截屏框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

桂平市| 金溪县| 静海县| 黄大仙区| 长泰县| 休宁县| 丁青县| 砚山县| 衡山县| 玉环县| 阿克陶县| 栾川县| 闽侯县| 麦盖提县| 大悟县| 云梦县| 精河县| 河东区| 安顺市| 穆棱市| 罗江县| 资源县| 西贡区| 合作市| 宣恩县| 黄龙县| 从化市| 尼勒克县| 安仁县| 平凉市| 河曲县| 汝州市| 江油市| 英吉沙县| 沂源县| 鸡西市| 开江县| 南华县| 彩票| 柘荣县| 鹤壁市|