PrintPreviewListBox.cpp
上传用户:hicheon
上传日期:2007-02-06
资源大小:34k
文件大小:25k
源码类别:
ListView/ListBox
开发平台:
Visual C++
- // PrintPreviewListBox.cpp : implementation file
- //
- #include "stdafx.h"
- #include "PrintPreviewListBox.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define PRINTMARGIN 2
- /////////////////////////////////////////////////////////////////////////////
- // CPrintPreviewListBox
- IMPLEMENT_DYNCREATE(CPrintPreviewListBox, CListBox)
- /*****************************************************************************/
- /* */
- /* .CLASSNAME: [CPrintPreviewListBox ] */
- /* */
- /* .MEMBERFUNCTION/-OPERATION [CPrintPreviewListBox ] */
- /* */
- /* .DESCRIPTION: */
- /* ....... */
- /* */
- /* .REPORT */
- /* dd.mm.yy Goran description */
- /* ........ ......... ................................................... */
- /* */
- /*****************************************************************************/
- CPrintPreviewListBox::CPrintPreviewListBox()
- {
- m_bPrintOnlySelectedItems = false;
- }
- /*****************************************************************************/
- /*****************************************************************************/
- /* */
- /* .CLASSNAME: [CPrintPreviewListBox ] */
- /* */
- /* .MEMBERFUNCTION/-OPERATION [~CPrintPreviewListBox ] */
- /* */
- /* .DESCRIPTION: */
- /* ....... */
- /* */
- /* .REPORT */
- /* dd.mm.yy Goran description */
- /* ........ ......... ................................................... */
- /* */
- /*****************************************************************************/
- CPrintPreviewListBox::~CPrintPreviewListBox()
- {
- }
- /*****************************************************************************/
- BEGIN_MESSAGE_MAP(CPrintPreviewListBox, CListBox)
- //{{AFX_MSG_MAP(CPrintPreviewListBox)
- ON_WM_CREATE()
- ON_WM_LBUTTONDOWN()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CPrintPreviewListBox message handlers
- /*****************************************************************************/
- /* */
- /* .CLASSNAME: [CPrintPreviewListBox ] */
- /* */
- /* .MEMBERFUNCTION/-OPERATION [GetPrintTitle ] */
- /* */
- /* .DESCRIPTION: */
- /* ....... */
- /* */
- /* .REPORT */
- /* dd.mm.yy Goran description */
- /* ........ ......... ................................................... */
- /* */
- /*****************************************************************************/
- CString CPrintPreviewListBox::GetPrintTitle()
- {
- return m_strTitle;
- }
- /*****************************************************************************/
- /*****************************************************************************/
- /* */
- /* .CLASSNAME: [CPrintPreviewListBox ] */
- /* */
- /* .MEMBERFUNCTION/-OPERATION [OnBeginPrinting ] */
- /* */
- /* .DESCRIPTION: */
- /* ....... */
- /* */
- /* .REPORT */
- /* dd.mm.yy Goran description */
- /* ........ ......... ................................................... */
- /* */
- /*****************************************************************************/
- void CPrintPreviewListBox::OnBeginPrinting (CDC* pDC, CPrintInfo* pInfo)
- {
- int nHeight = -((pDC->GetDeviceCaps (LOGPIXELSY) * 10) / 72);
- m_printerFont.CreateFont (nHeight, 0, 0, 0, FW_NORMAL, 0, 0, 0,
- DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
- DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Courier New");
- TEXTMETRIC tm;
- CFont* pOldFont = pDC->SelectObject (&m_printerFont);
- pDC->GetTextMetrics (&tm);
- m_cyPrinter = tm.tmHeight + tm.tmExternalLeading;
- CSize size = pDC->GetTextExtent ("---------1---------2---------"
- "3---------4---------5---------6---------7---------8-", 81);
- pDC->SelectObject (pOldFont);
- m_nLinesPerPage = (pDC->GetDeviceCaps (VERTRES) - (m_cyPrinter * (3 + (2 * PRINTMARGIN)))) / m_cyPrinter;
- m_nLinesTotal = SetLinesTotal(); // Setting up total lines for printing
- UINT nMaxPage = max (1, (m_nLinesTotal + (m_nLinesPerPage - 1)) / m_nLinesPerPage);
- pInfo->SetMaxPage (nMaxPage);
- m_cxOffset = (pDC->GetDeviceCaps (HORZRES) - size.cx) / 2;
- m_cxWidth = size.cx;
- }
- /*****************************************************************************/
- /*****************************************************************************/
- /* */
- /* .CLASSNAME: [CPrintPreviewListBox ] */
- /* */
- /* .MEMBERFUNCTION/-OPERATION [OnEndPrinting ] */
- /* */
- /* .DESCRIPTION: */
- /* ....... */
- /* */
- /* .REPORT */
- /* dd.mm.yy Goran description */
- /* ........ ......... ................................................... */
- /* */
- /*****************************************************************************/
- void CPrintPreviewListBox::OnEndPrinting (CDC* pDC, CPrintInfo* pInfo)
- {
- m_printerFont.DeleteObject ();
- }
- /*****************************************************************************/
- /*****************************************************************************/
- /* */
- /* .CLASSNAME: [CPrintPreviewListBox ] */
- /* */
- /* .MEMBERFUNCTION/-OPERATION [OnPrint ] */
- /* */
- /* .DESCRIPTION: */
- /* ....... */
- /* */
- /* .REPORT */
- /* dd.mm.yy Goran description */
- /* ........ ......... ................................................... */
- /* */
- /*****************************************************************************/
- void CPrintPreviewListBox::OnPrint (CDC* pDC, CPrintInfo* pInfo)
- {
- PrintPageHeader (pDC, pInfo->m_nCurPage);
- PrintPage (pDC, pInfo->m_nCurPage);
- }
- /*****************************************************************************/
- /*****************************************************************************/
- /* */
- /* .CLASSNAME: [CPrintPreviewListBox ] */
- /* */
- /* .MEMBERFUNCTION/-OPERATION [Print ] */
- /* */
- /* .DESCRIPTION: */
- /* ....... */
- /* */
- /* .REPORT */
- /* dd.mm.yy Goran description */
- /* ........ ......... ................................................... */
- /* */
- /*****************************************************************************/
- void CPrintPreviewListBox::Print()
- {
- CDC dc;
- CPrintDialog printDlg( FALSE );
- if( printDlg.DoModal() != IDOK ) // Get printer settings from user
- return;
- else
- {
- dc.Attach( printDlg.GetPrinterDC() ); // attach a printer DC
- dc.m_bPrinting = TRUE;
- CString strTitle;
- strTitle.LoadString( AFX_IDS_APP_TITLE );
- DOCINFO di; // Initialise print doc details
- memset( &di, 0, sizeof(DOCINFO) );
- di.cbSize = sizeof( DOCINFO );
- di.lpszDocName = strTitle;
- BOOL bPrintingOK = dc.StartDoc( &di ); // Begin a new print job
- CPrintInfo Info;
- Info.m_rectDraw.SetRect( 0, 0, dc.GetDeviceCaps(HORZRES), dc.GetDeviceCaps(VERTRES) );
- OnBeginPrinting( &dc, &Info ); // Initialise printing
- for( UINT page = Info.GetMinPage(); page <= Info.GetMaxPage() && bPrintingOK; page++ )
- {
- dc.StartPage(); // begin new page
- Info.m_nCurPage = page;
- OnPrint( &dc, &Info ); // Print page
- bPrintingOK = (dc.EndPage() > 0); // end page
- }
- OnEndPrinting( &dc, &Info ); // Clean up after printing
- if( bPrintingOK )
- dc.EndDoc(); // end a print job
- else
- dc.AbortDoc(); // abort job.
- dc.Detach(); // detach the printer DC
- }
- }
- /*****************************************************************************/
- /*****************************************************************************/
- /* */
- /* .CLASSNAME: [CPrintPreviewListBox ] */
- /* */
- /* .MEMBERFUNCTION/-OPERATION [PrintPage ] */
- /* */
- /* .DESCRIPTION: */
- /* ....... */
- /* */
- /* .REPORT */
- /* dd.mm.yy Goran description */
- /* ........ ......... ................................................... */
- /* */
- /*****************************************************************************/
- void CPrintPreviewListBox::PrintPage (CDC* pDC, UINT nPageNumber)
- {
- if( m_nLinesTotal != 0 )
- {
- UINT nStart = (nPageNumber - 1) * m_nLinesPerPage;
- UINT nEnd = min( m_nLinesTotal - 1, nStart + m_nLinesPerPage - 1 );
- CString string;
- CFont* pOldFont = pDC->SelectObject( &m_printerFont );
- UINT y;
- for( UINT i = nStart; i <= nEnd; i++ )
- {
- // BEGIN - Get items for printing
- string = m_strarFinalArray.GetAt( i );
- // END - Get items for printing
- y = ((i - nStart) + PRINTMARGIN + 3) * m_cyPrinter;
- pDC->TextOut( m_cxOffset, y, string );
- }
- pDC->SelectObject( pOldFont );
- }
- }
- /*****************************************************************************/
- /*****************************************************************************/
- /* */
- /* .CLASSNAME: [CPrintPreviewListBox ] */
- /* */
- /* .MEMBERFUNCTION/-OPERATION [PrintPageHeader ] */
- /* */
- /* .DESCRIPTION: */
- /* ....... */
- /* */
- /* .REPORT */
- /* dd.mm.yy Goran description */
- /* ........ ......... ................................................... */
- /* */
- /*****************************************************************************/
- void CPrintPreviewListBox::PrintPageHeader (CDC* pDC, UINT nPageNumber)
- {
- CString strHeader;
- strHeader = m_strTitle;
- CString strPageNumber;
- strPageNumber.Format( "Page %d", nPageNumber );
- UINT nSpaces = 81 - strPageNumber.GetLength() - strHeader.GetLength();
- for( UINT i = 0; i < nSpaces; i++ )
- strHeader += ' ';
- strHeader += strPageNumber;
- UINT y = m_cyPrinter * PRINTMARGIN;
- CFont* pOldFont = pDC->SelectObject( &m_printerFont );
- pDC->TextOut( m_cxOffset, y, strHeader );
- y += (m_cyPrinter * 3) / 2;
- pDC->MoveTo( m_cxOffset, y );
- pDC->LineTo( m_cxOffset + m_cxWidth, y );
- pDC->SelectObject( pOldFont );
- }
- /*****************************************************************************/
- /*****************************************************************************/
- /* */
- /* .CLASSNAME: [CPrintPreviewListBox ] */
- /* */
- /* .MEMBERFUNCTION/-OPERATION [SetLinesTotal ] */
- /* */
- /* .DESCRIPTION: */
- /* ....... */
- /* */
- /* .REPORT */
- /* dd.mm.yy Goran description */
- /* ........ ......... ................................................... */
- /* */
- /*****************************************************************************/
- int CPrintPreviewListBox::SetLinesTotal()
- {
- int nCount;
- int nRetVal = 0;
- aryListBoxSel.RemoveAll();
- m_strarFinalArray.RemoveAll();
- if( !m_bPrintOnlySelectedItems ) // Print out entire content of listbox
- {
- nCount = this->GetCount();
- for( int i = 0; i < nCount; i++ )
- {
- CString sListBoxItem;
- this->GetText( i, sListBoxItem );
- LineBreaking( sListBoxItem );
- }
- }
- else // Print out only selected items in listbox
- {
- // Get the indexes of all the selected items.
- if( m_bIsMultiselection )
- {
- nCount = this->GetSelCount();
- aryListBoxSel.SetSize( nCount );
- this->GetSelItems( nCount, aryListBoxSel.GetData() );
- }
- else
- {
- int nCurrentSelection = this->GetCurSel();
- aryListBoxSel.Add( nCurrentSelection );
- }
- for( int i = 0; i < aryListBoxSel.GetSize(); i++ )
- {
- CString sSelectedItem;
- int nSelectedItem = aryListBoxSel.GetAt( i );
- this->GetText( nSelectedItem, sSelectedItem );
- LineBreaking( sSelectedItem );
- }
- }
- return m_strarFinalArray.GetSize();
- }
- /*****************************************************************************/
- /*****************************************************************************/
- /* */
- /* .CLASSNAME: [CPrintPreviewListBox ] */
- /* */
- /* .MEMBERFUNCTION/-OPERATION [SetPrintTitle ] */
- /* */
- /* .DESCRIPTION: */
- /* ....... */
- /* */
- /* .REPORT */
- /* dd.mm.yy Goran description */
- /* ........ ......... ................................................... */
- /* */
- /*****************************************************************************/
- void CPrintPreviewListBox::SetPrintTitle( CString sNewTitle )
- {
- m_strTitle = sNewTitle;
- }
- /*****************************************************************************/
- /*****************************************************************************/
- /* */
- /* .CLASSNAME: [CPrintPreviewListBox ] */
- /* */
- /* .MEMBERFUNCTION/-OPERATION [LineBreaking ] */
- /* */
- /* .DESCRIPTION: */
- /* ....... */
- /* */
- /* .REPORT */
- /* dd.mm.yy Goran description */
- /* ........ ......... ................................................... */
- /* */
- /*****************************************************************************/
- void CPrintPreviewListBox::LineBreaking( CString sSelectedItem )
- {
- int nLength = sSelectedItem.GetLength();
- if( nLength > 81 )
- {
- CString sLeft, sRight;
- div_t div_result;
- int nQuotient;
- div_result = div( nLength, 81 );
- nQuotient = div_result.quot;
- sLeft = sSelectedItem;
- while( nQuotient > 0 )
- {
- sRight = sLeft.Right( sLeft.GetLength() - 81 );
- sLeft = sLeft.Left( 81 );
- m_strarFinalArray.Add( sLeft );
- sLeft = sRight;
- if( sLeft.GetLength() <= 81 )
- m_strarFinalArray.Add( sLeft );
- nQuotient--;
- }
- }
- else
- m_strarFinalArray.Add( sSelectedItem );
- }
- /*****************************************************************************/
- /*****************************************************************************/
- /* */
- /* .CLASSNAME: [CPrintPreviewListBox ] */
- /* */
- /* .MEMBERFUNCTION/-OPERATION [SetOnlySelectedItems ] */
- /* */
- /* .DESCRIPTION: */
- /* ....... */
- /* */
- /* .REPORT */
- /* dd.mm.yy Goran description */
- /* ........ ......... ................................................... */
- /* */
- /*****************************************************************************/
- void CPrintPreviewListBox::SetOnlySelectedItems(bool bOnlySelectedItems)
- {
- m_bPrintOnlySelectedItems = bOnlySelectedItems;
- }
- /*****************************************************************************/
- /*****************************************************************************/
- /* */
- /* .CLASSNAME: [CPrintPreviewListBox ] */
- /* */
- /* .MEMBERFUNCTION/-OPERATION [GetOnlySelectedItems ] */
- /* */
- /* .DESCRIPTION: */
- /* ....... */
- /* */
- /* .REPORT */
- /* dd.mm.yy Goran description */
- /* ........ ......... ................................................... */
- /* */
- /*****************************************************************************/
- bool CPrintPreviewListBox::GetOnlySelectedItems()
- {
- return m_bPrintOnlySelectedItems;
- }
- /*****************************************************************************/
- /*****************************************************************************/
- /* */
- /* .CLASSNAME: [CPrintPreviewListBox ] */
- /* */
- /* .MEMBERFUNCTION/-OPERATION [OnCreate ] */
- /* */
- /* .DESCRIPTION: */
- /* ....... */
- /* */
- /* .REPORT */
- /* dd.mm.yy Goran description */
- /* ........ ......... ................................................... */
- /* */
- /*****************************************************************************/
- int CPrintPreviewListBox::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CListBox::OnCreate(lpCreateStruct) == -1)
- return -1;
- // TODO: Add your specialized creation code here
- if( GetStyle() & LBS_MULTIPLESEL ) // It is multiselection listbox
- m_bIsMultiselection = true;
- else // It is not multiselection listbox
- m_bIsMultiselection = false;
- return 0;
- }
- /*****************************************************************************/
English
