Loading

WPF 截图控件之移除控件(九)「仿微信」

WPF 截图控件之移除控件(九)「仿微信」

WPF 截图控件之移除控件(九)「仿微信」

作者:WPFDevelopersOrg

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

正文

一、前几篇实现了添加 方框椭圆箭头 此时需要删除所添加的操作和撤销上次操作。
1)选中操作

  • 在添加方框椭圆箭头画笔文字的同时需要设置Tag=Draw用于标记是操作的痕迹 实现MouseLeftButtonDown事件。
  • 监听到操作MouseLeftButtonDown事件后循环VisualTreeHelper.GetChildren获取返回指定可视对象包含的子级个数。判断Tag==Draw设置其Opacity=1清除上次选择。
  • 用一个变量存放当前选择的操作使用FrameworkElement,然后设置当前内容Opacity=0.7

//箭头
controlArrow.MouseLeftButtonDown += (s, e) =>
                {
                    SelectElement();
                    frameworkElement = s as Control;
                    frameworkElement.Opacity = .7;
                };
 //方框
 borderRectangle.MouseLeftButtonDown += (s, e) =>
                        {
                            SelectElement();
                            frameworkElement = s as Border;
                            frameworkElement.Opacity = .7;
                        };
//椭圆
drawEllipse.MouseLeftButtonDown += (s, e) =>
                        {
                            SelectElement();
                            frameworkElement = s as Ellipse;
                            frameworkElement.Opacity = .7;
                        };
//文字
                         textBorder.PreviewMouseLeftButtonDown += (s, e) =>
                    {
                        _radioButtonText.IsChecked = true;
                        _radioButtonText_Click(null,null);
                        SelectElement();
                        var border = s as Border;
                        frameworkElement = border;
                        frameworkElement.Opacity = .7;
                        border.BorderThickness = new Thickness(1);
                    };
//画笔
                     polyLine.MouseLeftButtonDown += (s, e) =>
                    {
                        _radioButtonInk.IsChecked = true;
                        _radioButtonInk_Click(null, null);
                        SelectElement();
                        frameworkElement = s as Polyline;
                        frameworkElement.Opacity = .7;
                    };
 void SelectElement()
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(_canvas); i++)
            {
                var child = VisualTreeHelper.GetChild(_canvas, i);
                if (child is FrameworkElement frameworkElement && frameworkElement.Tag != null)
                    if (frameworkElement.Tag.ToString() == _tag)
                        frameworkElement.Opacity = 1;
            }
        }

2)删除操作 按下Delete键删除。


if (e.Key == Key.Delete)
            {
                if (_canvas.Children.Count > 0)
                    _canvas.Children.Remove(frameworkElement);
            }

3)撤销操作 按下Ctrl+Z 撤销上次操作。


if (e.KeyStates == Keyboard.GetKeyStates(Key.Z) && Keyboard.Modifiers == ModifierKeys.Control)
            {
                if (_canvas.Children.Count > 0)
                    _canvas.Children.Remove(_canvas.Children[_canvas.Children.Count - 1]);
            }

完整代码如下

项目地址

  • 框架名:WPFDevelopers
  • 作者:WPFDevelopers
  • GitHub
  • Gitee
    还未完成功能谁有兴趣欢迎在Github拉取分支后更改。
posted @ 2022-08-05 15:58  驚鏵  阅读(846)  评论(0编辑  收藏  举报