ToB企服应用市场:ToB评测及商务社交产业平台

标题: WPF 截图控件之绘制方框与椭圆(四) 「仿微信」 [打印本页]

作者: 惊落一身雪    时间: 2022-9-3 14:35
标题: WPF 截图控件之绘制方框与椭圆(四) 「仿微信」
前言

接着上周写的截图控件继续更新 绘制方框与椭圆
1.WPF实现截屏「仿微信」
2.WPF 实现截屏控件之移动(二)「仿微信」
3.WPF 截图控件之伸缩(三) 「仿微信」
正文

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

实现在截图区域内绘制 方框椭圆  有两种方式
1)可以在截图的区域内部添加一个Canvas宽高填充至区域内,在进行绘制方框或椭圆。
2)直接在外层的Canvas中添加,这样需要判断鼠标按下的位置和移动的位置必须在已截图区域内,如超出范围也不绘制到区域外。
本章使用了第二种方式
此篇更新截图时隐藏当前窗口
一、首先接着ScreenCut继续发电。
1.1

新增定义 画方框、椭圆、颜色选择框Popup、Popup内部Border、Border内部RadioButton的父容器
  1.      [TemplatePart(Name = RadioButtonRectangleTemplateName, Type = typeof(RadioButton))]
  2.   [TemplatePart(Name = RadioButtonEllipseTemplateName, Type = typeof(RadioButton))]
  3.   [TemplatePart(Name = PopupTemplateName, Type = typeof(Popup))]
  4.   [TemplatePart(Name = PopupBorderTemplateName, Type = typeof(Border))]
  5.   [TemplatePart(Name = WrapPanelColorTemplateName, Type = typeof(WrapPanel))]
  6.   
  7.      private const string RadioButtonRectangleTemplateName = "PART_RadioButtonRectangle";
  8.       private const string RadioButtonEllipseTemplateName = "PART_RadioButtonEllipse";
  9.       private const string PopupTemplateName = "PART_Popup";
  10.       private const string PopupBorderTemplateName = "PART_PopupBorder";
  11.       private const string WrapPanelColorTemplateName = "PART_WrapPanelColor";
  12.       private Popup _popup;
  13.       private WrapPanel _wrapPanel;
  14.       
  15.       /// <summary>
  16.       /// 当前绘制矩形
  17.       /// </summary>
  18.       private Border borderRectangle;
  19.       /// <summary>
  20.       /// 绘制当前椭圆
  21.       /// </summary>
  22.       private Ellipse drawEllipse;
  23.       /// <summary>
  24.       /// 当前选择颜色
  25.       /// </summary>
  26.       private Brush _currentBrush;
复制代码
1.2

新增RadioButtonStyles为了选择方框、椭圆、颜色
  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  3.    
  4.    <ResourceDictionary.MergedDictionaries>
  5.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  6.    </ResourceDictionary.MergedDictionaries>
  7.    
  8.    
  9. </ResourceDictionary>
复制代码
1.3

