Click here to Skip to main content
15,881,812 members
Articles / Desktop Programming / MFC
Article

Another ListView print (preview) sample.

Rate me:
Please Sign up or sign in to vote.
4.67/5 (15 votes)
7 Jan 2003CPOL 140.2K   1.8K   46   21
Illustrates how MFC based List View content can be printed.

Sample Image - ListPrintDemo.jpg

Introduction

This article shows how to add print preview functionality to MFC CListView class in report mode, preserving column order and relative column with. Note that this implementation does NOT try to fit all list columns on one page.

How it works

The OnBeginPrinting function prepares printer fonts and calculates paper, page, header, footer and body rectangles.

void CListDemoViewPrint::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{   
   // Create fonts
   m_pFontHeader = CreateFont(pDC,_T("Arial"), 12, FW_BOLD);
   m_pFontFooter = CreateFont(pDC,_T("Arial"), 10);
   m_pFontColumn = CreateFont(pDC,_T("Arial"), 9, FW_BOLD);
   m_pFontBody   = CreateFont(pDC,_T("Times New Roman"), 10);

   // Calculate character size
   m_CharSizeHeader = GetCharSize(pDC, m_pFontHeader);
   m_CharSizeFooter = GetCharSize(pDC, m_pFontFooter);
   m_CharSizeBody   = GetCharSize(pDC, m_pFontBody);

   // Prepare layout 
   m_rectPaper  = GetPaperRect(pDC);
   m_rectPage   = GetPageRect();
   m_rectHeader = GetHeaderRect();
   ...
   m_RatioX = GetTextRatioX(pDC);   
   ...
}

Character sizes are used later to calculate header and footer vertical height, body character size is used to calculate row (cell) height, number of rows per page and horizontal X ratio to adjust column width.

Character size is calculated simply by selecting font and getting sample string extent as follows.

CSize CListDemoViewPrint::GetCharSize(CDC* pDC, CFont* pFont)
{
   CFont *pOldFont = pDC->SelectObject(pFont);
   CSize charSize = pDC->GetTextExtent(_T("abcdefghijkl
       mnopqrstuvwxyzABCDEFGHIJKLMNOPQRSATUVWXYZ"),52);
   charSize.cx /= 52;
   pDC->SelectObject(pOldFont);
   return charSize;
}

Horizontal text ratio is calculated using body font character size and list control average character size.

double CListDemoViewPrint::GetTextRatioX(CDC* pDC)
{
   ASSERT(pDC != NULL);
   ASSERT(m_pListCtrl);
   CDC* pCurrentDC = m_pListCtrl->GetDC();
   TEXTMETRIC tmSrc;
   pCurrentDC->GetTextMetrics(&tmSrc);
   m_pListCtrl->ReleaseDC(pCurrentDC);
   return ((double)m_CharSizeBody.cx) / ((double)tmSrc.tmAveCharWidth);
}

Using the code

Add the CListDemoViewPrint class member to your CListView derived class.

class CListDemoView : public CListView
{
  ...
  CListDemoViewPrint m_Print;
};

Add the following printing functions to your class.

BOOL CListDemoView::OnPreparePrinting(CPrintInfo* pInfo)
{
   m_Print.OnPreparePrinting(pInfo);
   return DoPreparePrinting(pInfo);
}

void CListDemoView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{   
   m_Print.OnBeginPrinting(pDC, pInfo);
}

void CListDemoView::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
{
   m_Print.OnPrint(pDC, pInfo);
}

void CListDemoView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
   m_Print.OnEndPrinting(pDC, pInfo);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Estonia Estonia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionUsing this beautifull code with Ribbon bar Application Pin
Drakesal26-Nov-12 21:01
Drakesal26-Nov-12 21:01 
QuestionHow can hanlde big string of List Item? Pin
Le@rner21-Jan-09 23:57
Le@rner21-Jan-09 23:57 
Generallist view column summarizing Pin
ayeshika27-Jun-08 8:07
ayeshika27-Jun-08 8:07 
QuestionPortrait or Landscape? and pause? Pin
al.kaholik9-Feb-06 4:10
al.kaholik9-Feb-06 4:10 
QuestionHow Can I use this in a MutiView Project? Pin
huaguo20-May-05 19:30
huaguo20-May-05 19:30 
GeneralMultiple Pages Wide Pin
Brad Bruce6-Apr-05 7:59
Brad Bruce6-Apr-05 7:59 
When there are more columns than fit on a page, is there any way to make this print the rest of the columns on another set of pages, instead of truncating them?

I started looking and have a few thoughts.

1. Calculate the columns that will fit on each page (across)
2. Multiply the number of pages down by the number of pages accross --> total # of pages
3. Offset the starting rect by the page position for each set of pages down (Page 0 starts at 0, Page 1 starts at 2000 etc)

Approach 1 (down then across)
Approach 2 (across then down)

Either way, I'll need to know the columns to print on each page. (Calculate starting row and starting column based on page number)

Any other suggestions?
Generalprint preview Pin
Member 18118342-Apr-05 2:52
Member 18118342-Apr-05 2:52 
GeneralRe: print preview Pin
quike60413-Mar-09 11:44
quike60413-Mar-09 11:44 
General"Print setup..." menu always not active Pin
Member 181183429-Mar-05 8:55
Member 181183429-Mar-05 8:55 
GeneralDialog based applciation Pin
tuhin246-May-04 10:47
tuhin246-May-04 10:47 
GeneralRe: Dialog based applciation Pin
Karim Mribti2-Dec-04 3:37
Karim Mribti2-Dec-04 3:37 
GeneralRe: Dialog based applciation Pin
quike60413-Mar-09 11:51
quike60413-Mar-09 11:51 
GeneralBug if I want to Print in Courier New instead of Times New Roman Pin
mroy_10016-Feb-04 5:33
mroy_10016-Feb-04 5:33 
Generaltext alignment Pin
bonarbo3-Nov-03 17:37
bonarbo3-Nov-03 17:37 
GeneralLogos Pin
Susan Marshall24-Mar-03 4:39
Susan Marshall24-Mar-03 4:39 
GeneralProblem with cero rows Pin
Boney6-Mar-03 3:18
Boney6-Mar-03 3:18 
GeneralColumns not wide enough Pin
Chris Palmer15-Jan-03 5:43
Chris Palmer15-Jan-03 5:43 
General0 Width Columns Pin
shawkins9-Jan-03 12:32
shawkins9-Jan-03 12:32 
GeneralRe: 0 Width Columns Pin
Andres Kaasik9-Jan-03 22:39
Andres Kaasik9-Jan-03 22:39 
GeneralRe: 0 Width Columns Pin
shawkins10-Jan-03 6:40
shawkins10-Jan-03 6:40 
GeneralA comment Pin
Marc Clifton8-Jan-03 1:34
mvaMarc Clifton8-Jan-03 1:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.