65.9K
CodeProject 正在变化。 阅读更多。
Home

以网格形式跨多页打印 ListControl 数据

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.82/5 (15投票s)

2004年1月6日

CPOL
viewsIcon

64698

downloadIcon

1126

以网格形式在多页上打印 CListCtrl 的数据。

Sample Image - printlistctrl.jpg

引言

我希望以 Excel 格式打印列表控件的数据,但我无法找到任何类型的打印代码,所以我认为为什么不自己实现这样一个类呢,结果就是这里.....

我的类 CPrintListCtrl 完成了以 Excel 格式打印列表控件的任务。只需调用函数 PrintData(CListCtrl* pListCtrl, CString szPageTitle),它将完成所有工作。pListCtrlCListCtrl 指针,而 szPageTitle 是打印标题。在这个类中,我使用了 CPrintDialog 来完成所有工作。

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;
}

您可以自由更改打印标题颜色和列表控件标题颜色。玩得开心....

© . All rights reserved.