ScreenCut.xaml增加代码如下
  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  3.    
  4.    <ResourceDictionary.MergedDictionaries>
  5.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  6.    </ResourceDictionary.MergedDictionaries>
  7.    
  8.    
  9. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  10.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  11.    
  12.    <ResourceDictionary.MergedDictionaries>
  13.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  14.    </ResourceDictionary.MergedDictionaries>
  15.    
  16.    
  17. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  18.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  19.    
  20.    <ResourceDictionary.MergedDictionaries>
  21.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  22.    </ResourceDictionary.MergedDictionaries>
  23.    
  24.    
  25. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  26.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  27.    
  28.    <ResourceDictionary.MergedDictionaries>
  29.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  30.    </ResourceDictionary.MergedDictionaries>
  31.    
  32.    
  33. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  34.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  35.    
  36.    <ResourceDictionary.MergedDictionaries>
  37.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  38.    </ResourceDictionary.MergedDictionaries>
  39.    
  40.    
  41. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  42.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  43.    
  44.    <ResourceDictionary.MergedDictionaries>
  45.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  46.    </ResourceDictionary.MergedDictionaries>
  47.    
  48.    
  49. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  50.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  51.    
  52.    <ResourceDictionary.MergedDictionaries>
  53.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  54.    </ResourceDictionary.MergedDictionaries>
  55.    
  56.    
  57. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  58.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  59.    
  60.    <ResourceDictionary.MergedDictionaries>
  61.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  62.    </ResourceDictionary.MergedDictionaries>
  63.    
  64.    
  65. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  66.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  67.    
  68.    <ResourceDictionary.MergedDictionaries>
  69.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  70.    </ResourceDictionary.MergedDictionaries>
  71.    
  72.    
  73. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  74.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  75.    
  76.    <ResourceDictionary.MergedDictionaries>
  77.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  78.    </ResourceDictionary.MergedDictionaries>
  79.    
  80.    
  81. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  82.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  83.    
  84.    <ResourceDictionary.MergedDictionaries>
  85.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  86.    </ResourceDictionary.MergedDictionaries>
  87.    
  88.    
  89. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  90.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  91.    
  92.    <ResourceDictionary.MergedDictionaries>
  93.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  94.    </ResourceDictionary.MergedDictionaries>
  95.    
  96.    
  97. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  98.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  99.    
  100.    <ResourceDictionary.MergedDictionaries>
  101.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  102.    </ResourceDictionary.MergedDictionaries>
  103.    
  104.    
  105. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  106.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  107.    
  108.    <ResourceDictionary.MergedDictionaries>
  109.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  110.    </ResourceDictionary.MergedDictionaries>
  111.    
  112.    
  113. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  114.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  115.    
  116.    <ResourceDictionary.MergedDictionaries>
  117.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  118.    </ResourceDictionary.MergedDictionaries>
  119.    
  120.    
  121. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  122.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  123.    
  124.    <ResourceDictionary.MergedDictionaries>
  125.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  126.    </ResourceDictionary.MergedDictionaries>
  127.    
  128.    
  129. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  130.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  131.    
  132.    <ResourceDictionary.MergedDictionaries>
  133.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  134.    </ResourceDictionary.MergedDictionaries>
  135.    
  136.    
  137. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  138.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  139.    
  140.    <ResourceDictionary.MergedDictionaries>
  141.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  142.    </ResourceDictionary.MergedDictionaries>
  143.    
  144.    
  145. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  146.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  147.    
  148.    <ResourceDictionary.MergedDictionaries>
  149.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  150.    </ResourceDictionary.MergedDictionaries>
  151.    
  152.    
  153. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  154.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  155.    
  156.    <ResourceDictionary.MergedDictionaries>
  157.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  158.    </ResourceDictionary.MergedDictionaries>
  159.    
  160.    
  161. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  162.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  163.    
  164.    <ResourceDictionary.MergedDictionaries>
  165.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  166.    </ResourceDictionary.MergedDictionaries>
  167.    
  168.    
  169. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  170.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  171.    
  172.    <ResourceDictionary.MergedDictionaries>
  173.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  174.    </ResourceDictionary.MergedDictionaries>
  175.    
  176.    
  177. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  178.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  179.    
  180.    <ResourceDictionary.MergedDictionaries>
  181.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  182.    </ResourceDictionary.MergedDictionaries>
  183.    
  184.    
  185. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  186.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  187.    
  188.    <ResourceDictionary.MergedDictionaries>
  189.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  190.    </ResourceDictionary.MergedDictionaries>
  191.    
  192.    
  193. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  194.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  195.    
  196.    <ResourceDictionary.MergedDictionaries>
  197.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  198.    </ResourceDictionary.MergedDictionaries>
  199.    
  200.    
  201. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  202.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  203.    
  204.    <ResourceDictionary.MergedDictionaries>
  205.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  206.    </ResourceDictionary.MergedDictionaries>
  207.    
  208.    
  209. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  210.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  211.    
  212.    <ResourceDictionary.MergedDictionaries>
  213.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  214.    </ResourceDictionary.MergedDictionaries>
  215.    
  216.    
  217. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  218.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  219.    
  220.    <ResourceDictionary.MergedDictionaries>
  221.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  222.    </ResourceDictionary.MergedDictionaries>
  223.    
  224.    
  225. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  226.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  227.    
  228.    <ResourceDictionary.MergedDictionaries>
  229.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  230.    </ResourceDictionary.MergedDictionaries>
  231.    
  232.    
  233. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  234.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  235.    
  236.    <ResourceDictionary.MergedDictionaries>
  237.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  238.    </ResourceDictionary.MergedDictionaries>
  239.    
  240.    
  241. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  242.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  243.    
  244.    <ResourceDictionary.MergedDictionaries>
  245.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  246.    </ResourceDictionary.MergedDictionaries>
  247.    
  248.    
  249. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  250.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  251.    
  252.    <ResourceDictionary.MergedDictionaries>
  253.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  254.    </ResourceDictionary.MergedDictionaries>
  255.    
  256.    
  257. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  258.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  259.    
  260.    <ResourceDictionary.MergedDictionaries>
  261.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  262.    </ResourceDictionary.MergedDictionaries>
  263.    
  264.    
  265. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  266.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  267.    
  268.    <ResourceDictionary.MergedDictionaries>
  269.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  270.    </ResourceDictionary.MergedDictionaries>
  271.    
  272.    
  273. </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  274.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  275.    
  276.    <ResourceDictionary.MergedDictionaries>
  277.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  278.    </ResourceDictionary.MergedDictionaries>
  279.    
  280.    
  281. </ResourceDictionary>   
