使用 mouseDown C++/CLI 在图像上创建矩形
我在调试这个程序时遇到困难。我正在尝试模仿微软桌面上可以拖动到矩形中的功能。
我们希望做到:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找出问题所在:
figured out what the problem was: