Имам затруднения с отстраняването на грешки в тази програма. Опитвам се да имитирам функцията на работния плот на Microsoft, която можете да плъзнете в правоъгълник.
Искаме да го направим така, че:
1. За да започнете да рисувате правоъгълника, натиснете мишката надолу.
2. чрез плъзгане на мишката надолу вие създавате формата и размера на правоъгълника.
3. като махнете мишката, вие завършвате чертането на правоъгълника
4. искаме да можем да следим на началната точка и крайната точка.
Досега имаме
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();
}
Това е функцията getRect:
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);
}
проблемът, който имаме, е, че правоъгълникът продължава да се движи, сякаш не запазва местоположението на първото щракване надолу върху изображението като свое местоположение.