c# - Move the line in canvas - problems -
hi tryied move line object in app.
xaml:
<canvas x:name="canvas" height="300" width="350" mousedown="canvas_mousedown" mousemove="canvas_mousemove" mouseup="canvas_mouseup" background="transparent" />
code behind:
private void canvas_mousedown(object sender, mousebuttoneventargs e) { startpoint = e.getposition(canvas); if (e.originalsource line) { linia = (line)e.originalsource; if(!linefocus) linefocus = true; return; } else linefocus = false; } private void canvas_mousemove(object sender, mouseeventargs e) { var pos = e.getposition(canvas); translatetransform ruch = new translatetransform(pos.x - startpoint.x, pos.y - startpoint.y); linia.rendertransform = ruch; }
it works fine line moved, when try move again moving oryginal place (the place when draw them @ first time). when checked messagebox() this:
... linia = (line)e.originalsource; messagebox.show(linia.x1 + linia.y1 + linia.x2 + linia.y2); ...
allways return same values after move, reason of that?
these edits:
first need 2 points: start point(startpoint) , end point(pos). in move handler set newpoint current mouse position relative canvas. take difference between newpoint , oldpoint, 2 variables: dx , dy. add these differences points of line, , make new point next start point:
pos = e.getposition(canvas); double dx = pos.x - startpoint.x; double dy = pos.y - startpoint.y; //if want, can put here if- statement check if mouse down linia.x1 += dx; linia.x2 += dx; linia.y1 += dy; linia.y2 += dy; //here comes end of if startpoint = pos;
hope helped you!
supose want change position on 2 steps:
the line this:
x1 = 70
x2 = 80
y1 = 70
y2 = 80
you move mouse x=1 , y=1
pos = (71, 81)
startpoint = (70, 80)
1. step:
x1 = 70 + 71 - 70 = 71 same other values
the line this:
x1 = 71
x2 = 81
y1 = 71
y2 = 81
now set value of startpoint value of pos:
startpoint = (71, 81)
2. step
startpoint = (71, 81) pos = (72, 82) //you move mouse 1 , 1
this line this:
x1 = 71 + 72 - 71 = 72
x2 = 71 + 72 - 71 = 72
y1 = 81 + 82 - 81 = 82
y2 = 81 + 82 - 81 = 82
and on...