限制特定边框中的平移

时间:2014-12-02 14:09:34

标签: c# wpf

我需要帮助来限制边界中的路径或画布。我想要的是,当我单击鼠标左键时,按住它并移动以进行平移。当鼠标指针到达BORDER的某个边界时,它不应该移动。我在这里添加代码请帮帮我 XAML代码:

    <Border x:Name="OutMoastBorder" Height="820" Width="820" ClipToBounds="True" BorderThickness="2" BorderBrush="Black" Block.IsHyphenationEnabled="True">
        <Border x:Name="clipBorder" Height="810" Width="810" BorderThickness="2" BorderBrush="Black" ClipToBounds="True">

    <Canvas x:Name="CanvasPanel" Height="800" Width="800" Background="Beige">

            </Canvas>

    </Border>
    </Border>
    <Grid>
        <Button Content="Original Size" Height="23" Name="btn_Original" Width="75" Click="btn_Original_Click" Margin="4,4,921,973" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="4,59,0,0" Name="txtNoOfZones" VerticalAlignment="Top" Width="120" MaxLength="2" PreviewTextInput="txtNoOfZones_PreviewTextInput" />
        <Label Content="Enter a number below for No. of Zones" Height="28" HorizontalAlignment="Left" Margin="4,33,0,0" Name="label1" VerticalAlignment="Top" Width="220" FontFamily="Vijaya" FontSize="15" FontWeight="Bold" FontStyle="Normal" />
        <Button Content="Zones" Height="23" HorizontalAlignment="Left" Margin="130,58,0,0" Name="btnNoOfZones" VerticalAlignment="Top" Width="41" Click="btnNoOfZones_Click" />
    </Grid>
</Grid>

缩放和平移的代码:

void Zoom_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            Point p = e.MouseDevice.GetPosition(((Path)sender));

        Matrix m = CanvasPanel.RenderTransform.Value;
        if (e.Delta > 0)
            m.ScaleAtPrepend(1.1, 1.1, p.X, p.Y);
        else
            m.ScaleAtPrepend(1 / 1.1, 1 / 1.1, p.X, p.Y);

        CanvasPanel.RenderTransform = new MatrixTransform(m);
        // CanvasPanel.RenderTransformOrigin = new Point(0.5, 0.5);
    }

    private Point origin;
    private Point start;

    void Pan_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (((Path)sender).IsMouseCaptured) return;
        ((Path)sender).CaptureMouse();

        start = e.GetPosition(clipBorder);
        origin.X = CanvasPanel.RenderTransform.Value.OffsetX;
        origin.Y = CanvasPanel.RenderTransform.Value.OffsetY;

    }

    void Pan_MouseLeftBtnUp(object sender, MouseButtonEventArgs e)
    {
        ((Path)sender).ReleaseMouseCapture();

    }



    void Pan_MouseMove(object sender, MouseEventArgs e)
    {

        if (!((Path)sender).IsMouseCaptured) return;
        Point p = e.MouseDevice.GetPosition(clipBorder);

        Matrix m = CanvasPanel.RenderTransform.Value;
        m.OffsetX = origin.X + (p.X - start.X);
        m.OffsetY = origin.Y + (p.Y - start.Y);
        CanvasPanel.RenderTransform = new MatrixTransform(m);
    }
private const int NoOfSectors = 180;