复制代码
二、ScreenCut.cs 增加的后台逻辑如下


2.1 RadioButton选中方框和椭圆的切换Popup并设置ScreenCutMouseType枚举和鼠标:
  1.     _radioButtonRectangle = GetTemplateChild(RadioButtonRectangleTemplateName) as RadioButton;
  2.          if (_radioButtonRectangle != null)
  3.              _radioButtonRectangle.Click += _radioButtonRectangle_Click;
  4.          _radioButtonEllipse = GetTemplateChild(RadioButtonEllipseTemplateName) as RadioButton;
  5.          if (_radioButtonEllipse != null)
  6.              _radioButtonEllipse.Click += _radioButtonEllipse_Click;
  7.              private void _radioButtonRectangle_Click(object sender, RoutedEventArgs e)
  8.      {
  9.          RadioButtonChecked(_radioButtonRectangle, ScreenCutMouseType.DrawRectangle);
  10.      }
  11.      private void _radioButtonEllipse_Click(object sender, RoutedEventArgs e)
  12.      {
  13.          RadioButtonChecked(_radioButtonEllipse, ScreenCutMouseType.DrawEllipse);
  14.      }
  15.      void RadioButtonChecked(RadioButton radioButton, ScreenCutMouseType screenCutMouseTypeRadio)
  16.      {
  17.          if (radioButton.IsChecked == true)
  18.          {
  19.              screenCutMouseType = screenCutMouseTypeRadio;
  20.              _border.Cursor = Cursors.Arrow;
  21.              if (_popup.PlacementTarget != null && _popup.IsOpen)
  22.                  _popup.IsOpen = false;
  23.              _popup.PlacementTarget = radioButton;
  24.              _popup.IsOpen = true;
  25.          }
  26.          else
  27.          {
  28.              if (screenCutMouseType == screenCutMouseTypeRadio)
  29.                  Restore();
  30.          }
  31.      }
  32.      void Restore()
  33.      {
  34.          _border.Cursor = Cursors.SizeAll;
  35.          if (screenCutMouseType == ScreenCutMouseType.Default) return;
  36.          screenCutMouseType = ScreenCutMouseType.Default;
  37.      }
复制代码
2.2 ScreenCut绘制方框和椭圆代码如下:
  1. void DrawMultipleControl(Point current)        {            if (current == pointStart) return;<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  3.    
  4.    <ResourceDictionary.MergedDictionaries>
  5.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  6.    </ResourceDictionary.MergedDictionaries>
  7.    
  8.    
  9. </ResourceDictionary> if (current.X > rect.BottomRight.X                ||                current.Y > rect.BottomRight.Y)                return;            var drawRect = new Rect(pointStart, current);            switch (screenCutMouseType)            {                case ScreenCutMouseType.DrawRectangle:                    if (borderRectangle == null)                    {<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  10.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  11.    
  12.    <ResourceDictionary.MergedDictionaries>
  13.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  14.    </ResourceDictionary.MergedDictionaries>
  15.    
  16.    
  17. </ResourceDictionary>  borderRectangle = new Border()<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  18.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  19.    
  20.    <ResourceDictionary.MergedDictionaries>
  21.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  22.    </ResourceDictionary.MergedDictionaries>
  23.    
  24.    
  25. </ResourceDictionary>  {<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  26.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  27.    
  28.    <ResourceDictionary.MergedDictionaries>
  29.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  30.    </ResourceDictionary.MergedDictionaries>
  31.    
  32.    
  33. </ResourceDictionary>      BorderBrush = _currentBrush == null ? Brushes.Red : _currentBrush,<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  34.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  35.    
  36.    <ResourceDictionary.MergedDictionaries>
  37.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  38.    </ResourceDictionary.MergedDictionaries>
  39.    
  40.    
  41. </ResourceDictionary>      BorderThickness = new Thickness(3),<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  42.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  43.    
  44.    <ResourceDictionary.MergedDictionaries>
  45.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  46.    </ResourceDictionary.MergedDictionaries>
  47.    
  48.    
  49. </ResourceDictionary>      CornerRadius = new CornerRadius(3),<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  50.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  51.    
  52.    <ResourceDictionary.MergedDictionaries>
  53.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  54.    </ResourceDictionary.MergedDictionaries>
  55.    
  56.    
  57. </ResourceDictionary>  };<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  58.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  59.    
  60.    <ResourceDictionary.MergedDictionaries>
  61.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  62.    </ResourceDictionary.MergedDictionaries>
  63.    
  64.    
  65. </ResourceDictionary>  _canvas.Children.Add(borderRectangle);                    }                    break;                case ScreenCutMouseType.DrawEllipse:                    if (drawEllipse == null)                    {<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  66.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  67.    
  68.    <ResourceDictionary.MergedDictionaries>
  69.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  70.    </ResourceDictionary.MergedDictionaries>
  71.    
  72.    
  73. </ResourceDictionary>  drawEllipse = new Ellipse()<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  74.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  75.    
  76.    <ResourceDictionary.MergedDictionaries>
  77.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  78.    </ResourceDictionary.MergedDictionaries>
  79.    
  80.    
  81. </ResourceDictionary>  {<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  82.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  83.    
  84.    <ResourceDictionary.MergedDictionaries>
  85.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  86.    </ResourceDictionary.MergedDictionaries>
  87.    
  88.    
  89. </ResourceDictionary>      Stroke = _currentBrush == null ? Brushes.Red : _currentBrush,<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  90.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  91.    
  92.    <ResourceDictionary.MergedDictionaries>
  93.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  94.    </ResourceDictionary.MergedDictionaries>
  95.    
  96.    
  97. </ResourceDictionary>      StrokeThickness = 3,<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  98.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  99.    
  100.    <ResourceDictionary.MergedDictionaries>
  101.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  102.    </ResourceDictionary.MergedDictionaries>
  103.    
  104.    
  105. </ResourceDictionary>  };<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  106.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  107.    
  108.    <ResourceDictionary.MergedDictionaries>
  109.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  110.    </ResourceDictionary.MergedDictionaries>
  111.    
  112.    
  113. </ResourceDictionary>  _canvas.Children.Add(drawEllipse);                    }                    break;<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  114.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  115.    
  116.    <ResourceDictionary.MergedDictionaries>
  117.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  118.    </ResourceDictionary.MergedDictionaries>
  119.    
  120.    
  121. </ResourceDictionary>     }<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  122.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  123.    
  124.    <ResourceDictionary.MergedDictionaries>
  125.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  126.    </ResourceDictionary.MergedDictionaries>
  127.    
  128.    
  129. </ResourceDictionary> var _borderLeft = drawRect.Left - Canvas.GetLeft(_border);<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  130.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  131.    
  132.    <ResourceDictionary.MergedDictionaries>
  133.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  134.    </ResourceDictionary.MergedDictionaries>
  135.    
  136.    
  137. </ResourceDictionary> if (_borderLeft < 0)                _borderLeft = Math.Abs(_borderLeft);            if (drawRect.Width + _borderLeft < _border.ActualWidth)            {                var wLeft = Canvas.GetLeft(_border) + _border.ActualWidth;                var left = drawRect.Left < Canvas.GetLeft(_border) ? Canvas.GetLeft(_border) : drawRect.Left > wLeft ? wLeft : drawRect.Left;                if (borderRectangle != null)                {                    borderRectangle.Width = drawRect.Width;                    Canvas.SetLeft(borderRectangle, left);                }                if (drawEllipse != null)                {                    drawEllipse.Width = drawRect.Width;                    Canvas.SetLeft(drawEllipse, left);                }<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  138.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  139.    
  140.    <ResourceDictionary.MergedDictionaries>
  141.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  142.    </ResourceDictionary.MergedDictionaries>
  143.    
  144.    
  145. </ResourceDictionary>                  }<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  146.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  147.    
  148.    <ResourceDictionary.MergedDictionaries>
  149.        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  150.    </ResourceDictionary.MergedDictionaries>
  151.    
  152.    
  153. </ResourceDictionary> var _borderTop = drawRect.Top - Canvas.GetTop(_border);            if(_borderTop < 0)                _borderTop = Math.Abs(_borderTop);            if (drawRect.Height + _borderTop < _border.ActualHeight)            {                var hTop = Canvas.GetTop(_border) + _border.Height;                var top = drawRect.Top < Canvas.GetTop(_border) ? Canvas.GetTop(_border) : drawRect.Top > hTop ? hTop : drawRect.Top;                if (borderRectangle != null)                {                    borderRectangle.Height = drawRect.Height;                    Canvas.SetTop(borderRectangle, top);                }                if (drawEllipse != null)                {                    drawEllipse.Height = drawRect.Height;                    Canvas.SetTop(drawEllipse, top);                }            }        }
