C#?wpf使用DockPanel實現(xiàn)制作截屏框
前言
做桌面客戶端的時候有時需要實現(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內控件拖動》即可,只需要注意邊界處理(寬高不能為負數(shù)),此處不貼具體代碼。下面是Button_MouseDown的部分代碼示例,以此類推即可。
_mouseDownMargin = new Thickness(leftPanel.ActualWidth, topPanel.ActualHeight, rightPanel.ActualWidth, bottomPanel.ActualHeight);
4、實現(xiàn)拖動調大小邏輯
(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)控件拖動調整大小》的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),曾經為了事件這個界面功能花了不少精力,尤其是計算控件寬度以及位置關系,需要仔細計算。本文實現(xiàn)的截屏界面效果是可以的,拖動也是流暢的,完全滿足一般項目的使用。
到此這篇關于C# wpf使用DockPanel實現(xiàn)制作截屏框的文章就介紹到這了,更多相關C# wpf DockPanel截屏框內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C#使用throw和throw?ex拋出異常的區(qū)別介紹
這篇文章介紹了C#使用throw和throw?ex拋出異常的區(qū)別,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10C#使用MiniExcel實現(xiàn)導入導出數(shù)據(jù)到Excel/CSV文件
MiniExcel是一個簡單、高效避免OOM的.NET處理Excel查、寫、填充數(shù)據(jù)的工具,這篇文章主要介紹了C#如何使用MiniExcel實現(xiàn)導入導出數(shù)據(jù)到Excel/CSV文件,需要的可以參考下2024-02-02C#實現(xiàn)JSON解析器MojoUnityJson功能(簡單且高效)
MojoUnityJson 是使用C#實現(xiàn)的JSON解析器 ,算法思路來自于游戲引擎Mojoc的C語言實現(xiàn) Json.h。這篇文章主要介紹了C#實現(xiàn)JSON解析器MojoUnityJson的方法,需要的朋友可以參考下2018-01-01