I'm trying to use the following code to allow the user to draw a rectangle around something on the screen, to select that area.
public partial class MainWindow : Window
{
public enum DrawMode
{
Move,
Draw
}
private DrawMode _drawmode;
private Point _startPoint;
private Rectangle _rectangle;
public MainWindow()
{
_rectangle = new Rectangle();
_rectangle.Stroke = new SolidColorBrush(Colors.Black);
_rectangle.StrokeThickness = 1;
InitializeComponent();
}
private void MapImageOnMouseDown(object sender, MouseButtonEventArgs e)
{
_drawmode = DrawMode.Draw;
_startPoint = e.GetPosition(DrawCanvas);
}
private void MapImageOnMouseUp(object sender, MouseButtonEventArgs e)
{
_drawmode = DrawMode.Move;
}
private void MapImageOnMouseMove(object sender, MouseEventArgs e)
{
if (_drawmode == DrawMode.Draw)
{
DrawCanvas.Children.Remove(_rectangle);
var endPoint = e.GetPosition(DrawCanvas);
var width = Math.Abs(endPoint.X - _startPoint.X);
var height = Math.Abs(endPoint.Y - _startPoint.Y);
_rectangle.Width = width;
_rectangle.Height = height;
DrawCanvas.Children.Add(_rectangle);
Canvas.SetTop(_rectangle, _startPoint.X);
Canvas.SetLeft(_rectangle, _startPoint.Y);
}
}
}
However, although when I mousedown, the top left of the rectangle is nowhere near the point that I mousedown at. Are there different coordinate systems or something?