本实例主要是使用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&