MFC: mouse click to display the image pixels

  • mat: member variables, image storage mat
  • Width of the image controls: widthPicCtrl
  • width: Width of the mat of FIG.
  • Left corner of the image control: leftTop
  • Length of the image controls: heightPicCtrl
  • height: High mat of FIG.

//获取鼠标点击处的像素值
void CCameraLinkTestDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	//超出图片区域
	if (!mat.empty())
	{
		int pX = int((widthPicCtrl - width) / 2) + leftTop0.x;
		int pX2= int((widthPicCtrl - width) / 2) + leftTop0.x+width;
		int pY = int((heightPicCtrl - height) / 2) + leftTop0.y;
		int pY2= int((heightPicCtrl - height) / 2) + leftTop0.y +height;
        //图片显示的区间
		if (point.x>=pX&&point.x < pX2 && point.y >=pY&& point.y < pY2)
		{
			//获得图形坐标
			uchar pix = imgOf8bit.at<uchar>(point.y - pY, point.x - pX);
			CString x, y, p;
			int tmp = pix;
			x.Format(_T("%d"), point.y - pY);
			y.Format(_T("%d"), point.x - pX);
			p.Format(_T("%d"), pix);
			GetDlgItem(IDC_EDIT1)->SetWindowTextW(x);
			GetDlgItem(IDC_EDIT2)->SetWindowTextW(y);
			GetDlgItem(IDC_EDIT3)->SetWindowTextW(p);
		}
		else
		{
			GetDlgItem(IDC_EDIT1)->SetWindowTextW(NULL);
			GetDlgItem(IDC_EDIT2)->SetWindowTextW(NULL);
			GetDlgItem(IDC_EDIT3)->SetWindowTextW(NULL);
		}
	}
	CDialogEx::OnLButtonDown(nFlags, point);
}

Image control must be close to the edge of the client area

He published 194 original articles · won praise 95 · views 80000 +

Guess you like

Origin blog.csdn.net/qq_41498261/article/details/105030638