Создать прямоугольник на изображении, используя mouseDown C ++ / CLI - PullRequest
0 голосов
/ 22 марта 2011

У меня проблемы с отладкой этой программы.Я пытаюсь имитировать функцию на рабочем столе Microsoft, которую вы можете перетащить в прямоугольник.
Мы хотим сделать так, чтобы оно:
1. Чтобы начать рисовать прямоугольник, нажмите кнопку мыши вниз.
2. перетаскивая mosue вниз, вы создаете форму и размер прямоугольника.
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);

 }


проблема, с которой мы сталкиваемся, заключается в том, что прямоугольник продолжает двигаться, как будто он не сохраняет местоположение первого щелчка вниз на изображении в качестве своего местоположения.

1 Ответ

0 голосов
/ 22 марта 2011

разобрался в чем проблема:

int w = abs(xOne - xTwo); //width
int h = abs(yOne - yTwo); //height
...