复制代码
2.3 Popup跟随问题这里解决办法是先关闭再打开代码如下:
  1. if (_popup != null && _popup.IsOpen)
  2.            {
  3.                _popup.IsOpen = false;
  4.                _popup.IsOpen = true;
  5.            }
复制代码
2.4  ScreenCut使用方式如下:
  1. public partial class ScreenCutExample : UserControl
  2.    {
  3.        public bool IsChecked
  4.        {
  5.            get { return (bool)GetValue(IsCheckedProperty); }
  6.            set { SetValue(IsCheckedProperty, value); }
  7.        }
  8.        public static readonly DependencyProperty IsCheckedProperty =
  9.            DependencyProperty.Register("IsChecked", typeof(bool), typeof(ScreenCutExample), new PropertyMetadata(false));
  10.        public ScreenCutExample()
  11.        {
  12.            InitializeComponent();
  13.        }
  14.        private void Button_Click(object sender, RoutedEventArgs e)
  15.        {
  16.            var screenCut = new ScreenCut();
  17.            if (IsChecked)
  18.            {
  19.                App.CurrentMainWindow.WindowState = WindowState.Minimized;
  20.                screenCut.Show();
  21.                screenCut.Activate();
  22.            }
  23.            else
  24.                screenCut.ShowDialog();
  25.        }
  26.    }
复制代码
完整代码如下


项目地址


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4