C#?wpf使用DockPanel實(shí)現(xiàn)制作截屏框
前言
做桌面客戶端的時(shí)候有時(shí)需要實(shí)現(xiàn)截屏功能,能夠在界面上框選截屏,做一個(gè)蒙版然后中間選框透明可以移動(dòng)和改變大小。這個(gè)功能是不太好實(shí)現(xiàn)的,需要一定的方法,其中使用DockPanel是相對簡單直接的實(shí)現(xiàn)。
一、如何實(shí)現(xiàn)
我們按照如下步驟即可實(shí)現(xiàn)一個(gè)截屏窗口
1、設(shè)置透明窗口
首先窗口必須是無邊框的透明窗口,我們這里不使用Transparency,因?yàn)閷π阅苡绊懕容^大。我們使用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
在窗口中定義一個(gè)DockPanel控件作為父級,定義4個(gè)方位填充控件以及中間截屏框。
<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)拖動(dòng)邏輯
(1)注冊事件
與Grid的拖動(dòng)實(shí)現(xiàn)非常類似,4個(gè)方位的控件可以當(dāng)成margin使用,上一步的截屏框注冊3個(gè)鼠標(biāo)事件
<Grid x:Name="clipRect" MouseDown="Button_MouseDown" MouseMove="Button_MouseMove" MouseUp="Button_MouseUp" Background="Transparent">
(2)拖動(dòng)邏輯
將4個(gè)方位的控件的寬高組成一個(gè)margin,代入《C# wpf 實(shí)現(xiàn)Grid內(nèi)控件拖動(dòng)》即可,只需要注意邊界處理(寬高不能為負(fù)數(shù)),此處不貼具體代碼。下面是Button_MouseDown的部分代碼示例,以此類推即可。
_mouseDownMargin = new Thickness(leftPanel.ActualWidth, topPanel.ActualHeight, rightPanel.ActualWidth, bottomPanel.ActualHeight);
4、實(shí)現(xiàn)拖動(dòng)調(diào)大小邏輯
(1)注冊事件
給界面中的8個(gè)Thumb注冊同一個(gè)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個(gè)方位的控件的寬高組成一個(gè)margin,代入《C# wpf Grid中實(shí)現(xiàn)控件拖動(dòng)調(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)為了事件這個(gè)界面功能花了不少精力,尤其是計(jì)算控件寬度以及位置關(guān)系,需要仔細(xì)計(jì)算。本文實(shí)現(xiàn)的截屏界面效果是可以的,拖動(dòng)也是流暢的,完全滿足一般項(xiàng)目的使用。
到此這篇關(guān)于C# wpf使用DockPanel實(shí)現(xiàn)制作截屏框的文章就介紹到這了,更多相關(guān)C# wpf DockPanel截屏框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 使用C#從零開始實(shí)現(xiàn)一個(gè)截屏工具
- 使用C#自制一個(gè)截屏工具
- C#?WPF使用GDI實(shí)現(xiàn)截屏功能
- C# WPF利用Clip屬性實(shí)現(xiàn)截屏框功能
- C#?wpf實(shí)現(xiàn)截屏框熱鍵截屏的示例代碼
- C#?wpf使用GDI+實(shí)現(xiàn)截屏效果
- 通過C#編寫一個(gè)簡易的Windows截屏增強(qiáng)工具
- C#實(shí)現(xiàn)小截屏軟件功能
- 基于C#實(shí)現(xiàn)的屏幕指定區(qū)域截屏代碼
- c#不使用系統(tǒng)api實(shí)現(xiàn)可以指定區(qū)域屏幕截屏功能
- c#根據(jù)網(wǎng)址抓取網(wǎng)頁截屏生成圖片的示例
- C# 全屏截圖的功能實(shí)現(xiàn)
相關(guān)文章
C#利用Spire.XLS for .NET高效隱藏和顯示Excel工作表
在日常工作中,我們經(jīng)常需要處理各種Excel文件,下面我們就就來探討如何利用 C# 編程,結(jié)合強(qiáng)大的 Spire.XLS for .NET 庫,輕松實(shí)現(xiàn)Excel工作表的批量或條件性隱藏與顯示吧2025-12-12
使用C#在Word文檔中自動(dòng)化創(chuàng)建與定制圖表
在辦公自動(dòng)化需求不斷增長的今天,越來越多的企業(yè)希望將數(shù)據(jù)可視化工作融入自動(dòng)化文檔生成流程中,過去,我們通常依賴 Excel 或 PowerPoint 來制作圖表,再手工插入到 Word 文檔中,本文將演示如何在 Word 文檔中創(chuàng)建圖表,需要的朋友可以參考下2025-12-12
C#實(shí)現(xiàn)設(shè)置Word段落對齊樣式的方法詳解
段落對齊是Word文檔格式排版的基礎(chǔ)需求,合理的對齊樣式能提升文檔的可讀性和美觀度,本文將講解如何通過Free Spire.Doc for .NET 實(shí)現(xiàn) Word 段落對齊樣式的設(shè)置,有需要的小伙伴可以了解下2025-12-12
帶著問題讀CLR via C#(筆記一)CLR的執(zhí)行模型
CLR (Common Language Runtime) 是一個(gè)可以由多種編程語言使用的“運(yùn)行時(shí)”。2013-04-04
C#遍歷得到checkboxlist選中值和設(shè)置選中項(xiàng)的代碼
這篇文章主要介紹了C#遍歷得到checkboxlist選中值和設(shè)置選中項(xiàng)的代碼,代碼簡單易懂,具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08

