winforms - C# Windows Forms. Moveable areas on the PictureBox -
i'm working images like: http://imgur.com/a2vkb
i've managed find vertical line between pages on scanned image. there errors , need make option user change line position , angle. think nice within picturebox.
i need somehow draw line between 2 movable points on picturebox current image. when move point, position of line , it's angle have changed properly.
here sample code can use need. uses 4 events :
- paint
- mousedown
- mousemove
- mouseup
you can copy-paste code form called form1, picture box called picturebox1
int handleradius = 3; int mpointmoveinprogress = 0; point mpoint1, mpoint2; public form1() { initializecomponent(); mpoint1 = new point(50, 50); // set correct default values mpoint1 = new point(50, 300); // set correct default values } private void picturebox1_paint(object sender, painteventargs e) { // draw line e.graphics.drawline(new pen(color.black, 2), mpoint1, mpoint2); rectangle rectangle; // draw first handle rectangle = new rectangle(mpoint1.x - handleradius, mpoint1.y - handleradius, handleradius * 2, handleradius * 2); e.graphics.fillrectangle(brushes.white, rectangle); e.graphics.drawrectangle(pens.black, rectangle); // draw second handle rectangle = new rectangle(mpoint2.x - handleradius, mpoint2.y - handleradius, handleradius * 2, handleradius * 2); e.graphics.fillrectangle(brushes.white, rectangle); e.graphics.drawrectangle(pens.black, rectangle); } private void picturebox1_mousedown(object sender, mouseeventargs e) { // determine if point under cursor. if so, declare move in progress if (math.abs(e.x - mpoint1.x) < handleradius && math.abs(e.y - mpoint1.y) < handleradius) mpointmoveinprogress = 1; else if (math.abs(e.x - mpoint2.x) < handleradius && math.abs(e.y - mpoint2.y) < handleradius) mpointmoveinprogress = 2; else mpointmoveinprogress = 0; } private void picturebox1_mousemove(object sender, mouseeventargs e) { if (mpointmoveinprogress == 1) // if moving first point { mpoint1.x = e.x ; mpoint1.y = e.y ; refresh(); } else if (mpointmoveinprogress == 2) // if moving second point { mpoint2.x = e.x ; mpoint2.y = e.y ; refresh(); } else // if moving in picturebox: change cursor hand if above handle { if (math.abs(e.x - mpoint1.x) < handleradius && math.abs(e.y - mpoint1.y) < handleradius) cursor.current = cursors.hand; else if (math.abs(e.x - mpoint2.x) < handleradius && math.abs(e.y - mpoint2.y) < handleradius) cursor.current = cursors.hand; else cursor.current = cursors.default; } } private void picturebox1_mouseup(object sender, mouseeventargs e) { // declare no move in progress mpointmoveinprogress = 0; }
Comments
Post a Comment