HexEdit/GridCtrl_src/GridCtrl.cpp
void CGridCtrl::OnBeginPrinting(CDC *pDC, CPrintInfo *pInfo)
{
    // OnBeginPrinting() is called after the user has committed to
    // printing by OK'ing the Print dialog, and after the framework
    // has created a CDC object for the printer or the preview view.

    // This is the right opportunity to set up the page range.
    // Given the CDC object, we can determine how many rows will
    // fit on a page, so we can in turn determine how many printed
    // pages represent the entire document.

    ASSERT(pDC && pInfo);
    if (!pDC || !pInfo) return;

    // Get a DC for the current window (will be a screen DC for print previewing)
    CDC *pCurrentDC = GetDC();        // will have dimensions of the client area
    if (!pCurrentDC) return;

    CSize PaperPixelsPerInch(pDC->GetDeviceCaps(LOGPIXELSX), pDC->GetDeviceCaps(LOGPIXELSY));
    CSize ScreenPixelsPerInch(pCurrentDC->GetDeviceCaps(LOGPIXELSX), pCurrentDC->GetDeviceCaps(LOGPIXELSY));

    // Create the printer font
    int nFontSize = -10;
    CString strFontName = "Arial";
    m_PrinterFont.CreateFont(nFontSize, 0,0,0, FW_NORMAL, 0,0,0, DEFAULT_CHARSET,
                             OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY,
                             DEFAULT_PITCH | FF_DONTCARE, strFontName);

    CFont *pOldFont = pDC->SelectObject(&m_PrinterFont);

    // Get the average character width (in GridCtrl units) and hence the margins
    m_CharSize = pDC->GetTextExtent(_T("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSATUVWXYZ"),52);
    m_CharSize.cx /= 52;
    int nMargins = (m_nLeftMargin+m_nRightMargin)*m_CharSize.cx;

    // Get the page sizes (physical and logical)
    m_PaperSize = CSize(pDC->GetDeviceCaps(HORZRES), pDC->GetDeviceCaps(VERTRES));

    if( m_bWysiwygPrinting)
    {
        m_LogicalPageSize.cx = ScreenPixelsPerInch.cx * m_PaperSize.cx / PaperPixelsPerInch.cx * 3 / 4;
        m_LogicalPageSize.cy = ScreenPixelsPerInch.cy * m_PaperSize.cy / PaperPixelsPerInch.cy * 3 / 4;
    }
    else
    {
        m_PaperSize = CSize(pDC->GetDeviceCaps(HORZRES), pDC->GetDeviceCaps(VERTRES));

        m_LogicalPageSize.cx = GetVirtualWidth()+nMargins;
#ifdef _WIN32_WCE
        m_LogicalPageSize.cy = (m_LogicalPageSize.cx * m_PaperSize.cy) / m_PaperSize.cx;
#else
        m_LogicalPageSize.cy = MulDiv(m_LogicalPageSize.cx, m_PaperSize.cy, m_PaperSize.cx);
#endif
    }

    m_nPageHeight = m_LogicalPageSize.cy - GetFixedRowHeight()
                       - (m_nHeaderHeight+m_nFooterHeight + 2*m_nGap)*m_CharSize.cy;

    // Get the number of pages. Assumes no row is bigger than the page size.
    int nTotalRowHeight = 0;
    m_nNumPages = 1;
    for (int row = GetFixedRowCount(); row < GetRowCount(); row++)
    {
        nTotalRowHeight += GetRowHeight(row);
        if (nTotalRowHeight > m_nPageHeight) {
            m_nNumPages++;
            nTotalRowHeight = GetRowHeight(row);
        }
    }

    // now, figure out how many additional pages must print out if rows ARE bigger
    //  than page size
    int iColumnOffset = 0;
    int i1;
    for( i1=0; i1 < GetFixedColumnCount(); i1++)
    {
        iColumnOffset += GetColumnWidth( i1);
    }
    m_nPageWidth = m_LogicalPageSize.cx - iColumnOffset
                    - nMargins;
    m_nPageMultiplier = 1;

    if( m_bWysiwygPrinting)
    {
        int iTotalRowWidth = 0;
        for( i1 = GetFixedColumnCount(); i1 < GetColumnCount(); i1++)
        {
            iTotalRowWidth += GetColumnWidth( i1);
            if( iTotalRowWidth > m_nPageWidth)
            {
                m_nPageMultiplier++;
                iTotalRowWidth = GetColumnWidth( i1);
            }
        }
        m_nNumPages *= m_nPageMultiplier;
    }

    // Set up the print info
    pInfo->SetMaxPage(m_nNumPages);
    pInfo->m_nCurPage = 1;                        // start printing at page# 1

    ReleaseDC(pCurrentDC);
    pDC->SelectObject(pOldFont);
}
// EFW - Various changes in the next few functions to support the
// new print margins and a few other adjustments.

