繁体   English   中英

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

[英]rectangle drawn in different point when canvas is zoomed

我有一个具有缩放功能的画布。 其中包含很多元素,因此,对于选择,我将使用一个选择框,当单击选择框时会动态创建该选择框。 单击按钮时,将矩形添加到画布,然后再次单击按钮,将其删除。 我有以下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;
                }
            }

    }

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

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

当我的画布使用原始大小时,将完美地绘制矩形,但是当我放大或缩小时,矩形将异常绘制。 它从其他方面开始借鉴。 可能是什么问题? 请帮忙

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM