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;
}
static bool IsPointInsidePath(Point point, Path path)
{
// 判断点是否在不规则图形中
return path.Data.FillContains(point);
}
private void DrawGrid()
{
double cellWidth = drawingCanvas.ActualWidth / 50;
double cellHeight = drawingCanvas.ActualHeight / 50;
for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 50; j++)
{
Rectangle gridCell = new Rectangle
{
Width = cellWidth,
Height = cellHeight,
Stroke = Brushes.Blue,
StrokeThickness = 0.5
};
Canvas.SetLeft(gridCell, i * cellWidth);
Canvas.SetTop(gridCell, j * cellHeight);
drawingCanvas.Children.Add(gridCell);
}
}
}
}
}