Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / MFC

Print ListControl data in the form of a grid on multiple pages

Rate me:
Please Sign up or sign in to vote.
2.82/5 (15 votes)
14 Jan 2005CPOL 63.4K   1.1K   23   8
Printing the data of a CListCtrl on multple pages in the form of a grid.

Sample Image - printlistctrl.jpg

Introduction

I wanted to print a list control data in Excel format, but I could not find code for that type of printing anywhere, and I thought why not implement such a class myself, and the result is here.....

My class CPrintListCtrl does the job of printing the list control in Excel format. Simply call the function PrintData(CListCtrl* pListCtrl, CString szPageTitle) which does all the job. pListCtrl is the CListCtrl pointer and szPageTitle is the tile of the print. In this class, I have used the CPrintDialog to do all the job.

C++
bool CPrintListCtrl::PrintData(CListCtrl* pListCtrl, CString szPageTitle)
{
 // Check if the data exists in the list control
 if(pListCtrl->GetItemCount() <= 0)
  return false;
 
 // create the printdialog class pointer with the desired properties 
 // and set the options to the objects 
 CPrintDialog *aPrintDlg = new CPrintDialog(FALSE, 
                               PD_ALLPAGES | PD_RETURNDC, NULL);
 aPrintDlg->m_pd.nMinPage = aPrintDlg->m_pd.nMaxPage = 1;
 aPrintDlg->m_pd.nFromPage = aPrintDlg->m_pd.nToPage = 1;
 // call the printer selection dialog
 aPrintDlg->DoModal();
 // get the HDC of the printer dialog
 m_HdcPrint = aPrintDlg->GetPrinterDC();

 // check for valid handle and proceed
 if(m_HdcPrint != NULL)
 {
  m_CurrentYPos = 130;
  m_Space = 5;
  m_GeneralFont.CreateFont(-((::GetDeviceCaps (m_HdcPrint, LOGPIXELSY)*12)/72),
       0,0,0,FW_NORMAL,FALSE,FALSE,FALSE,DEFAULT_CHARSET,
       OUT_CHARACTER_PRECIS,CLIP_CHARACTER_PRECIS,
       DEFAULT_QUALITY,DEFAULT_PITCH,_T("Arial"));
  m_HeadingFont.CreateFont(-((::GetDeviceCaps (m_HdcPrint, LOGPIXELSY)*18)/72),
     0,0,0,FW_NORMAL,FALSE,FALSE,FALSE,DEFAULT_CHARSET,
     OUT_CHARACTER_PRECIS,CLIP_CHARACTER_PRECIS,
     DEFAULT_QUALITY,DEFAULT_PITCH,_T("Arial"));
  m_ListFont.CreateFont(-((::GetDeviceCaps (m_HdcPrint, LOGPIXELSY)*10)/72),
    0,0,0,FW_NORMAL,FALSE,FALSE,FALSE,DEFAULT_CHARSET,
    OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
    DEFAULT_QUALITY,DEFAULT_PITCH,_T("Arial"));
  m_PrintDC = new CDC;
  m_PrintDC->Attach (m_HdcPrint);
  m_PrintDC->StartDoc(_T("Periodical List"));  
  m_PrintDC->StartPage();
  m_PageRect.left = 0;
  m_PageRect.top = 0;
  m_PageRect.right = ::GetDeviceCaps (m_HdcPrint, HORZRES) ;
  m_PageRect.bottom = ::GetDeviceCaps (m_HdcPrint, VERTRES) ;

  // Print the List Control heading  
  PrintListCtrlHeading(pListCtrl, szPageTitle);
  // Finally Print the data
  PrintListData(pListCtrl);

  m_PrintDC->EndPage();
  m_PrintDC->EndDoc();
  m_PrintDC->Detach();
  delete m_PrintDC;
  m_HeadingFont.Detach();
  m_GeneralFont.Detach();
  m_ListFont.Detach();
 }
 else
 {
  delete aPrintDlg;
  return false;
 }
 delete aPrintDlg;
 return true;
}

You are free to change the print title color and the list control heading color. Have fun...

License

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


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

Comments and Discussions

 
QuestionMemory leak fix Pin
Anders Gustafsson20-Jun-18 19:58
Anders Gustafsson20-Jun-18 19:58 
QuestionPrinting Icons from Imagelist Pin
rootdial10-Dec-09 23:00
rootdial10-Dec-09 23:00 
GeneralHi! Pin
cristitomi3-Apr-07 2:12
cristitomi3-Apr-07 2:12 
GeneralPrint Preview Pin
Kyeema19-Jan-06 0:08
Kyeema19-Jan-06 0:08 
GeneralNice Pin
wrax15-Apr-05 2:23
wrax15-Apr-05 2:23 
GeneralFormat needs fixing!! Pin
WREY7-Jan-04 19:52
WREY7-Jan-04 19:52 
GeneralRe: Format needs fixing!! Pin
sunil samineni7-Jan-04 20:03
sunil samineni7-Jan-04 20:03 
GeneralRe: Format needs fixing!! Pin
spud16-Jan-04 3:32
spud16-Jan-04 3:32 

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.