WPF 截图绘制方框与椭圆(四)

WPF 截图绘制方框与椭圆(四)

WPF 截图绘制方框与椭圆(四)

作者:WPFDevelopersOrg

原文链接: https://github.com/WPFDevelopersOrg/WPFDevelopers

正文

  • 有开发者在B站反馈第三篇有Issues已修复。

实现在截图区域内绘制 方框椭圆 有两种方式
1)可以在截图的区域内部添加一个Canvas宽高填充至区域内,在进行绘制方框或椭圆。
2)直接在外层的Canvas中添加,这样需要判断鼠标按下的位置和移动的位置必须在已截图区域内,如超出范围也不绘制到区域外。

本章使用了第二种方式
此篇更新截图时隐藏当前窗口

一、首先接着ScreenCut继续发电。

1.1

新增定义 画方框、椭圆、颜色选择框Popup、Popup内部Border、Border内部RadioButton的父容器

     [TemplatePart(Name = RadioButtonRectangleTemplateName, Type = typeof(RadioButton))]
  [TemplatePart(Name = RadioButtonEllipseTemplateName, Type = typeof(RadioButton))]
  [TemplatePart(Name = PopupTemplateName, Type = typeof(Popup))]
  [TemplatePart(Name = PopupBorderTemplateName, Type = typeof(Border))]
  [TemplatePart(Name = WrapPanelColorTemplateName, Type = typeof(WrapPanel))]
  
     private const string RadioButtonRectangleTemplateName = "PART_RadioButtonRectangle";
      private const string RadioButtonEllipseTemplateName = "PART_RadioButtonEllipse";
      private const string PopupTemplateName = "PART_Popup";
      private const string PopupBorderTemplateName = "PART_PopupBorder";
      private const string WrapPanelColorTemplateName = "PART_WrapPanelColor";
      private Popup _popup;
      private WrapPanel _wrapPanel;
      
      /// <summary>
      /// 当前绘制矩形
      /// </summary>
      private Border borderRectangle;
      /// <summary>
      /// 绘制当前椭圆
      /// </summary>
      private Ellipse drawEllipse;
      /// <summary>
      /// 当前选择颜色
      /// </summary>
      private Brush _currentBrush;

1.2

新增RadioButtonStyles为了选择方框、椭圆、颜色

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
   <ResourceDictionary.MergedDictionaries>
       <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
   </ResourceDictionary.MergedDictionaries>

   <Style x:Key="PathRadioButton" TargetType="{x:Type RadioButton}" BasedOn="{StaticResource ControlBasicStyle}">
       <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
       <Setter Property="FrameworkElement.OverridesDefaultStyle" Value="True" />
       <Setter Property="HorizontalContentAlignment" Value="Center" />
       <Setter Property="VerticalContentAlignment" Value="Center" />
       <Setter Property="BorderThickness" Value="1" />
       <Setter Property="Padding" Value="8" />
       <Setter Property="Cursor" Value="Hand"/>
       <Setter Property="Template">
           <Setter.Value>
               <ControlTemplate TargetType="{x:Type RadioButton}">
                   <Border Background="Transparent">
                       <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                         Margin="{TemplateBinding Padding}" 
                                         VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                         x:Name="PART_ContentPresenter" Opacity=".8"/>
                   </Border>
                   <ControlTemplate.Triggers>
                       <Trigger Property="IsChecked" Value="True">
                           <Setter Property="Opacity" TargetName="PART_ContentPresenter" Value="1"/>
                       </Trigger>
                       <Trigger Property="IsMouseOver" Value=
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值