I am having difficulty debugging this program. I am trying to imitate the function on microsoft desktop that you can drag into a rectangle.
We want to make it so that it:
1. in order to start drawing the rectangle you press the mouse down.
2. by dragging the mosue downwards you create the shape and size of the rectangle.
3. by unclicking the mouse you are ending the drawing of the rectangle
4. we want to be able to keep track of the beginning point and the end point.
So far we have
private: System::Void pictureBox1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) {
// e->Graphics;
//put pen where it belongs..
Pen^ blackPen = gcnew Pen( Color::Black,3.0f );
if(drawing)
{
e->Graphics->DrawRectangle( blackPen, *getRect(ROIlocation1->X, ROIlocation1->Y, ROIlocation2->X, ROIlocation2->Y ));
}
//pictureBox1->Invalidate();
}
private: System::Void pictureBox1_MouseDown(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
currentPos = startPos = e->Location;
ROIlocation1 = startPos;
drawing = true;
pictureBox1->Invalidate();
}
private: System::Void pictureBox1_MouseUp(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
//NEW
if (drawing){
drawing = false;
//getRect(startPos->X, startPos->Y, currentPos->X, currentPos->Y);
pictureBox1->Invalidate();
}
}
private: System::Void pictureBox1_MouseMove(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
//NEW
currentPos=e->Location;
ROIlocation2=currentPos;
if(drawing) pictureBox1->Invalidate();
}
This is the getRect function:
System::Drawing::Rectangle^ getRect(int xOne, int yOne, int xTwo, int yTwo){
// ms drawRect(x,y,w,h) wants the upper left corner of the rectangle, and the width and height.
// we are given two sets of coordinates. the user can draw the rectangle in four ways,
// not necessarily starting with the upper right hand corner of the rectangle.
// this function will return a rectangle with the appropriate attributes
// that the user expects.
int ulpX = std::min(xOne, xTwo); // upper left point x
int ulpY = std::max(yOne, yTwo); //upper left point y
int h = abs(xOne - xTwo); //height
int w = abs(yOne - yTwo); //width
return gcnew System::Drawing::Rectangle(ulpX, ulpY, w, h);
}
the problem we are having is the rectangle keeps moving around as if it isn't keeping the location of the first click down on the image as its location.
figured out what the problem was:
int w = abs(xOne - xTwo); //width
int h = abs(yOne - yTwo); //height