private const int NoOfZones = 5;
void OnLoaded(object sender, RoutedEventArgs args)
{
    const double PIES = 2 * Math.PI / NoOfSectors;
    Point center = new Point(CanvasPanel.ActualWidth / 2, CanvasPanel.ActualHeight / 2);   // center (x,y) Co-ordinates to get center of canvas
    double radius = 0.1 * Math.Min(CanvasPanel.ActualWidth, CanvasPanel.ActualHeight);
    for (int s = 0; s <= NoOfSectors; s++)
        {

            for (int z = 1; z <= NoOfZones; z++)
            {
                Path path = new Path();
                PathGeometry pathGeo = new PathGeometry();
                PathFigure pathFig = new PathFigure();

                double radians = 2 * Math.PI * s / NoOfSectors;

                double outerRadius = radius * z;
                double innerRadius = radius * ((z - 1));

                Size outerArcSize = new Size(outerRadius, outerRadius);
                Size innerArcSize = new Size(innerRadius, innerRadius);

                Point p1_1st_LineSegment;  //------------------------------> Points variable, to store each iterate point in these.
                Point p2_1st_ArcSegment;
                Point p3_2nd_LineSegment;
                Point p4_2nd_ArcSegment;

                p1_1st_LineSegment = new Point(center.X + innerRadius * Math.Cos(radians - PIES), center.Y - innerRadius * Math.Sin(radians - PIES));  // point for LINE from Center
                p2_1st_ArcSegment = new Point(center.X + innerRadius * Math.Cos(radians), center.Y - innerRadius * Math.Sin(radians));                 // point for ARC after the 1st LINE formation
                p3_2nd_LineSegment = new Point(center.X + outerRadius * Math.Cos(radians), center.Y - outerRadius * Math.Sin(radians));                // Point for 2nd LINE after forming both LINE abd ARC
                p4_2nd_ArcSegment = new Point(center.X + outerRadius * Math.Cos(radians - PIES), center.Y - outerRadius * Math.Sin(radians - PIES));   // Point for 2nd ARC which is Counter-CLockwise that closes a path


                pathFig.StartPoint = center;
                pathFig.Segments.Add(new LineSegment(p1_1st_LineSegment, true));
                pathFig.Segments.Add(new ArcSegment(p2_1st_ArcSegment, innerArcSize, 1, false, SweepDirection.Clockwise, true));
                pathFig.Segments.Add(new LineSegment(p3_2nd_LineSegment, true));
                pathFig.Segments.Add(new ArcSegment(p4_2nd_ArcSegment, outerArcSize, 1, false, SweepDirection.Counterclockwise, true));
                pathFig.IsClosed = false;             //false because, path has to be close with ARC, not with LINE
                pathFig.IsFilled = true;
                pathGeo.Figures.Add(pathFig);        // binding data to a Geometry
                pathGeo.FillRule = FillRule.Nonzero;
                path.Data = pathGeo;                // binding whole geometry data to a path
                path.Stroke = Brushes.Black;
                path.Fill = Brushes.Silver;
                path.StrokeThickness = 0.1;
                // CanvasPanel.RenderTransformOrigin = new Point(0.5, 0.5);    //--------------------> this makes "Canvas" to be Zoom from center
                CanvasPanel.Children.Add(path);    // binding to a CanvasPanel as a children
                path.MouseLeftButtonDown += MouseLeftButtonClick;  // calling Mouse-click-event
                path.MouseWheel += Zoom_MouseWheel;

                path.MouseLeftButtonDown += Pan_MouseLeftButtonDown;
                path.MouseLeftButtonUp += Pan_MouseLeftBtnUp;
                path.MouseMove += Pan_MouseMove;


            }
        }

请帮帮我。

此致

Viswanath。

1 个答案:

答案 0 :(得分:0)

PLz用后面的代码中的以下代码替换MouseMove事件。

 void Pan_MouseMove(object sender, MouseEventArgs e)
    {

        if (!((Path)sender).IsMouseCaptured) return;
        Point p = e.MouseDevice.GetPosition(clipBorder);

        if (p.X > 0 && p.Y > 0 && p.X < clipBorder.ActualWidth && p.Y < clipBorder.ActualHeight)
        {
            Matrix m = CanvasPanel.RenderTransform.Value;
            m.OffsetX = origin.X + (p.X - start.X);
            m.OffsetY = origin.Y + (p.Y - start.Y);
            CanvasPanel.RenderTransform = new MatrixTransform(m);
        }
    }

我已经测试过,这肯定会解决这个问题。 问候, Viswa