Various changes in the next few functions to support the
new print margins and a few other adjustments.
Various changes in the next few functions to support the new print margins and a few other adjustments .
void CGridCtrl::OnBeginPrinting(CDC *pDC, CPrintInfo *pInfo)
{
    ASSERT(pDC && pInfo);
    if (!pDC || !pInfo) return;
    CDC *pCurrentDC = GetDC();        
    if (!pCurrentDC) return;
    CSize PaperPixelsPerInch(pDC->GetDeviceCaps(LOGPIXELSX), pDC->GetDeviceCaps(LOGPIXELSY));
    CSize ScreenPixelsPerInch(pCurrentDC->GetDeviceCaps(LOGPIXELSX), pCurrentDC->GetDeviceCaps(LOGPIXELSY));
    int nFontSize = -10;
    CString strFontName = "Arial";
    m_PrinterFont.CreateFont(nFontSize, 0,0,0, FW_NORMAL, 0,0,0, DEFAULT_CHARSET,
                             OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY,
                             DEFAULT_PITCH | FF_DONTCARE, strFontName);
    CFont *pOldFont = pDC->SelectObject(&m_PrinterFont);
    m_CharSize = pDC->GetTextExtent(_T("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSATUVWXYZ"),52);
    m_CharSize.cx /= 52;
    int nMargins = (m_nLeftMargin+m_nRightMargin)*m_CharSize.cx;
    m_PaperSize = CSize(pDC->GetDeviceCaps(HORZRES), pDC->GetDeviceCaps(VERTRES));
    if( m_bWysiwygPrinting)
    {
        m_LogicalPageSize.cx = ScreenPixelsPerInch.cx * m_PaperSize.cx / PaperPixelsPerInch.cx * 3 / 4;
        m_LogicalPageSize.cy = ScreenPixelsPerInch.cy * m_PaperSize.cy / PaperPixelsPerInch.cy * 3 / 4;
    }
    else
    {
        m_PaperSize = CSize(pDC->GetDeviceCaps(HORZRES), pDC->GetDeviceCaps(VERTRES));
        m_LogicalPageSize.cx = GetVirtualWidth()+nMargins;
#ifdef _WIN32_WCE
        m_LogicalPageSize.cy = (m_LogicalPageSize.cx * m_PaperSize.cy) / m_PaperSize.cx;
#else
        m_LogicalPageSize.cy = MulDiv(m_LogicalPageSize.cx, m_PaperSize.cy, m_PaperSize.cx);
#endif
    }
    m_nPageHeight = m_LogicalPageSize.cy - GetFixedRowHeight()
                       - (m_nHeaderHeight+m_nFooterHeight + 2*m_nGap)*m_CharSize.cy;
    int nTotalRowHeight = 0;
    m_nNumPages = 1;
    for (int row = GetFixedRowCount(); row < GetRowCount(); row++)
    {
        nTotalRowHeight += GetRowHeight(row);
        if (nTotalRowHeight > m_nPageHeight) {
            m_nNumPages++;
            nTotalRowHeight = GetRowHeight(row);
        }
    }
    int iColumnOffset = 0;
    int i1;
    for( i1=0; i1 < GetFixedColumnCount(); i1++)
    {
        iColumnOffset += GetColumnWidth( i1);
    }
    m_nPageWidth = m_LogicalPageSize.cx - iColumnOffset
                    - nMargins;
    m_nPageMultiplier = 1;
    if( m_bWysiwygPrinting)
    {
        int iTotalRowWidth = 0;
        for( i1 = GetFixedColumnCount(); i1 < GetColumnCount(); i1++)
        {
            iTotalRowWidth += GetColumnWidth( i1);
            if( iTotalRowWidth > m_nPageWidth)
            {
                m_nPageMultiplier++;
                iTotalRowWidth = GetColumnWidth( i1);
            }
        }
        m_nNumPages *= m_nPageMultiplier;
    }
    pInfo->SetMaxPage(m_nNumPages);
    pInfo->m_nCurPage = 1;                        
    ReleaseDC(pCurrentDC);
    pDC->SelectObject(pOldFont);
}
void CGridCtrl :: OnBeginPrinting ( CDC * pDC , CPrintInfo * pInfo ) { ASSERT ( pDC && pInfo ) ; if ( ! pDC || ! pInfo ) return ; CDC * pCurrentDC = GetDC ( ) ; if ( ! pCurrentDC ) return ; CSize PaperPixelsPerInch ( pDC -> GetDeviceCaps ( LOGPIXELSX ) , pDC -> GetDeviceCaps ( LOGPIXELSY ) ) ; CSize ScreenPixelsPerInch ( pCurrentDC -> GetDeviceCaps ( LOGPIXELSX ) , pCurrentDC -> GetDeviceCaps ( LOGPIXELSY ) ) ; int nFontSize = -10 ; CString strFontName = " " ; m_PrinterFont . CreateFont ( nFontSize , 0 , 0 , 0 , FW_NORMAL , 0 , 0 , 0 , DEFAULT_CHARSET , OUT_CHARACTER_PRECIS , CLIP_CHARACTER_PRECIS , DEFAULT_QUALITY , DEFAULT_PITCH | FF_DONTCARE , strFontName ) ; CFont * pOldFont = pDC -> SelectObject ( & m_PrinterFont ) ; m_CharSize = pDC -> GetTextExtent ( _T ( " " ) , 52 ) ; m_CharSize . cx /= 52 ; int nMargins = ( m_nLeftMargin + m_nRightMargin ) * m_CharSize . cx ; m_PaperSize = CSize ( pDC -> GetDeviceCaps ( HORZRES ) , pDC -> GetDeviceCaps ( VERTRES ) ) ; if ( m_bWysiwygPrinting ) { m_LogicalPageSize . cx = ScreenPixelsPerInch . cx * m_PaperSize . cx / PaperPixelsPerInch . cx * 3 / 4 ; m_LogicalPageSize . cy = ScreenPixelsPerInch . cy * m_PaperSize . cy / PaperPixelsPerInch . cy * 3 / 4 ; } else { m_PaperSize = CSize ( pDC -> GetDeviceCaps ( HORZRES ) , pDC -> GetDeviceCaps ( VERTRES ) ) ; m_LogicalPageSize . cx = GetVirtualWidth ( ) + nMargins ; #ifdef _WIN32_WCE m_LogicalPageSize . cy = ( m_LogicalPageSize . cx * m_PaperSize . cy ) / m_PaperSize . cx ; #else m_LogicalPageSize . cy = MulDiv ( m_LogicalPageSize . cx , m_PaperSize . cy , m_PaperSize . cx ) ; #endif } m_nPageHeight = m_LogicalPageSize . cy - GetFixedRowHeight ( ) - ( m_nHeaderHeight + m_nFooterHeight + 2 * m_nGap ) * m_CharSize . cy ; int nTotalRowHeight = 0 ; m_nNumPages = 1 ; for ( int row = GetFixedRowCount ( ) ; row < GetRowCount ( ) ; row ++ ) { nTotalRowHeight += GetRowHeight ( row ) ; if ( nTotalRowHeight > m_nPageHeight ) { m_nNumPages ++ ; nTotalRowHeight = GetRowHeight ( row ) ; } } int iColumnOffset = 0 ; int i1 ; for ( i1 = 0 ; i1 < GetFixedColumnCount ( ) ; i1 ++ ) { iColumnOffset += GetColumnWidth ( i1 ) ; } m_nPageWidth = m_LogicalPageSize . cx - iColumnOffset - nMargins ; m_nPageMultiplier = 1 ; if ( m_bWysiwygPrinting ) { int iTotalRowWidth = 0 ; for ( i1 = GetFixedColumnCount ( ) ; i1 < GetColumnCount ( ) ; i1 ++ ) { iTotalRowWidth += GetColumnWidth ( i1 ) ; if ( iTotalRowWidth > m_nPageWidth ) { m_nPageMultiplier ++ ; iTotalRowWidth = GetColumnWidth ( i1 ) ; } } m_nNumPages *= m_nPageMultiplier ; } pInfo -> SetMaxPage ( m_nNumPages ) ; pInfo -> m_nCurPage = 1 ; ReleaseDC ( pCurrentDC ) ; pDC -> SelectObject ( pOldFont ) ; }
EFW - Various changes in the next few functions to support the
new print margins and a few other adjustments.
EFW - Various changes in the next few functions to support the new print margins and a few other adjustments .
// OnBeginPrinting() is called after the user has committed to
 // printing by OK'ing the Print dialog, and after the framework
 // has created a CDC object for the printer or the preview view.
 // This is the right opportunity to set up the page range.
 // Given the CDC object, we can determine how many rows will
 // fit on a page, so we can in turn determine how many printed
 // pages represent the entire document.
 // Get a DC for the current window (will be a screen DC for print previewing)
 // will have dimensions of the client area
 // Create the printer font
 // Get the average character width (in GridCtrl units) and hence the margins
 // Get the page sizes (physical and logical)
 // Get the number of pages. Assumes no row is bigger than the page size.
 // now, figure out how many additional pages must print out if rows ARE bigger
 //  than page size
 // Set up the print info
 // start printing at page# 1
