上位机mfc图像局部放大源代码
例程实现一个简单功能,
通过鼠标在图片上移动,放大鼠标移动区域的图片。
效果如图,
实现时,图片外围多余部分未刷新,出现重叠,可以自己强制刷新。
鼠标操作关键代码为:
- void CZoomPartView::OnMouseMove(UINT nFlags, CPoint point)
- {
- //计算要放大的局部矩形的源图像位置和目标位置
- CString cord;
- int dd;
- CRect srect,drect,mrect;
- srect.left = point.x - s;
- srect.top = point.y - s;
- srect.right = point.x + s;
- srect.bottom = point.y + s;
-
- drect.left = point.x - d;
- drect.top = point.y - d;
- drect.right = point.x + d;
- drect.bottom = point.y + d;
-
- mrect.left = oldx - d;
- mrect.top = oldy - d;
- mrect.right = oldx + d;
- mrect.bottom = oldy + d;
- dd = 2*d;
- CDC * pDC = GetDC();
- OnPrepareDC(pDC);
- //放大图像
- if (recover)
- {
- pDC->BitBlt(mrect.left,mrect.top,dd,dd,
- m_pdcMem,mrect.left,mrect.top,mana);
- }
- pDC->StretchBlt(drect.left,drect.top,
- drect.Width(),drect.Height(),m_pdcMem,srect.left,
- srect.top,srect.Width(),srect.Height(),SRCCOPY);
- oldx = point.x; oldy = point.y;
- ReleaseDC(pDC);
-
- recover = true;
- CView::OnMouseMove(nFlags, point);
- }
- void CZoomPartView::OnLButtonDown(UINT nFlags, CPoint point)
- {
- //如果鼠标位置不在位图上,则还原位图大小显示
- CRect rc(0,0,m_sizeSource.cx,m_sizeSource.cy);
- if(!rc.PtInRect(point))
- {
- Invalidate();
- }
- else if (d > 5)//如果放大倍数大于5,就继续减小放大倍数,然后进行放大显示
- {
- CDC * pDC = GetDC();
- pDC->StretchBlt(oldx - d,oldy - d,2*d,
- 2*d,m_pdcMem,oldx - d,oldy - d,2*d,2*d,mana);
- d -= 10;
- ReleaseDC(pDC);
- CZoomPartView::OnMouseMove(nFlags, point);
- }
- CView::OnLButtonDown(nFlags, point);
- }
- void CZoomPartView::OnRButtonDown(UINT nFlags, CPoint point)
- {
- //如果鼠标位置不在位图上,则还原位图大小显示
- CRect rc(0,0,m_sizeSource.cx,m_sizeSource.cy);
- if(!rc.PtInRect(point))
- {
- Invalidate();
- }
- else if (d <150)//如果放大倍数小于150,就继续增加放大倍数,然后进行放大显示
- {
- d += 10;
- CZoomPartView::OnMouseMove(nFlags, point);
- }
- CView::OnRButtonDown(nFlags, point);
- }
复制代码
|