c# - Restrict Panning in specific border -
i need bound path or canvas in boundary. want is, when click mouse-left-button, holding , move panning. should not move when mouse pointer reach boundary border. i'll add code here please me out xaml code:
<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 number below 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>
code behind zooming , panning:
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 center of canvas double radius = 0.1 * math.min(canvaspanel.actualwidth, canvaspanel.actualheight); (int s = 0; s <= noofsectors; s++) { (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, 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 line center p2_1st_arcsegment = new point(center.x + innerradius * math.cos(radians), center.y - innerradius * math.sin(radians)); // point arc after 1st line formation p3_2nd_linesegment = new point(center.x + outerradius * math.cos(radians), center.y - outerradius * math.sin(radians)); // point 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 2nd arc counter-clockwise closes 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 close arc, not line pathfig.isfilled = true; pathgeo.figures.add(pathfig); // binding data geometry pathgeo.fillrule = fillrule.nonzero; path.data = pathgeo; // binding whole geometry data path path.stroke = brushes.black; path.fill = brushes.silver; path.strokethickness = 0.1; // canvaspanel.rendertransformorigin = new point(0.5, 0.5); //--------------------> makes "canvas" zoom center canvaspanel.children.add(path); // binding canvaspanel 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 me out.
regards,
viswanath.
plz replace mousemove event following code in code behind.
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); } }
i have tested, surely solve this. regards, viswa
Comments
Post a Comment