Click here to Skip to main content
15,181,193 members
Articles / Desktop Programming / MFC
Posted 7 Jan 2003


46 bookmarked

Another ListView print (preview) sample.

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

Sample Image - ListPrintDemo.jpg


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); /= 52;
   return charSize;

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

double CListDemoViewPrint::GetTextRatioX(CDC* pDC)
   CDC* pCurrentDC = m_pListCtrl->GetDC();
   return ((double) / ((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)
   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);


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


About the Author

Andres Kaasik
Web Developer
Estonia Estonia
No Biography provided

Comments and Discussions

QuestionUsing this beautifull code with Ribbon bar Application Pin
Drakesal26-Nov-12 22:01
MemberDrakesal26-Nov-12 22:01 
QuestionHow can hanlde big string of List Item? Pin
Le@rner22-Jan-09 0:57
MemberLe@rner22-Jan-09 0:57 
Generallist view column summarizing Pin
ayeshika27-Jun-08 9:07
Memberayeshika27-Jun-08 9:07 
QuestionPortrait or Landscape? and pause? Pin
al.kaholik9-Feb-06 5:10
Memberal.kaholik9-Feb-06 5:10 
QuestionHow Can I use this in a MutiView Project? Pin
huaguo20-May-05 20:30
Memberhuaguo20-May-05 20:30 
GeneralMultiple Pages Wide Pin
Brad Bruce6-Apr-05 8:59
MemberBrad Bruce6-Apr-05 8:59 
Generalprint preview Pin
Member 18118342-Apr-05 3:52
MemberMember 18118342-Apr-05 3:52 
GeneralRe: print preview Pin
quike60413-Mar-09 12:44
Memberquike60413-Mar-09 12:44 
General"Print setup..." menu always not active Pin
Member 181183429-Mar-05 9:55
MemberMember 181183429-Mar-05 9:55 
GeneralDialog based applciation Pin
tuhin246-May-04 11:47
Membertuhin246-May-04 11:47 
GeneralRe: Dialog based applciation Pin
Karim Mribti2-Dec-04 4:37
MemberKarim Mribti2-Dec-04 4:37 
GeneralRe: Dialog based applciation Pin
quike60413-Mar-09 12:51
Memberquike60413-Mar-09 12:51 
GeneralBug if I want to Print in Courier New instead of Times New Roman Pin
mroy_10016-Feb-04 6:33
Membermroy_10016-Feb-04 6:33 
Generaltext alignment Pin
bonarbo3-Nov-03 18:37
Memberbonarbo3-Nov-03 18:37 
GeneralLogos Pin
Susan Marshall24-Mar-03 5:39
MemberSusan Marshall24-Mar-03 5:39 
GeneralProblem with cero rows Pin
Boney6-Mar-03 4:18
MemberBoney6-Mar-03 4:18 
GeneralColumns not wide enough Pin
Chris Palmer15-Jan-03 6:43
MemberChris Palmer15-Jan-03 6:43 
General0 Width Columns Pin
shawkins9-Jan-03 13:32
Membershawkins9-Jan-03 13:32 
GeneralRe: 0 Width Columns Pin
Andres Kaasik9-Jan-03 23:39
MemberAndres Kaasik9-Jan-03 23:39 
GeneralRe: 0 Width Columns Pin
shawkins10-Jan-03 7:40
Membershawkins10-Jan-03 7:40 
GeneralA comment Pin
Marc Clifton8-Jan-03 2:34
mvaMarc Clifton8-Jan-03 2: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.