Click here to Skip to main content
Licence CPOL
First Posted 7 Jan 2003
Views 101,061
Downloads 701
Bookmarked 43 times

Another ListView print (preview) sample.

By | 7 Jan 2003 | Article
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)

About the Author

Andres Kaasik

Web Developer

Estonia Estonia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow can hanlde big string of List Item? Pinmember"_$h@nky_"23:57 21 Jan '09  
Generallist view column summarizing Pinmemberayeshika8:07 27 Jun '08  
QuestionPortrait or Landscape? and pause? PinmemberAm0k4:10 9 Feb '06  
QuestionHow Can I use this in a MutiView Project? Pinmemberhuaguo19:30 20 May '05  
GeneralMultiple Pages Wide PinmemberBrad Bruce7:59 6 Apr '05  
Generalprint preview Pinmembertim7897982:52 2 Apr '05  
GeneralRe: print preview Pinmemberquike604111:44 3 Mar '09  
General"Print setup..." menu always not active PinmemberTim406968:55 29 Mar '05  
GeneralDialog based applciation Pinmembertuhin2410:47 6 May '04  
GeneralRe: Dialog based applciation PinmemberKarim Mribti3:37 2 Dec '04  
GeneralRe: Dialog based applciation Pinmemberquike604111:51 3 Mar '09  
GeneralBug if I want to Print in Courier New instead of Times New Roman Pinmembermroy_1005:33 16 Feb '04  
Generaltext alignment Pinmemberbonarbo17:37 3 Nov '03  
GeneralLogos PinmemberSusan Marshall4:39 24 Mar '03  
GeneralProblem with cero rows PinmemberBoney3:18 6 Mar '03  
GeneralColumns not wide enough PinmemberChris Palmer5:43 15 Jan '03  
General0 Width Columns Pinmembershawkins12:32 9 Jan '03  
GeneralRe: 0 Width Columns PinmemberAndres Kaasik22:39 9 Jan '03  
GeneralRe: 0 Width Columns Pinmembershawkins6:40 10 Jan '03  
GeneralA comment PinmemberMarc Clifton1:34 8 Jan '03  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 8 Jan 2003
Article Copyright 2003 by Andres Kaasik
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid