c# - Usercontrol drag & drop on Panel -
i'm making graphical editor, experiencing problems drag & drop on panel
. ellipse doesn't take exact position drop it, , think it's placed in usercontrol of size 150;150. here's link of short movie illustrate mean: http://gyazo.com/abf5484a31e2d1ce8ebccc49bee9fdb6 in first part can see ellipse goes wrong position , in end of movie when draw line seems ellipse has block around it. how can solve issues?
form1.cs
public partial class form1 : form { bool draw = false; int x, y, xe, ye; public form1() { initializecomponent(); menucomboboxshape.combobox.datasource = enum.getvalues(typeof(item)); } public enum item { pencil, rectangle, ellipse, } private void panel_mousedown(object sender, mouseeventargs e) { draw = true; x = e.x; y = e.y; } private void panel_mouseup(object sender, mouseeventargs e) { draw = false; xe = e.x; ye = e.y; item item; enum.tryparse<item>(menucomboboxshape.combobox.selectedvalue.tostring(), out item); switch (item) { case item.pencil: using (graphics g = panel.creategraphics()) using (var pen = new pen(system.drawing.color.black)) //create pen used draw line (using statement makes sure pen disposed) { g.drawline(pen,new point(x, y), new point(xe, ye)); } break; case item.rectangle: rectangleshape recshape = new rectangleshape(x, y, xe - x, ye - y); panel.controls.add(recshape); panel.invalidate(); break; case item.ellipse: ellipseshapetest test = new ellipseshapetest(x, y, xe - x, ye - y); panel.controls.add(test); panel.invalidate(); break; default: break; } } }
ellipseshapetest.cs
class ellipseshapetest : usercontrol { private int x; private int y; private int width; private int height; public ellipseshapetest(int x, int y, int width, int height) { sety(y); setx(x); setwidth(width); setheight(height); } public int getx() { return x;} public int gety() { return y; } public int getwidth() { return width; } public int getheight() { return height; } public void setx(int newx) { x = newx; } public void sety(int newy) { y = newy; } public void setwidth(int newwidth) { width = newwidth; } public void setheight(int newheight) { height = newheight; } protected override void onpaint(painteventargs e) { base.onpaint(e); // call methods of system.drawing.graphics object. // draw aqua rectangle in rectangle represented control. e.graphics.drawellipse(pens.aqua,x,y,width,height); } protected override void onmousemove(mouseeventargs e) { // make cursor hand cursor when mouse moves // on button. cursor = cursors.hand; // call mybase.onmousemove activate delegate. base.onmousemove(e); if (e.button == mousebuttons.left) { this.location = new point(e.x, e.y); invalidate(); } }
here label
subclass draggable. same code should work control, including usercontrol
.
public draglabel() { //.. mousedown += draglabel_mousedown; mousemove += draglabel_mousemove; } point mdown { get; set; } void draglabel_mousedown(object sender, mouseeventargs e) { mdown = e.location; } void draglabel_mousemove(object sender, mouseeventargs e) { if (e.button == mousebuttons.left) { location = new point(e.x + left - mdown.x, e.y + top - mdown.y); } }
if application needs make several classes draggable, put fuctionality in dragcontroller
class , register controls need there..
as second problem: in example see one control. in simple case should make uc's backcolor = color.transparent
. once want add more such shapes hit wall and, i'm afraid, have give up on such design. see here discussion of problems , suggested solution..!
btw: using control
move around active shape fine, other shapes have drawn
onto background. (this makes more sense after have understood last paragraph ;-)
Comments
Post a Comment