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.
bool CPrintListCtrl::PrintData(CListCtrl* pListCtrl, CString szPageTitle)
{
if(pListCtrl->GetItemCount() <= 0)
return false;
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;
aPrintDlg->DoModal();
m_HdcPrint = aPrintDlg->GetPrinterDC();
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) ;
PrintListCtrlHeading(pListCtrl, szPageTitle);
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...