[英]How could I display a crosshair cursor with x-y coordinates when hovering over a picturebox?
我想将鼠标悬停在图片框上时做一个十字准线指针,并在图片框上按鼠标左键时存储一个坐标。
我的代码如下所示:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
cv::VideoCapture cap;
cap.open(0);
if (!cap.isOpened()) {
MessageBox::Show("Failed To Open WebCam");
_getch();
return;
}
///query_maximum_resolution(cap, pictureBox1->Width, pictureBox1->Height);
cap.set(CV_CAP_PROP_FRAME_WIDTH, pictureBox1->Width);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, pictureBox1->Height);
Pen^ myPen = gcnew Pen(Brushes::Red);
while (1)
{
cap.read(frame);
pictureBox1->Image = mat2bmp.Mat2Bimap(frame);
Graphics^ g = Graphics::FromImage(pictureBox1->Image);
Point pos = this->PointToClient(System::Windows::Forms::Cursor::Position);
g->DrawLine(myPen, pos.X, 0, pos.X, pictureBox1->Height);
g->DrawLine(myPen, 0, pos.Y, pictureBox1->Width, pos.Y);
pictureBox1->Refresh();
delete g;
}
}
但是,当我运行代码时,它变得更慢且没有响应。 任何使其快速高效的想法。 任何帮助都会有所帮助。
IO发生在UI线程上,UI线程是主要的应用程序UI渲染线程。 单击按钮是事件处理程序,它将出现在UI线程上。 如果您的UI线程中有一个while循环,它将使应用程序挂起。 UI线程上完成的工作应该很小或异步。
编辑1:刚发现您已将winform标记为标签之一。 如果使用的是winforms,则必须将MouseHover事件处理程序添加到UI控件中。 每当鼠标到达该区域时,都会调用此方法( https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.forms.control.mousehover?view=netframework-4.7.2 ) 。 在这种方法中,只需编写上面的代码,而无需while循环。 这样的事情。
private: System::Void button1_MouseHover(System::Object^ sender, System::EventArgs^ e) {
cv::VideoCapture cap;
cap.open(0);
if (!cap.isOpened()) {
MessageBox::Show("Failed To Open WebCam");
_getch();
return;
}
///query_maximum_resolution(cap, pictureBox1->Width, pictureBox1->Height);
cap.set(CV_CAP_PROP_FRAME_WIDTH, pictureBox1->Width);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, pictureBox1->Height);
Pen^ myPen = gcnew Pen(Brushes::Red);
cap.read(frame);
pictureBox1->Image = mat2bmp.Mat2Bimap(frame);
Graphics^ g = Graphics::FromImage(pictureBox1->Image);
Point pos = this->PointToClient(System::Windows::Forms::Cursor::Position);
g->DrawLine(myPen, pos.X, 0, pos.X, pictureBox1->Height);
g->DrawLine(myPen, 0, pos.Y, pictureBox1->Width, pos.Y);
pictureBox1->Refresh();
delete g;
}
注意:此事件也出现在UI线程中。 每当鼠标移到感兴趣区域时,都会出现这种情况。 因此,您将不需要while循环。 在此处添加while循环将再次导致您在问题中提出的相同问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.