画布缩放时在不同点绘制的矩形

时间:2016-02-18 06:41:09

标签: c# wpf canvas rectangles

我有一个具有缩放功能的画布。其中有很多元素,所以,为了选择,我将使用一个选择框,我在单击选择框时动态创建。单击按钮,我将矩形添加到画布,然后再次单击按钮,我将其删除。 我有以下xaml代码:

<Viewbox x:Name="vbCanvas">
            <Grid x:Name="theGrid"
                  MouseDown="Grid_MouseDown"
                  MouseUp="Grid_MouseUp"
                  MouseMove="Grid_MouseMove"
                  Background="Transparent">

                <Canvas Name="canvasWaSNA" Margin="0,10,10,10" Height="720" Width="1280">

                </Canvas>
            </Grid>
        </Viewbox>

theGrid的鼠标事件在画布上的运行时绘制矩形。这些事件的代码是:

bool mouseDown = false; 
Point mouseDownPos;
Point mouseUpPos;
private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
    {
            mouseDown = true;
            mouseDownPos = e.GetPosition(theGrid);
            theGrid.CaptureMouse();
            // Initial placement of the drag selection box.         
            Canvas.SetLeft(sBox, mouseDownPos.X);
            Canvas.SetTop(sBox, mouseDownPos.Y);
            sBox.Width = 0;
            sBox.Height = 0;

            // Make the drag selection box visible.
            sBox.Visibility = Visibility.Visible;
        }
    }

    private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
    {
            // Release the mouse capture and stop tracking it.
            mouseDown = false;
            mouseUpPos = e.GetPosition(theGrid);

            theGrid.ReleaseMouseCapture();

            // Show the drag selection box.
            sBox.Visibility = Visibility.Visible;

            MessageBox.Show(mouseDownPos.ToString() + " " + mouseUpPos.ToString());

    }

    private void Grid_MouseMove(object sender, MouseEventArgs e)
    {
            if (mouseDown)
            {
                // When the mouse is held down, reposition the drag selection box.
                Point mousePos = e.GetPosition(theGrid);

                if (mouseDownPos.X < mousePos.X)
                {
                    Canvas.SetLeft(sBox, mouseDownPos.X);
                    sBox.Width = mousePos.X - mouseDownPos.X;
                }
                else
                {
                    Canvas.SetLeft(sBox, mousePos.X);
                    sBox.Width = mouseDownPos.X - mousePos.X;
                }

                if (mouseDownPos.Y < mousePos.Y)
                {
                    Canvas.SetTop(sBox, mouseDownPos.Y);
                    sBox.Height = mousePos.Y - mouseDownPos.Y;
                }
                else
                {
                    Canvas.SetTop(sBox, mousePos.Y);
                    sBox.Height = mouseDownPos.Y - mousePos.Y;
                }
            }

    }

要在运行时创建Rectangle,我必须单击一个按钮。该按钮的事件如下:

private void select_Click_1(object sender, RoutedEventArgs e)
    {

            if (!canvasWaSNA.Children.Contains(sBox))
            {

                sBox.Name = "selectionBox";
                sBox.StrokeThickness = 1.5 / zoomfactor;
                sBox.StrokeDashArray = new DoubleCollection { 1, 2 };
                sBox.Visibility = System.Windows.Visibility.Collapsed;
                sBox.Stroke = Brushes.Gray;
                canvasWaSNA.Children.Add(sBox);

        }
        else
        {
            sBox.Visibility = System.Windows.Visibility.Collapsed;
            canvasWaSNA.Children.Remove(sBox);

        }
    }

我使用以下代码放大画布:

double zoomfactor = 1.0;
    void window_MouseWheel(object sender, MouseWheelEventArgs e)
    {
        Point p = e.MouseDevice.GetPosition(canvasWaSNA); //gets the location of the canvas at which the mouse is pointed

        Matrix m = canvasWaSNA.RenderTransform.Value;
        if (e.Delta > 0)
        { //the amount of wheel of mouse changed. e.Delta holds int value.. +ve for uproll and -ve for downroll
            m.ScaleAtPrepend(1.1, 1.1, p.X, p.Y);
            zoomfactor *= 1.1;
        }
        else
        {
            m.ScaleAtPrepend(1 / 1.1, 1 / 1.1, p.X, p.Y);
            zoomfactor /= 1.1;
        }

        canvasWaSNA.RenderTransform = new MatrixTransform(m);
    }

当我的画布处于原始大小时,矩形被完美绘制但是当我放大或缩小时,矩形被异常绘制。它开始从其他方面吸取。可能是什么问题?请帮忙

1 个答案:

答案 0 :(得分:0)

好吧,我不应该相对于theGrid捕获鼠标位置,因为我必须创建相对于画布的矩形。所以,我必须得到e.GetPosition(canvasWaSNA)的位置,并显示预期的结果。它捕获了画布上的鼠标位置。现在,即使放大或缩小,矩形也能完美绘制。 此外,我通过使用画布的zoomfactor引用它来改进绘制的矩形的StrokeThickness

private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
    {mouseDown = true;
            mouseDownPos = e.GetPosition(canvasWaSNA);
            theGrid.CaptureMouse();
            sBox.StrokeThickness = 1.5 / zoomfactor;

            // Initial placement of the drag selection box.         
            Canvas.SetLeft(sBox, mouseDownPos.X);
            Canvas.SetTop(sBox, mouseDownPos.Y);
            sBox.Width = 0;
            sBox.Height = 0;

            // Make the drag selection box visible.
            sBox.Visibility = Visibility.Visible;
        }