主要功能:在PictureControl中,双击位置1,图片就在位置1的位置进行放大,再次双击缩小至原图像。
1、为Picture Control添加控件事件 ——我这里命名为:OnStnDblclickStaticMark
?
?2、设置该控件属性的通知为true,即双击时将向其父级发送通知,默认不通知。
?
3、在事件中添加代码块
注意:其中有一部分为全局变量:
cvPicMark表示opencv读入的图片
d_ZoomRadioMark表示图形的缩放比例
void CCameraTemplate::OnStnDblclickStaticMark()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
CRect rect;
//获取PictureControl控件区域,IDC_PICTURE为PictureControl的ID
GetDlgItem(IDC_PICTURE)->GetWindowRect(&rect);
// 将屏幕坐标切换至客户坐标
ScreenToClient(&rect);
// 获取当前鼠标的客户坐标
CPoint mypoint;
GetCursorPos(&mypoint);
ScreenToClient(&mypoint);
// 注意:cvPicMark表示opencv读入的图片
// rectUse为图片区域
CRect rectUse;
rectUse.top = rect.top;
rectUse.left = rect.left;
rectUse.bottom = rectUse.top + cvPicMark.rows;
rectUse.right = rectUse.left + cvPicMark.cols;
// 鼠标在PictureControl中的相对坐标
CPoint picPoint;
picPoint.x = mypoint.x - rectUse.left;
picPoint.y = mypoint.y - rectUse.top;
//判断鼠标点击坐标是否在图片区域内
if (rectUse.PtInRect(mypoint))
{
// 缩放后的图片
cv::Mat zoomPicMark;
// 截取后的剩余部分图片
cv::Mat imagePart;
// 图片的高.宽
int height = 0;
int weight = 0;
// 放大后图片的右上角坐标
int picCutTop = 0;
int picCutLeft = 0;
if (d_ZoomRadioMark == 1)//放大图片
{
d_ZoomRadioMark = 2;
//GetFixMat()改变Mat图像的大小,以适应窗体。但不改图像比例,图像不变形。
GetFixMat(cvOriPicMark, zoomPicMark, cvPicMark.rows * d_ZoomRadioMark, cvPicMark.cols * d_ZoomRadioMark);
picCutTop = (int)((double)picPoint.x * (d_ZoomRadioMark - 1));
picCutLeft = (int)((double)picPoint.y * (d_ZoomRadioMark - 1));
height = zoomPicMark.rows - picCutTop;
weight = zoomPicMark.cols - picCutLeft;
}
else // 还原图像
{
d_ZoomRadioMark = 1;
zoomPicMark = cvPicMark;
picCutTop = 0;
picCutLeft = 0;
height = zoomPicMark.rows;
weight = zoomPicMark.cols;
}
if (zoomPicMark.rows > rect.bottom - rect.top)
{
height = rect.bottom - rect.top;
}
if (zoomPicMark.cols > rect.right - rect.left)
{
weight = rect.right - rect.left;
}
imagePart = zoomPicMark(cv::Rect(picCutTop, picCutLeft, weight, height));
// Mat2CImage()将mat图片转image
Mat2CImage(imagePart, msPicMark);
GetDlgItem(IDC_PICTURE)->GetWindowRect(&rect);
ScreenToClient(rect);
InvalidateRect(rect);
}
UpdateData(FALSE);
}
|