//xaml
<Window x:Class="WpfApp68.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:WpfApp68"
WindowState="Maximized"
WindowStyle="None"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Canvas x:Name="cvs"
Background="Transparent"
MouseDown="cvs_MouseDown"
MouseMove="cvs_MouseMove"
MouseUp="cvs_MouseUp"
MouseRightButtonUp="cvs_MouseRightButtonUp">
<Rectangle x:Name="rect"
Visibility="Collapsed"
Fill="LightBlue"
Stroke="Blue"
StrokeThickness="5"/>
</Canvas>
</Grid>
</Window>
//cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp68
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Point startPt, endPt;
private int width, height, idx = 0;
private Random rnd;
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
rnd = new Random();
rect.Visibility = Visibility.Collapsed;
width = (int)this.ActualWidth - 120;
height = (int)this.ActualHeight - 120;
InitEllipses();
}
private void InitEllipses()
{
for (int i = 0; i < 100; i++)
{
Ellipse elp = new Ellipse();
elp.Width = 100;
elp.Height = 100;
elp.Stroke = new SolidColorBrush(Colors.Blue);
elp.Fill = new SolidColorBrush(Colors.Red);
elp.ToolTip = $"{++idx},{Guid.NewGuid().ToString("N")}";
Canvas.SetLeft(elp, rnd.Next(10, width));
Canvas.SetTop(elp, rnd.Next(10, height));
if (!cvs.Children.Contains(elp))
{
cvs.Children.Add(elp);
}
}
}
private void cvs_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
rect.Visibility = Visibility.Collapsed;
}
private void cvs_MouseDown(object sender, MouseButtonEventArgs e)
{
startPt = e.GetPosition(cvs);
ResetEllipses();
}
private void ResetEllipses()
{
int childrenCount = cvs.Children.Count;
for (int i = 0; i < childrenCount; i++)
{
var elp = cvs.Children[i] as Ellipse;
if (elp != null)
{
elp.Stroke = new SolidColorBrush(Colors.Blue);
elp.StrokeThickness = 5;
}
}
}
private void cvs_MouseMove(object sender, MouseEventArgs e)
{
if (Mouse.LeftButton == MouseButtonState.Pressed)
{
endPt = e.GetPosition(cvs);
rect.Visibility = Visibility.Visible;
rect.Width = Math.Abs(startPt.X - endPt.X);
rect.Height = Math.Abs(startPt.Y - endPt.Y);
Canvas.SetLeft(rect, Math.Min(startPt.X, endPt.X));
Canvas.SetTop(rect, Math.Min(startPt.Y, endPt.Y));
SelectEllipsesInRect();
}
}
private void SelectEllipsesInRect()
{
int childrenCount = cvs.Children.Count;
for (int i = 0; i < childrenCount; i++)
{
var elp = cvs.Children[i] as Ellipse;
if (elp != null)
{
var elpX = Canvas.GetLeft(elp);
var elpY = Canvas.GetTop(elp);
if (elpX > Math.Min(startPt.X, endPt.X)
&& elpY > Math.Min(startPt.Y, endPt.Y)
&& elpX + 100 < Math.Max(startPt.X, endPt.X)
&& elpY + 100 < Math.Max(startPt.Y, endPt.Y))
{
elp.Stroke = new SolidColorBrush(Colors.Orange);
elp.StrokeThickness = 10;
}
else
{
elp.Stroke = new SolidColorBrush(Colors.Blue);
elp.StrokeThickness = 5;
}
}
}
}
private void cvs_MouseUp(object sender, MouseButtonEventArgs e)
{
}
}
}
![]()
![]()
![]()