Click here to Skip to main content
14,664,939 members
Articles » Desktop Development » List Controls » ListView controls
Article
Posted 7 Jan 2003

Stats

132.5K views
1.7K downloads
46 bookmarked

Another ListView print (preview) sample.

Rate this:
4.67 (15 votes)
Please Sign up or sign in to vote.
4.67 (15 votes)
7 Jan 2003CPOL
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)

Share

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 21:01
MemberDrakesal26-Nov-12 21:01 
QuestionHow can hanlde big string of List Item? Pin
Le@rner21-Jan-09 23:57
MemberLe@rner21-Jan-09 23:57 
Generallist view column summarizing Pin
ayeshika27-Jun-08 8:07
Memberayeshika27-Jun-08 8:07 
QuestionPortrait or Landscape? and pause? Pin
al.kaholik9-Feb-06 4:10
Memberal.kaholik9-Feb-06 4:10 
QuestionHow Can I use this in a MutiView Project? Pin
huaguo20-May-05 19:30
Memberhuaguo20-May-05 19:30 
GeneralMultiple Pages Wide Pin
Brad Bruce6-Apr-05 7:59
MemberBrad Bruce6-Apr-05 7:59 
Generalprint preview Pin
Member 18118342-Apr-05 2:52
MemberMember 18118342-Apr-05 2:52 
GeneralRe: print preview Pin
quike60413-Mar-09 11:44
Memberquike60413-Mar-09 11:44 
General"Print setup..." menu always not active Pin
Member 181183429-Mar-05 8:55
MemberMember 181183429-Mar-05 8:55 
GeneralDialog based applciation Pin
tuhin246-May-04 10:47
Membertuhin246-May-04 10:47 
GeneralRe: Dialog based applciation Pin
Karim Mribti2-Dec-04 3:37
MemberKarim Mribti2-Dec-04 3:37 
GeneralRe: Dialog based applciation Pin
quike60413-Mar-09 11:51
Memberquike60413-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
Membermroy_10016-Feb-04 5:33 
Generaltext alignment Pin
bonarbo3-Nov-03 17:37
Memberbonarbo3-Nov-03 17:37 
GeneralLogos Pin
Susan Marshall24-Mar-03 4:39
MemberSusan Marshall24-Mar-03 4:39 
GeneralProblem with cero rows Pin
Boney6-Mar-03 3:18
MemberBoney6-Mar-03 3:18 
Hi

There is a problem if one tries to print a empty list.
I added the following code in "ListViewDemoPrint.cpp" line 393 and things works fine.

// Prepare document and print rows
for (i=nStartRow; i < nEndRow; i++)
{
if(i>=0)
{
DrawRow(pDC, i);
}
}

Thanks for a great class.

best regards
Bjørnar Skogseth
GeneralColumns not wide enough Pin
Chris Palmer15-Jan-03 5:43
MemberChris Palmer15-Jan-03 5:43 
General0 Width Columns Pin
shawkins9-Jan-03 12:32
Membershawkins9-Jan-03 12:32 
GeneralRe: 0 Width Columns Pin
Andres Kaasik9-Jan-03 22:39
MemberAndres Kaasik9-Jan-03 22:39 
GeneralRe: 0 Width Columns Pin
shawkins10-Jan-03 6:40
Membershawkins10-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.