OpenGL Programming for Windows MFC


11. Copy to printer DC


11.1 OnPrint をオーバライドし以下の行追加

void CXxxView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{

    CRect rect ;
    int w0,h0 ;
    int w1,h1 ;
    int dpix,dpiy ;
    double inchx,inchy ;
    
    GetClientRect(&rect) ;

    w0 = rect.Width() ;
    h0 = rect.Height() ;

    w1 = pDC->GetDeviceCaps(HORZRES) ;
    h1 = pDC->GetDeviceCaps(VERTRES) ;

    dpix = pDC->GetDeviceCaps(LOGPIXELSX) ;
    dpiy = pDC->GetDeviceCaps(LOGPIXELSY) ;

    inchx = (double)w1 / (double)dpix ;
    inchy = (double)h1 / (double)dpiy ;
    
    CDC* MemDC = new CDC ;
    MemDC->CreateCompatibleDC(m_pDC) ;

    CBitmap *bitmap = new CBitmap ;

    bitmap->CreateCompatibleBitmap(m_pDC,w0,h0) ;

    CBitmap* pOldBitmap = (CBitmap*)MemDC->SelectObject(bitmap) ;

    RenderingToBitmap(MemDC->m_hDC, w0, h0) ;

    {
        int wd,hd ;
        double ratio ;
        double inch_ratio ;
        double inch_x,inch_y ;
        DOCINFO dinf ;

        dinf.cbSize = sizeof(DOCINFO);
        dinf.lpszDocName=  "xxx";
        dinf.lpszOutput =  NULL;
        dinf.lpszDatatype = NULL ;
        dinf.fwType = 0 ;

        ratio = (double)w0 / (double) h0 ;

        inch_ratio = inchx / inchy ;

        if (ratio < inch_ratio) {
            inch_y = inchy ;
            inch_x = inchy * ratio ;
        }
        else {
            inch_x = inchx ;
            inch_y = inchx / ratio ;
        }

        wd = (int)(inch_x * dpix) ;
        hd = (int)(inch_y * dpiy) ;
		
        StartDoc(pDC->m_hDC, &dinf) ;
        StartPage(pDC->m_hDC) ;
        
        pDC->StretchBlt(0, 0, wd, hd, MemDC, 0, 0, w0, h0, SRCCOPY) ;

        EndPage(pDC->m_hDC) ;
        EndDoc(pDC->m_hDC) ;
    }

    MemDC->SelectObject(pOldBitmap) ;
    MemDC->DeleteDC() ;

    delete MemDC ;
    delete bitmap ;

    CView::OnPrint(pDC, pInfo);
}
Overrid OnPrint of CXXXView by Class Wizard.
Then, add the above line.

prev next