1. 程式人生 > >分割視窗後,CView 中顯示影像

分割視窗後,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);
}