我的矩形绘图坐标有什么问题?

时间:2014-10-14 06:38:12

标签: c# .net wpf wpf-4.0 wpf-graphics

我尝试使用以下代码允许用户在屏幕上的某些内容周围绘制一个矩形,以选择该区域。

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);
        }
    }
}

然而,虽然当我mousedown时,矩形的左上角远不及我所在的那个点。是否有不同的坐标系或什么?

2 个答案:

答案 0 :(得分:1)

您混淆了XY(或LeftTop)。变化

Canvas.SetTop(_rectangle, _startPoint.X);
Canvas.SetLeft(_rectangle, _startPoint.Y);

Canvas.SetLeft(_rectangle, _startPoint.X);
Canvas.SetTop(_rectangle, _startPoint.Y);

每次位置更改时,也无需删除和添加Rectangle:

public MainWindow()
{
    InitializeComponent();

    _rectangle = new Rectangle
    {
        Stroke = Brushes.Black,
        StrokeThickness = 1
    };

    DrawCanvas.Children.Add(_rectangle); // add it once
}

private void MapImageOnMouseMove(object sender, MouseEventArgs e)
{
    if (_drawmode == DrawMode.Draw)
    {
        var endPoint = e.GetPosition(DrawCanvas);
        _rectangle.Width = Math.Abs(endPoint.X - _startPoint.X);
        _rectangle.Height = Math.Abs(endPoint.Y - _startPoint.Y);
        Canvas.SetLeft(_rectangle, Math.Min(_startPoint.X, endPoint.X));
        Canvas.SetTop(_rectangle, Math.Min(_startPoint.Y, endPoint.Y));
    }
}

答案 1 :(得分:0)

    Point startPoint = new Point(0, 0);
    Point endPoint = new Point(1, 1);
    bool mousePress = false;
    control.MouseDown += (s, e) =>
    {
            startPoint = new Point(e.X,e.Y);
            mousePress = true;
    };
    control.MouseUp += (s, e) =>
    {
            mousePress = false;
    };
    control.MouseMove += (s, e) =>
    {
            if (mousePress)
            {
                endPoint = new Point(e.X, e.Y);
                control.Invalidate();
            }
    };
    control.Paint += (s, e) =>
    {
            Graphics g = e.Graphics;
            g.DrawRectangle(Pens.Black, new Rectangle(startPoint.X, startPoint.Y,endPoint.X-startPoint.X, endPoint.Y-startPoint.Y));
    };

我过去一直在画画,这对我来说是矩形的,不知道它是如何翻译成wpf的那样