分割窗口后,CView 中显示影像

在新建MFC类CView1,继承CView

图像显示:

    BITMAPINFO *m_pBinfo_Image = (BITMAPINFO *)(new BYTE[sizeof(BITMAPINFOHEADER) + 256*sizeof(RGBQUAD)]);
    memset(m_pBinfo_Image, 0, sizeof(BITMAPINFOHEADER) + 256*sizeof(RGBQUAD));
    m_pBinfo_Image->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);// + 256*sizeof(RGBQUAD);
    m_pBinfo_Image->bmiHeader.biBitCount = 8;
    m_pBinfo_Image->bmiHeader.biPlanes   = 1;
    m_pBinfo_Image->bmiHeader.biClrUsed  = 256;
    m_pBinfo_Image->bmiHeader.biWidth    = CAMERA_WIDTH;
    m_pBinfo_Image->bmiHeader.biHeight   = CAMERA_HEIGHT;

    RGBQUAD *pColor = m_pBinfo_Image->bmiColors;
    int i;
    for (i=0;i<256;i++)
    {
        pColor[i].rgbBlue = pColor[i].rgbGreen = pColor[i].rgbRed = i;
    }

void CView1::OnDraw(CDC* pDC)
{
    CString str;
    CZgMemDC   *pMemDC = new CZgMemDC(pDC);
    //BITMAPINFO结构定义了Windows设备无关位图(DIB)的度量和颜色信息
    BITMAPINFO *pInfo = m_pBinfo_Image  ;
    BYTE *pImgData = m_pImgDataL;//图像内存,
    int w  = CAMERA_WIDTH;//图像大小
    int h = CAMERA_HEIGHT;
    pInfo->bmiHeader.biWidth    = w;
    pInfo->bmiHeader.biHeight   = h;

    CRect rect;
    GetClientRect(&rect);
    int cenx = rect.Width()/2;
    int ceny = rect.Height()/2;
    int x0 = cenx - m_CenX*m_Scale;
    int y0 = ceny - (h-1-m_CenY)*m_Scale;

    CPen *pPenOld,PenRed,PenGreen;
    PenRed.CreatePen(PS_SOLID,1,RGB(255,0,0));
    PenGreen.CreatePen(PS_SOLID,1,RGB(0,255,0));
    pPenOld = pMemDC->SelectObject(&PenRed);

    if( pImgData != NULL )
    {
        HDC hdc = pMemDC->GetSafeHdc();
        CBitmap *pOld = (CBitmap*)pMemDC->SelectObject(pImgData);
        SetStretchBltMode(hdc, COLORONCOLOR);
        StretchDIBits(hdc,
            x0, y0, w*m_Scale, h*m_Scale,
            0, 0, w, h,
            pImgData,
            pInfo,
            DIB_RGB_COLORS,
            SRCCOPY);
        pMemDC->SelectObject(pOld);
    }

    pMemDC->MoveTo(0,ceny); pMemDC->LineTo(cenx*2,ceny);
    pMemDC->MoveTo(cenx,0); pMemDC->LineTo(cenx,ceny*2);

    pMemDC->SelectObject(pPenOld);
    PenRed.DeleteObject();
    PenGreen.DeleteObject();
    delete pMemDC;

}

关闭背景刷新,避免图像的闪烁

BOOL CView1::OnEraseBkgnd(CDC* pDC)
{
    // TODO: Add your message handler code here and/or call default
    return TRUE;
    return CView::OnEraseBkgnd(pDC);
}

鼠标操作成员变量

double  m_CenX,m_CenY,m_Scale;
double  m_LBD_CenX,m_LBD_CenY;
CPoint  m_LBD_pt;
bool    m_bLBD;

//鼠标操作,移动图像

void CView1::OnMouseMove(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    if (m_bLBD && (nFlags&MK_LBUTTON)==MK_LBUTTON)
    {
        int w = CAMERA_WIDTH;
        int h = CAMERA_HEIGHT;
        m_CenX = m_LBD_CenX - (point.x - m_LBD_pt.x)/m_Scale;
        m_CenY = m_LBD_CenY + (point.y - m_LBD_pt.y)/m_Scale;
        m_CenX = max(0.0,min(w-1.0,m_CenX));
        m_CenY = max(0.0,min(h-1.0,m_CenY));
        Invalidate(FALSE);  
    }
    CView::OnMouseMove(nFlags, point);
}

//鼠标操作,缩放图像

BOOL CViewLeft::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
    // TODO: Add your message handler code here and/or call default
    if (1)
    {
        if (zDelta > 0)
        {
            m_Scale *= 1.1892071150027210667174999705605;
        } 
        else
        {
            m_Scale /= 1.1892071150027210667174999705605;
        }
        Invalidate(FALSE);
    }
    return CView::OnMouseWheel(nFlags, zDelta, pt);
}

//鼠标左键操作

void CView1::OnLButtonDown(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    m_LBD_CenX = m_CenX;
    m_LBD_CenY = m_CenY;
    m_LBD_pt = point;
    m_bLBD = true;
    CView::OnLButtonDown(nFlags, point);
}


void CView1::OnLButtonUp(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    m_bLBD = false;
    CView::OnLButtonUp(nFlags, point);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值