本实例主要是使用WPF实现拖动鼠标画出矩形框同时框选控件的功能,效果如下:

1、首先Canvas里放一些矩形控件来做示例,添加鼠标按下、抬起、移动事件

<Window x:Class="MouseBoxSelection.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:MouseBoxSelection"mc:Ignorable="d"Title="MainWindow" Height="250" Width="500"  ><Canvas x:Name="mainCanvas" Background="LightGray" MouseMove="MainCanvas_MouseMove" MouseLeftButtonUp="MainCanvas_MouseLeftButtonUp" MouseLeftButtonDown="MainCanvas_MouseLeftButtonDown"><Rectangle Width="25" Height="25" Canvas.Left="100" Canvas.Top="50" Fill="Red"></Rectangle><Rectangle Width="25" Height="25" Canvas.Left="150" Canvas.Top="50" Fill="Blue"></Rectangle><Rectangle Width="25" Height="25" Canvas.Left="200" Canvas.Top="50" Fill="Red"></Rectangle><Rectangle Width="25" Height="25" Canvas.Left="250" Canvas.Top="50" Fill="Blue"></Rectangle><Rectangle Width="25" Height="25" Canvas.Left="300" Canvas.Top="50" Fill="Red"></Rectangle><Rectangle Width="25" Height="25" Canvas.Left="350" Canvas.Top="50" Fill="Blue"></Rectangle><Rectangle Width="25" Height="25" Canvas.Left="100" Canvas.Top="100" Fill="Red"></Rectangle><Rectangle Width="25" Height="25" Canvas.Left="150" Canvas.Top="100" Fill="Blue"></Rectangle><Rectangle Width="25" Height="25" Canvas.Left="200" Canvas.Top="100" Fill="Red"></Rectangle><Rectangle Width="25" Height="25" Canvas.Left="250" Canvas.Top="100" Fill="Blue"></Rectangle><Rectangle Width="25" Height="25" Canvas.Left="300" Canvas.Top="100" Fill="Red"></Rectangle><Rectangle Width="25" Height="25" Canvas.Left="350" Canvas.Top="100" Fill="Blue"></Rectangle><Button x:Name="ReSet" Content="恢复" Width="80" Height="30" Canvas.Right="30" Canvas.Bottom="30" Cursor="Hand" Click="ReSet_Click"></Button></Canvas>
</Window>

2、添加全局变量

        private Border currentBoxSelectedBorder = null;//拖动展示的提示框private bool isCanMove = false;//鼠标是否移动private Point tempStartPoint;//起始坐标

3、实现鼠标按下记录起始点,拖动绘制选框,抬起选中逻辑

        /// <summary>/// 鼠标按下记录起始点/// </summary>private void MainCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e){isCanMove = true;tempStartPoint = e.GetPosition(this.mainCanvas);}/// <summary>/// 框选逻辑/// </summary>private void MainCanvas_MouseMove(object sender, MouseEventArgs e){if(isCanMove){Point tempEndPoint = e.GetPosition(this.mainCanvas);//绘制跟随鼠标移动的方框DrawMultiselectBorder(tempEndPoint, tempStartPoint);}}/// <summary>/// 鼠标抬起时清除选框/// </summary>private void MainCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e){if (currentBoxSelectedBorder != null){//获取选框的矩形位置Point tempEndPoint = e.GetPosition(this.mainCanvas);Rect tempRect = new Rect(tempStartPoint, tempEndPoint);//获取子控件List<Rectangle> childList = GetChildObjects<Rectangle>(this.mainCanvas);foreach (var child in childList){//获取子控件矩形位置Rect childRect = new Rect(Canvas.GetLeft(child), Canvas.GetTop(child), child.Width, child.Height);//若子控件与选框相交则改变样式if (childRect.IntersectsWith(tempRect))child.Opacity = 0.4;}this.mainCanvas.Children.Remove(currentBoxSelectedBorder);currentBoxSelectedBorder = null;}isCanMove = false;}/// <summary>/// 绘制跟随鼠标移动的方框/// </summary>private void DrawMultiselectBorder(Point endPoint, Point startPoint){if (currentBoxSelectedBorder == null){currentBoxSelectedBorder = new Border();currentBoxSelectedBorder.Background = new SolidColorBrush(Colors.Orange);currentBoxSelectedBorder.Opacity = 0.4;currentBoxSelectedBorder.BorderThickness = new Thickness(2);currentBoxSelectedBorder.BorderBrush = new SolidColorBrush(Colors.OrangeRed);Canvas.SetZIndex(currentBoxSelectedBorder, 100);this.mainCanvas.Children.Add(currentBoxSelectedBorder);}currentBoxSelectedBorder.Width = Math.Abs(endPoint.X - startPoint.X);currentBoxSelectedBorder.Height = Math.Abs(endPoint.Y - startPoint.Y);if (endPoint.X - startPoint.X >= 0)Canvas.SetLeft(currentBoxSelectedBorder, startPoint.X);elseCanvas.SetLeft(currentBoxSelectedBorder, endPoint.X);if (endPoint.Y - startPoint.Y >= 0)Canvas.SetTop(currentBoxSelectedBorder, startPoint.Y);elseCanvas.SetTop(currentBoxSelectedBorder, endPoint.Y);}/// <summary>/// 获得所有子控件/// </summary>public static List<T> GetChildObjects<T>(System.Windows.DependencyObject obj) where T : System.Windows.FrameworkElement{System.Windows.DependencyObject child = null;List<T> childList = new List<T>();for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++){child = VisualTreeHelper.GetChild(obj, i);if (child is T){childList.Add((T)child);}childList.AddRange(GetChildObjects<T>(child));}return childList;}/// <summary>/// 恢复原来状态/// </summary>private void ReSet_Click(object sender, RoutedEventArgs e){List<Rectangle> childList = GetChildObjects<Rectangle>(this.mainCanvas);foreach (var child in childList){if (child.Opacity != 1)child.Opacity = 1;}}

WPF实现鼠标拖动框选功能相关推荐

  1. 基于Fixed定位的框选功能

    最近项目涉及到一个支持批量操作的小需求,交互上需要使用框选来触发.在查阅了一些资料后发现,网上的方案基本都是基于绝对定位布局的,此方案如果是针对全局(在body上)的框选,还是可用的.但是现实需求里几 ...

  2. C#制作QQ截图的自动框选功能的个人思路(三)自动框选

    效果图: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; ...

  3. C#制作QQ截图的自动框选功能的个人思路(二)设置Hook

    上一篇介绍了一下我的一个个人思路而已..这一篇来分析分析代码... 主要分为两大部分 第一部分 就是 那个自动框选的那部分了啊 第二部分 就是设置Hook(不然窗体一直禁用啊) 先来说说Hook 也就 ...

  4. 基于leaflet完成框选功能(不随地图缩放)并截图打印

    给定一个矩形框用于规定地图打印范围,并截图打印该范围,用户可以在此范围内进行标绘,需要满足以下要求: 1)初始状态下,矩形框不随着地图的放大.缩小.移动而变化位置:(解锁状态) 2)点击锁定按钮后,矩 ...

  5. vue2 实现鼠标左键拖拽实现框选功能

    一.实现如图所示功能 二.鼠标mousedown事件和click事件重复,不设置click事件可以达到相同效果 // 代码如下 <template><el-dialogtitle=& ...

  6. 鼠标移动框选动态绘制图形,基于zrender

    本文只介绍根据鼠标事件动态绘制矩形方法 zrender实现简易画板功能可绘制大部分常用图形在文章末尾可参考 像下图这样,可随心所欲绘制你想绘制的矩形: 完整代码:(有详细注释,有任何疑问欢迎留言) & ...

  7. UE4 RTS 框选功能实现

    代码如下,可直接复制粘贴: Begin Object Class=/Script/BlueprintGraph.K2Node_InputKey Name="K2Node_InputKey_0 ...

  8. d3.js实现力导向图圈选框选

    #d3.js实现力导向图圈选框选 今天给大家带来的是如何在2D可视化图形中加入通过鼠标拖动圈选功能,以力导向图为例. ##最终效果 demo跳转 ##代码解析 我们是要在节点的上方绘制一个矩形覆盖节点 ...

  9. vue3实现鼠标左键拖拽画矩形框框选功能

    vue3 + elementuiPlus 实现鼠标左键拖拽画矩形框 框选列表功能,仿照桌面框选功能 效果如图: vue3鼠标框选 代码: <template><div class=& ...

  10. 实现PCL使用Ctrl + 鼠标左键进行框选

    PCL自带的框选功能需要通过 点击 'x'进入退出,使用起来十分不便.本文实现了使用Ctrl + 鼠标左键进行框选 1.注册PCLVisualizer的键盘事件和框选结束事件,并设置PCL的Picke ...

最新文章

  1. Oracle PL/SQL编程学习笔记:Merge方法的使用
  2. JVM堆 栈 方法区详解
  3. 多角度人脸识别简单介绍
  4. Nmap扫描教程之DNS服务类
  5. bzoj3993 [SDOI2015]星际战争
  6. 软件工程结对开发团队成员以及题目介绍
  7. 计算机精英协会考核题 —— 第三题:斐波那契数
  8. 网络转型临界点 带你看瞻博网络的创新步伐
  9. wordpress搭建博客 主题推荐 2019
  10. java.net.SocketException: Software caused connection abort: socket write erro
  11. 案例:Xshell 成功创建定时任务(解决no crontab for root using an empty one问题)- 最新版
  12. MySQL tips (日期时间操作/concat 等)
  13. LeetCode刷题(19)
  14. linux安装python3.5_linux安装python3.5.1
  15. Matlab期货量化交易特征选取,【策略分享】Matlab量化交易策略源码分享
  16. 如何做好学术演讲-01
  17. Keil C51详细安装教程(最新版)
  18. 电子商务正在形成共同体经济
  19. usb xhci babble error问题解决
  20. 【夜读】一个人保持年轻的5个好习惯

热门文章

  1. mysql建表语句转hive_如何获取hive建表语句
  2. SR 学习记录----JUNOS为例
  3. 代码检测利器“利特莫斯”之优化血泪史
  4. 移动安全--52--我设计的Java代码混淆解决方案
  5. IDEA插件开发指南
  6. HCIP-loT——关键特性
  7. Excel函数公式大全—MATCH函数
  8. mysql和sqlyog安装_MySQL与sqlyog安装教程图文详解
  9. 1501_FTA失效树分析简介
  10. 信息系统项目管理师进度管理论文范例