HI,
I need help to bound the path or canvas in a boundary. What I want is, when I click the mouse-left-button, holding it and move for panning. It should not move when mouse pointer reach some boundary as BORDER. I'll add code here please help me outXAML code:
<GridHeight="1000"Width="1000"x:Name="grid"><Borderx:Name="OutMoastBorder"Height="820"Width="820"ClipToBounds="True"BorderThickness="2"BorderBrush="Black"Block.IsHyphenationEnabled="True"><Borderx:Name="clipBorder"Height="810"Width="810"BorderThickness="2"BorderBrush="Black"ClipToBounds="True"><Canvasx:Name="CanvasPanel"Height="800"Width="800"Background="Beige"></Canvas></Border></Border><Grid><ButtonContent="Original Size"Height="23"Name="btn_Original"Width="75"Click="btn_Original_Click"Margin="4,4,921,973"/><TextBoxHeight="23"HorizontalAlignment="Left"Margin="4,59,0,0"Name="txtNoOfZones"VerticalAlignment="Top"Width="120"MaxLength="2"PreviewTextInput="txtNoOfZones_PreviewTextInput"/><LabelContent="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"/><ButtonContent="Zones"Height="23"HorizontalAlignment="Left"Margin="130,58,0,0"Name="btnNoOfZones"VerticalAlignment="Top"Width="41"Click="btnNoOfZones_Click"/></Grid></Grid>
Code behind for zooming and panning:
voidZoom_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=newMatrixTransform(m);// CanvasPanel.RenderTransformOrigin = new Point(0.5, 0.5);}privatePoint origin;privatePoint start;voidPan_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;}voidPan_MouseLeftBtnUp(object sender,MouseButtonEventArgs e){((Path)sender).ReleaseMouseCapture();}voidPan_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=newMatrixTransform(m);}
privateconstintNoOfSectors=180;privateconstintNoOfZones=5;voidOnLoaded(object sender,RoutedEventArgs args){constdouble PIES =2*Math.PI /NoOfSectors;Point center =newPoint(CanvasPanel.ActualWidth/2,CanvasPanel.ActualHeight/2);// center (x,y) Co-ordinates to get center of canvasdouble 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 =newPath();PathGeometry pathGeo =newPathGeometry();PathFigure pathFig =newPathFigure();double radians =2*Math.PI * s /NoOfSectors;double outerRadius = radius * z;double innerRadius = radius *((z -1));Size outerArcSize =newSize(outerRadius, outerRadius);Size innerArcSize =newSize(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 =newPoint(center.X + innerRadius *Math.Cos(radians - PIES), center.Y - innerRadius *Math.Sin(radians - PIES));// point for LINE from Center p2_1st_ArcSegment =newPoint(center.X + innerRadius *Math.Cos(radians), center.Y - innerRadius *Math.Sin(radians));// point for ARC after the 1st LINE formation p3_2nd_LineSegment =newPoint(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 =newPoint(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(newLineSegment(p1_1st_LineSegment,true)); pathFig.Segments.Add(newArcSegment(p2_1st_ArcSegment, innerArcSize,1,false,SweepDirection.Clockwise,true)); pathFig.Segments.Add(newLineSegment(p3_2nd_LineSegment,true)); pathFig.Segments.Add(newArcSegment(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 centerCanvasPanel.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;}}
Please help me out.
Regards,
Viswanath.