使用 mouseDown C++/CLI 在图像上创建矩形

发布于 2024-10-24 11:46:39 字数 2352 浏览 1 评论 0原文

我在调试这个程序时遇到困难。我正在尝试模仿微软桌面上可以拖动到矩形中的功能。
我们希望做到:
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);

 }


我们遇到的问题是矩形不断移动,就好像它没有将第一次单击图像的位置作为其位置一样。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-10-31 11:46:39

找出问题所在:

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

figured out what the problem was:

int w = abs(xOne - xTwo); //width
int h = abs(yOne - yTwo); //height
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文