C#?wpf使用DockPanel實(shí)現(xiàn)制作截屏框
前言
做桌面客戶端的時候有時需要實(shí)現(xiàn)截屏功能,能夠在界面上框選截屏,做一個蒙版然后中間選框透明可以移動和改變大小。這個功能是不太好實(shí)現(xiàn)的,需要一定的方法,其中使用DockPanel是相對簡單直接的實(shí)現(xiàn)。
一、如何實(shí)現(xiàn)
我們按照如下步驟即可實(shí)現(xiàn)一個截屏窗口
1、設(shè)置透明窗口
首先窗口必須是無邊框的透明窗口,我們這里不使用Transparency,因為對性能影響比較大。我們使用WindowChrome實(shí)現(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>效果預(yù)覽

3、實(shí)現(xiàn)拖動邏輯
(1)注冊事件
與Grid的拖動實(shí)現(xiàn)非常類似,4個方位的控件可以當(dāng)成margin使用,上一步的截屏框注冊3個鼠標(biāo)事件
<Grid x:Name="clipRect" MouseDown="Button_MouseDown" MouseMove="Button_MouseMove" MouseUp="Button_MouseUp" Background="Transparent">
(2)拖動邏輯
將4個方位的控件的寬高組成一個margin,代入《C# wpf 實(shí)現(xiàn)Grid內(nèi)控件拖動》即可,只需要注意邊界處理(寬高不能為負(fù)數(shù)),此處不貼具體代碼。下面是Button_MouseDown的部分代碼示例,以此類推即可。
_mouseDownMargin = new Thickness(leftPanel.ActualWidth, topPanel.ActualHeight, rightPanel.ActualWidth, bottomPanel.ActualHeight);
4、實(shí)現(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)實(shí)現(xiàn)邏輯
將4個方位的控件的寬高組成一個margin,代入《C# wpf Grid中實(shí)現(xiàn)控件拖動調(diào)整大小》的Thumb_DragDelta即可,只需要注意邊界處理(寬高不能為負(fù)數(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;
}二、效果預(yù)覽

三、總結(jié)
本文簡單介紹截屏框的實(shí)現(xiàn),曾經(jīng)為了事件這個界面功能花了不少精力,尤其是計算控件寬度以及位置關(guān)系,需要仔細(xì)計算。本文實(shí)現(xiàn)的截屏界面效果是可以的,拖動也是流暢的,完全滿足一般項目的使用。
到此這篇關(guān)于C# wpf使用DockPanel實(shí)現(xiàn)制作截屏框的文章就介紹到這了,更多相關(guān)C# wpf DockPanel截屏框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用throw和throw?ex拋出異常的區(qū)別介紹
這篇文章介紹了C#使用throw和throw?ex拋出異常的區(qū)別,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10
讓C# Excel導(dǎo)入導(dǎo)出 支持不同版本Office
讓C# Excel導(dǎo)入導(dǎo)出,支持不同版本的Office,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08
C#中使用Override和New關(guān)鍵字進(jìn)行版本控制
在?C#?中,override?和?new?關(guān)鍵字用于控制類之間的成員方法的隱藏和重寫,理解它們之間的差異和使用場景對于設(shè)計靈活且易于維護(hù)的代碼至關(guān)重要,在這篇博客中,我們將詳細(xì)探討這兩個關(guān)鍵字的用法,并通過示例來說明它們的實(shí)際應(yīng)用,需要的朋友可以參考下2024-10-10
C#使用MiniExcel實(shí)現(xiàn)導(dǎo)入導(dǎo)出數(shù)據(jù)到Excel/CSV文件
MiniExcel是一個簡單、高效避免OOM的.NET處理Excel查、寫、填充數(shù)據(jù)的工具,這篇文章主要介紹了C#如何使用MiniExcel實(shí)現(xiàn)導(dǎo)入導(dǎo)出數(shù)據(jù)到Excel/CSV文件,需要的可以參考下2024-02-02
C#實(shí)現(xiàn)JSON解析器MojoUnityJson功能(簡單且高效)
MojoUnityJson 是使用C#實(shí)現(xiàn)的JSON解析器 ,算法思路來自于游戲引擎Mojoc的C語言實(shí)現(xiàn) Json.h。這篇文章主要介紹了C#實(shí)現(xiàn)JSON解析器MojoUnityJson的方法,需要的朋友可以參考下2018-01-01

