WPF设置框选不规则图形,并把框选范围内的不规则图形内容的图形显示出来
前端XAML代码
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="600">
<Canvas Name="drawingCanvas" Background="LightGray" MouseLeftButtonDown="Canvas_MouseLeftButtonDown" MouseMove="Canvas_MouseMove" MouseLeftButtonUp="Canvas_MouseLeftButtonUp">
<!-- This is the canvas where the irregular shape will be drawn -->
</Canvas>
</Window>
后端代码
using System.Text;
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 WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private bool isDrawing = false;
private PathFigure currentPathFigure;
private List<PathFigure> pathFigures = new List<PathFigure>();
private Path path;
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
InitializeDrawingCanvas();
DrawGrid(); // Draw the 10x10 grid
}
private void InitializeDrawingCanvas()
{
path = new Path
{
Stroke = Brushes.Black,
StrokeThickness = 2
};
drawingCanvas.Children.Add(path);
}
private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
isDrawing = true;
pathFigures = new List<PathFigure>();
currentPathFigure = new PathFigure();
currentPathFigure.StartPoint = e.GetPosition(drawingCanvas);
pathFigures.Add(currentPathFigure);
path.Data = new PathGeometry(pathFigures);
}
private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
if (isDrawing)
{
// Remove the last line segment (connecting to the start point)
if (currentPathFigure.Segments.Count > 1)
currentPathFigure.Segments.RemoveAt(currentPathFigure.Segments.Count - 1);
currentPathFigure.Segments.Add(new LineSegment(e.GetPosition(drawingCanvas), true));
// Add a new line segment connecting to the start point
currentPathFigure.Segments.Add(new LineSegment(currentPathFigure.StartPoint, true));
path.Data = new PathGeometry(pathFigures);
}
}
private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
isDrawing = false;
UpdateDrawGrid();
pathFigures = new List<PathFigure>();
currentPathFigure = new PathFigure();
}
public bool IsRectInsidePath(Rect rect)
{
if (path.Data.FillContains(new Point(rect.X, rect.Y)) ||
path.Data.FillContains(new Point(rect.X + rect.Width, rect.Y)) ||
path.Data.FillContains(new Point(rect.X, rect.Y + rect.Height)) ||
path.Data.FillContains(new Point(rect.X + rect.Width, rect.Y + rect.Height)))
{
return true;
}
return false;
}
static bool IsPointInsidePath(Point point, Path path)
{
// 判断点是否在不规则图形中
return path.Data.FillContains(point);
}
private void UpdateDrawGrid()
{
double cellWidth = drawingCanvas.ActualWidth / 50;
double cellHeight = drawingCanvas.ActualHeight / 50;
int index = 0;
for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 50; j++)
{
var rect = new Rect(i * cellWidth, j * cellHeight, cellWidth, cellHeight);
if (IsRectInsidePath(rect))
{
var list = GetAllChildControls(drawingCanvas);
foreach (var item in list)
{
if (item is Rectangle r)
{
if (r.Name == "Rect_" + index)
{
r.Fill = Brushes.Red;
}
}
}
}
index++;
}
}
}
private void DrawGrid()
{
double cellWidth = drawingCanvas.ActualWidth / 50;
double cellHeight = drawingCanvas.ActualHeight / 50;
int index = 0;
for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 50; j++)
{
Rectangle gridCell = new Rectangle
{
Name ="Rect_"+ index.ToString(),
Width = cellWidth,
Height = cellHeight,
Stroke = Brushes.Blue,
StrokeThickness = 0.5
};
Canvas.SetLeft(gridCell, i * cellWidth);
Canvas.SetTop(gridCell, j * cellHeight);
drawingCanvas.Children.Add(gridCell);
index++;
}
}
}
static List<UIElement> GetAllChildControls(DependencyObject parent)
{
var allChildControls = new System.Collections.Generic.List<UIElement>();
// 使用递归遍历VisualTree获取所有子控件
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is UIElement)
{
allChildControls.Add(child as UIElement);
}
// 递归处理子控件的子控件
allChildControls.AddRange(GetAllChildControls(child));
}
return allChildControls;
}
}
}