drawing reverse rectangle using mouse
-
hi i am making an applicaiton that draws the rectangles using mouse,when mouse is down it takes the starting points of rectangle(x,y) and when mouse moves then ending point of rectangle are taken and rectangle is drawn,here is code of that
private void frmMedtronic_MouseMove(object sender, MouseEventArgs e) { if (mIsMouseDown) { //increase width of rectangle towards Rite if (e.Location.X > mrctRectangle.X) mrctRectangle.Width = e.Location.X - mrctRectangle.X; else // * trouble here { mrctRectangle.X = e.Location.X; mrctRectangle.Width = e.Location.X - mrctRectangle.X; } if (e.Location.Y > mrctRectangle.Y) mrctRectangle.Height = e.Location.Y - mrctRectangle.Y; //moving Rectangle towards Left * else //trouble * { mrctRectangle.Y = e.Location.Y; mrctRectangle.Height = e.Location.Y - mrctRectangle.Y; } } Invalidate(); }
and in formpaint drawrectangle function is called. The problem is in moving rectangle toward left or moving upwards,as in this case ending points are greater then starting and in drawing as widht and height of rectangle can only b positive values not negative,i am having trouble in moving towards upward and left as point of screen are from 0 and not negative as in openGL, so suggest me what to do.Regards. Tasleem Arif
-
hi i am making an applicaiton that draws the rectangles using mouse,when mouse is down it takes the starting points of rectangle(x,y) and when mouse moves then ending point of rectangle are taken and rectangle is drawn,here is code of that
private void frmMedtronic_MouseMove(object sender, MouseEventArgs e) { if (mIsMouseDown) { //increase width of rectangle towards Rite if (e.Location.X > mrctRectangle.X) mrctRectangle.Width = e.Location.X - mrctRectangle.X; else // * trouble here { mrctRectangle.X = e.Location.X; mrctRectangle.Width = e.Location.X - mrctRectangle.X; } if (e.Location.Y > mrctRectangle.Y) mrctRectangle.Height = e.Location.Y - mrctRectangle.Y; //moving Rectangle towards Left * else //trouble * { mrctRectangle.Y = e.Location.Y; mrctRectangle.Height = e.Location.Y - mrctRectangle.Y; } } Invalidate(); }
and in formpaint drawrectangle function is called. The problem is in moving rectangle toward left or moving upwards,as in this case ending points are greater then starting and in drawing as widht and height of rectangle can only b positive values not negative,i am having trouble in moving towards upward and left as point of screen are from 0 and not negative as in openGL, so suggest me what to do.Regards. Tasleem Arif
Try this:
int StartX = (e.Location.X > mrctRectangle.X ? e.Location.X : mrctRectangle.X);
int StartY = (e.Location.Y > mrctRectangle.Y ? e.Location.Y : mrctRectangle.Y);
int EndX = (e.Location.X < mrctRectangle.X ? e.Location.X : mrctRectangle.X);
int EndY = (e.Location.Y < mrctRectangle.Y ? e.Location.Y : mrctRectangle.Y);You have here the start and end points of an rectangle. Hope it helps.
I will use Google before asking dumb questions
-
hi i am making an applicaiton that draws the rectangles using mouse,when mouse is down it takes the starting points of rectangle(x,y) and when mouse moves then ending point of rectangle are taken and rectangle is drawn,here is code of that
private void frmMedtronic_MouseMove(object sender, MouseEventArgs e) { if (mIsMouseDown) { //increase width of rectangle towards Rite if (e.Location.X > mrctRectangle.X) mrctRectangle.Width = e.Location.X - mrctRectangle.X; else // * trouble here { mrctRectangle.X = e.Location.X; mrctRectangle.Width = e.Location.X - mrctRectangle.X; } if (e.Location.Y > mrctRectangle.Y) mrctRectangle.Height = e.Location.Y - mrctRectangle.Y; //moving Rectangle towards Left * else //trouble * { mrctRectangle.Y = e.Location.Y; mrctRectangle.Height = e.Location.Y - mrctRectangle.Y; } } Invalidate(); }
and in formpaint drawrectangle function is called. The problem is in moving rectangle toward left or moving upwards,as in this case ending points are greater then starting and in drawing as widht and height of rectangle can only b positive values not negative,i am having trouble in moving towards upward and left as point of screen are from 0 and not negative as in openGL, so suggest me what to do.Regards. Tasleem Arif
Hi, this looks the easiest to me: store MouseDown point in pt1, current MouseMove position in pt2, and in OnPaint for both x and y do:
xMin= x1<=x2 ? x1 : x2;
width= x1+x2-xMin -xMin; // x1+x2-xMin is xMax!ADDED: then Graphics.DrawRectangle(..., xMin, yMin, width, height); BTW: You might want to add 1 to width/height to always see the rectangle... :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets