簡體   English   中英

將鼠標懸停在圖片框上時,如何顯示帶有xy坐標的十字光標?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM