PrintPreviewListBox.cpp
上传用户:hicheon
上传日期:2007-02-06
资源大小:34k
文件大小:25k
源码类别:

ListView/ListBox

开发平台:

Visual C++

  1. // PrintPreviewListBox.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "PrintPreviewListBox.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif
  10. #define PRINTMARGIN 2
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CPrintPreviewListBox
  13. IMPLEMENT_DYNCREATE(CPrintPreviewListBox, CListBox)
  14. /*****************************************************************************/
  15. /*                                                                           */
  16. /* .CLASSNAME:                [CPrintPreviewListBox           ]              */
  17. /*                                                                           */
  18. /* .MEMBERFUNCTION/-OPERATION [CPrintPreviewListBox           ]              */
  19. /*                                                                           */
  20. /* .DESCRIPTION:                                                             */
  21. /*  .......                                                                  */
  22. /*                                                                           */
  23. /* .REPORT                                                                   */
  24. /*  dd.mm.yy  Goran      description                                         */
  25. /*  ........  .........  ................................................... */
  26. /*                                                                           */
  27. /*****************************************************************************/
  28. CPrintPreviewListBox::CPrintPreviewListBox()
  29. {
  30. m_bPrintOnlySelectedItems = false;
  31. }
  32. /*****************************************************************************/
  33. /*****************************************************************************/
  34. /*                                                                           */
  35. /* .CLASSNAME:                [CPrintPreviewListBox           ]              */
  36. /*                                                                           */
  37. /* .MEMBERFUNCTION/-OPERATION [~CPrintPreviewListBox          ]              */
  38. /*                                                                           */
  39. /* .DESCRIPTION:                                                             */
  40. /*  .......                                                                  */
  41. /*                                                                           */
  42. /* .REPORT                                                                   */
  43. /*  dd.mm.yy  Goran      description                                         */
  44. /*  ........  .........  ................................................... */
  45. /*                                                                           */
  46. /*****************************************************************************/
  47. CPrintPreviewListBox::~CPrintPreviewListBox()
  48. {
  49. }
  50. /*****************************************************************************/
  51. BEGIN_MESSAGE_MAP(CPrintPreviewListBox, CListBox)
  52. //{{AFX_MSG_MAP(CPrintPreviewListBox)
  53. ON_WM_CREATE()
  54. ON_WM_LBUTTONDOWN()
  55. //}}AFX_MSG_MAP
  56. END_MESSAGE_MAP()
  57. /////////////////////////////////////////////////////////////////////////////
  58. // CPrintPreviewListBox message handlers
  59. /*****************************************************************************/
  60. /*                                                                           */
  61. /* .CLASSNAME:                [CPrintPreviewListBox           ]              */
  62. /*                                                                           */
  63. /* .MEMBERFUNCTION/-OPERATION [GetPrintTitle                  ]              */
  64. /*                                                                           */
  65. /* .DESCRIPTION:                                                             */
  66. /*  .......                                                                  */
  67. /*                                                                           */
  68. /* .REPORT                                                                   */
  69. /*  dd.mm.yy  Goran      description                                         */
  70. /*  ........  .........  ................................................... */
  71. /*                                                                           */
  72. /*****************************************************************************/
  73. CString CPrintPreviewListBox::GetPrintTitle()
  74. {
  75. return m_strTitle;
  76. }
  77. /*****************************************************************************/
  78. /*****************************************************************************/
  79. /*                                                                           */
  80. /* .CLASSNAME:                [CPrintPreviewListBox           ]              */
  81. /*                                                                           */
  82. /* .MEMBERFUNCTION/-OPERATION [OnBeginPrinting                ]              */
  83. /*                                                                           */
  84. /* .DESCRIPTION:                                                             */
  85. /*  .......                                                                  */
  86. /*                                                                           */
  87. /* .REPORT                                                                   */
  88. /*  dd.mm.yy  Goran      description                                         */
  89. /*  ........  .........  ................................................... */
  90. /*                                                                           */
  91. /*****************************************************************************/
  92. void CPrintPreviewListBox::OnBeginPrinting (CDC* pDC, CPrintInfo* pInfo)
  93. {
  94. int nHeight = -((pDC->GetDeviceCaps (LOGPIXELSY) * 10) / 72);
  95. m_printerFont.CreateFont (nHeight, 0, 0, 0, FW_NORMAL, 0, 0, 0,
  96. DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
  97. DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Courier New");
  98. TEXTMETRIC tm;
  99. CFont* pOldFont = pDC->SelectObject (&m_printerFont);
  100. pDC->GetTextMetrics (&tm);
  101. m_cyPrinter = tm.tmHeight + tm.tmExternalLeading;
  102. CSize size = pDC->GetTextExtent ("---------1---------2---------" 
  103. "3---------4---------5---------6---------7---------8-", 81);
  104. pDC->SelectObject (pOldFont);
  105. m_nLinesPerPage = (pDC->GetDeviceCaps (VERTRES) - (m_cyPrinter * (3 + (2 * PRINTMARGIN)))) / m_cyPrinter;
  106. m_nLinesTotal = SetLinesTotal(); // Setting up total lines for printing
  107. UINT nMaxPage = max (1, (m_nLinesTotal + (m_nLinesPerPage - 1)) / m_nLinesPerPage);
  108. pInfo->SetMaxPage (nMaxPage);
  109. m_cxOffset = (pDC->GetDeviceCaps (HORZRES) - size.cx) / 2;
  110. m_cxWidth = size.cx;
  111. }
  112. /*****************************************************************************/
  113. /*****************************************************************************/
  114. /*                                                                           */
  115. /* .CLASSNAME:                [CPrintPreviewListBox           ]              */
  116. /*                                                                           */
  117. /* .MEMBERFUNCTION/-OPERATION [OnEndPrinting                  ]              */
  118. /*                                                                           */
  119. /* .DESCRIPTION:                                                             */
  120. /*  .......                                                                  */
  121. /*                                                                           */
  122. /* .REPORT                                                                   */
  123. /*  dd.mm.yy  Goran      description                                         */
  124. /*  ........  .........  ................................................... */
  125. /*                                                                           */
  126. /*****************************************************************************/
  127. void CPrintPreviewListBox::OnEndPrinting (CDC* pDC, CPrintInfo* pInfo)
  128. {
  129. m_printerFont.DeleteObject ();
  130. }
  131. /*****************************************************************************/
  132. /*****************************************************************************/
  133. /*                                                                           */
  134. /* .CLASSNAME:                [CPrintPreviewListBox           ]              */
  135. /*                                                                           */
  136. /* .MEMBERFUNCTION/-OPERATION [OnPrint                        ]              */
  137. /*                                                                           */
  138. /* .DESCRIPTION:                                                             */
  139. /*  .......                                                                  */
  140. /*                                                                           */
  141. /* .REPORT                                                                   */
  142. /*  dd.mm.yy  Goran      description                                         */
  143. /*  ........  .........  ................................................... */
  144. /*                                                                           */
  145. /*****************************************************************************/
  146. void CPrintPreviewListBox::OnPrint (CDC* pDC, CPrintInfo* pInfo)
  147. {
  148. PrintPageHeader (pDC, pInfo->m_nCurPage);
  149. PrintPage (pDC, pInfo->m_nCurPage);
  150. }
  151. /*****************************************************************************/
  152. /*****************************************************************************/
  153. /*                                                                           */
  154. /* .CLASSNAME:                [CPrintPreviewListBox           ]              */
  155. /*                                                                           */
  156. /* .MEMBERFUNCTION/-OPERATION [Print                          ]              */
  157. /*                                                                           */
  158. /* .DESCRIPTION:                                                             */
  159. /*  .......                                                                  */
  160. /*                                                                           */
  161. /* .REPORT                                                                   */
  162. /*  dd.mm.yy  Goran      description                                         */
  163. /*  ........  .........  ................................................... */
  164. /*                                                                           */
  165. /*****************************************************************************/
  166. void CPrintPreviewListBox::Print()
  167. {
  168. CDC dc;
  169. CPrintDialog printDlg( FALSE );
  170. if( printDlg.DoModal() != IDOK )             // Get printer settings from user
  171. return;
  172. else
  173. {
  174. dc.Attach( printDlg.GetPrinterDC() );         // attach a printer DC
  175. dc.m_bPrinting = TRUE;
  176. CString strTitle;
  177. strTitle.LoadString( AFX_IDS_APP_TITLE );
  178. DOCINFO di;                                 // Initialise print doc details
  179. memset( &di, 0, sizeof(DOCINFO) );
  180. di.cbSize = sizeof( DOCINFO );
  181. di.lpszDocName = strTitle;
  182. BOOL bPrintingOK = dc.StartDoc( &di );        // Begin a new print job
  183. CPrintInfo Info;
  184. Info.m_rectDraw.SetRect( 0, 0, dc.GetDeviceCaps(HORZRES), dc.GetDeviceCaps(VERTRES) );
  185. OnBeginPrinting( &dc, &Info );                // Initialise printing
  186. for( UINT page = Info.GetMinPage(); page <= Info.GetMaxPage() && bPrintingOK; page++ )
  187. {
  188. dc.StartPage();                         // begin new page
  189. Info.m_nCurPage = page;
  190. OnPrint( &dc, &Info );                    // Print page
  191. bPrintingOK = (dc.EndPage() > 0);       // end page
  192. }
  193. OnEndPrinting( &dc, &Info );                  // Clean up after printing
  194. if( bPrintingOK )
  195. dc.EndDoc();                            // end a print job
  196. else
  197. dc.AbortDoc();                          // abort job.
  198. dc.Detach();                                // detach the printer DC
  199. }
  200. }
  201. /*****************************************************************************/
  202. /*****************************************************************************/
  203. /*                                                                           */
  204. /* .CLASSNAME:                [CPrintPreviewListBox           ]              */
  205. /*                                                                           */
  206. /* .MEMBERFUNCTION/-OPERATION [PrintPage                      ]              */
  207. /*                                                                           */
  208. /* .DESCRIPTION:                                                             */
  209. /*  .......                                                                  */
  210. /*                                                                           */
  211. /* .REPORT                                                                   */
  212. /*  dd.mm.yy  Goran      description                                         */
  213. /*  ........  .........  ................................................... */
  214. /*                                                                           */
  215. /*****************************************************************************/
  216. void CPrintPreviewListBox::PrintPage (CDC* pDC, UINT nPageNumber)
  217. {
  218. if( m_nLinesTotal != 0 )
  219. {
  220. UINT nStart = (nPageNumber - 1) * m_nLinesPerPage;
  221. UINT nEnd = min( m_nLinesTotal - 1, nStart + m_nLinesPerPage - 1 );
  222. CString string;
  223. CFont* pOldFont = pDC->SelectObject( &m_printerFont );
  224. UINT y;
  225. for( UINT i = nStart; i <= nEnd; i++ )
  226. {
  227. // BEGIN - Get items for printing
  228. string = m_strarFinalArray.GetAt( i );
  229. // END - Get items for printing
  230. y = ((i - nStart) + PRINTMARGIN + 3) * m_cyPrinter;
  231. pDC->TextOut( m_cxOffset, y, string );
  232. }
  233. pDC->SelectObject( pOldFont );
  234. }
  235. }
  236. /*****************************************************************************/
  237. /*****************************************************************************/
  238. /*                                                                           */
  239. /* .CLASSNAME:                [CPrintPreviewListBox           ]              */
  240. /*                                                                           */
  241. /* .MEMBERFUNCTION/-OPERATION [PrintPageHeader                ]              */
  242. /*                                                                           */
  243. /* .DESCRIPTION:                                                             */
  244. /*  .......                                                                  */
  245. /*                                                                           */
  246. /* .REPORT                                                                   */
  247. /*  dd.mm.yy  Goran      description                                         */
  248. /*  ........  .........  ................................................... */
  249. /*                                                                           */
  250. /*****************************************************************************/
  251. void CPrintPreviewListBox::PrintPageHeader (CDC* pDC, UINT nPageNumber)
  252. {
  253. CString strHeader;
  254. strHeader = m_strTitle;
  255. CString strPageNumber;
  256. strPageNumber.Format( "Page %d", nPageNumber );
  257. UINT nSpaces = 81 - strPageNumber.GetLength() - strHeader.GetLength();
  258. for( UINT i = 0; i < nSpaces; i++ )
  259. strHeader += ' ';
  260. strHeader += strPageNumber;
  261. UINT y = m_cyPrinter * PRINTMARGIN;
  262. CFont* pOldFont = pDC->SelectObject( &m_printerFont );
  263. pDC->TextOut( m_cxOffset, y, strHeader );
  264. y += (m_cyPrinter * 3) / 2;
  265. pDC->MoveTo( m_cxOffset, y );
  266. pDC->LineTo( m_cxOffset + m_cxWidth, y );
  267. pDC->SelectObject( pOldFont );
  268. }
  269. /*****************************************************************************/
  270. /*****************************************************************************/
  271. /*                                                                           */
  272. /* .CLASSNAME:                [CPrintPreviewListBox           ]              */
  273. /*                                                                           */
  274. /* .MEMBERFUNCTION/-OPERATION [SetLinesTotal                  ]              */
  275. /*                                                                           */
  276. /* .DESCRIPTION:                                                             */
  277. /*  .......                                                                  */
  278. /*                                                                           */
  279. /* .REPORT                                                                   */
  280. /*  dd.mm.yy  Goran      description                                         */
  281. /*  ........  .........  ................................................... */
  282. /*                                                                           */
  283. /*****************************************************************************/
  284. int CPrintPreviewListBox::SetLinesTotal()
  285. {
  286. int nCount;
  287. int nRetVal = 0;
  288. aryListBoxSel.RemoveAll();
  289. m_strarFinalArray.RemoveAll();
  290. if( !m_bPrintOnlySelectedItems ) // Print out entire content of listbox
  291. {
  292. nCount = this->GetCount();
  293. for( int i = 0; i < nCount; i++ )
  294. {
  295. CString sListBoxItem;
  296. this->GetText( i, sListBoxItem );
  297. LineBreaking( sListBoxItem );
  298. }
  299. }
  300. else // Print out only selected items in listbox
  301. {
  302. // Get the indexes of all the selected items.
  303. if( m_bIsMultiselection )
  304. {
  305. nCount = this->GetSelCount();
  306. aryListBoxSel.SetSize( nCount );
  307. this->GetSelItems( nCount, aryListBoxSel.GetData() );
  308. }
  309. else
  310. {
  311. int nCurrentSelection = this->GetCurSel();
  312. aryListBoxSel.Add( nCurrentSelection );
  313. }
  314. for( int i = 0; i < aryListBoxSel.GetSize(); i++ )
  315. {
  316. CString sSelectedItem;
  317. int nSelectedItem = aryListBoxSel.GetAt( i );
  318. this->GetText( nSelectedItem, sSelectedItem );
  319. LineBreaking( sSelectedItem );
  320. }
  321. }
  322. return m_strarFinalArray.GetSize();
  323. }
  324. /*****************************************************************************/
  325. /*****************************************************************************/
  326. /*                                                                           */
  327. /* .CLASSNAME:                [CPrintPreviewListBox           ]              */
  328. /*                                                                           */
  329. /* .MEMBERFUNCTION/-OPERATION [SetPrintTitle                  ]              */
  330. /*                                                                           */
  331. /* .DESCRIPTION:                                                             */
  332. /*  .......                                                                  */
  333. /*                                                                           */
  334. /* .REPORT                                                                   */
  335. /*  dd.mm.yy  Goran      description                                         */
  336. /*  ........  .........  ................................................... */
  337. /*                                                                           */
  338. /*****************************************************************************/
  339. void CPrintPreviewListBox::SetPrintTitle( CString sNewTitle )
  340. {
  341. m_strTitle = sNewTitle;
  342. }
  343. /*****************************************************************************/
  344. /*****************************************************************************/
  345. /*                                                                           */
  346. /* .CLASSNAME:                [CPrintPreviewListBox           ]              */
  347. /*                                                                           */
  348. /* .MEMBERFUNCTION/-OPERATION [LineBreaking                   ]              */
  349. /*                                                                           */
  350. /* .DESCRIPTION:                                                             */
  351. /*  .......                                                                  */
  352. /*                                                                           */
  353. /* .REPORT                                                                   */
  354. /*  dd.mm.yy  Goran      description                                         */
  355. /*  ........  .........  ................................................... */
  356. /*                                                                           */
  357. /*****************************************************************************/
  358. void CPrintPreviewListBox::LineBreaking( CString sSelectedItem )
  359. {
  360. int nLength = sSelectedItem.GetLength();
  361. if( nLength > 81 )
  362. {
  363. CString sLeft, sRight;
  364. div_t div_result;
  365. int nQuotient;
  366. div_result = div( nLength, 81 );
  367. nQuotient = div_result.quot;
  368. sLeft = sSelectedItem;
  369. while( nQuotient > 0 )
  370. {
  371. sRight = sLeft.Right( sLeft.GetLength() - 81 );
  372. sLeft = sLeft.Left( 81 );
  373. m_strarFinalArray.Add( sLeft );
  374. sLeft = sRight;
  375. if( sLeft.GetLength() <= 81 )
  376. m_strarFinalArray.Add( sLeft );
  377. nQuotient--;
  378. }
  379. }
  380. else
  381. m_strarFinalArray.Add( sSelectedItem );
  382. }
  383. /*****************************************************************************/
  384. /*****************************************************************************/
  385. /*                                                                           */
  386. /* .CLASSNAME:                [CPrintPreviewListBox           ]              */
  387. /*                                                                           */
  388. /* .MEMBERFUNCTION/-OPERATION [SetOnlySelectedItems           ]              */
  389. /*                                                                           */
  390. /* .DESCRIPTION:                                                             */
  391. /*  .......                                                                  */
  392. /*                                                                           */
  393. /* .REPORT                                                                   */
  394. /*  dd.mm.yy  Goran      description                                         */
  395. /*  ........  .........  ................................................... */
  396. /*                                                                           */
  397. /*****************************************************************************/
  398. void CPrintPreviewListBox::SetOnlySelectedItems(bool bOnlySelectedItems)
  399. {
  400. m_bPrintOnlySelectedItems = bOnlySelectedItems;
  401. }
  402. /*****************************************************************************/
  403. /*****************************************************************************/
  404. /*                                                                           */
  405. /* .CLASSNAME:                [CPrintPreviewListBox           ]              */
  406. /*                                                                           */
  407. /* .MEMBERFUNCTION/-OPERATION [GetOnlySelectedItems           ]              */
  408. /*                                                                           */
  409. /* .DESCRIPTION:                                                             */
  410. /*  .......                                                                  */
  411. /*                                                                           */
  412. /* .REPORT                                                                   */
  413. /*  dd.mm.yy  Goran      description                                         */
  414. /*  ........  .........  ................................................... */
  415. /*                                                                           */
  416. /*****************************************************************************/
  417. bool CPrintPreviewListBox::GetOnlySelectedItems()
  418. {
  419. return m_bPrintOnlySelectedItems;
  420. }
  421. /*****************************************************************************/
  422. /*****************************************************************************/
  423. /*                                                                           */
  424. /* .CLASSNAME:                [CPrintPreviewListBox           ]              */
  425. /*                                                                           */
  426. /* .MEMBERFUNCTION/-OPERATION [OnCreate                       ]              */
  427. /*                                                                           */
  428. /* .DESCRIPTION:                                                             */
  429. /*  .......                                                                  */
  430. /*                                                                           */
  431. /* .REPORT                                                                   */
  432. /*  dd.mm.yy  Goran      description                                         */
  433. /*  ........  .........  ................................................... */
  434. /*                                                                           */
  435. /*****************************************************************************/
  436. int CPrintPreviewListBox::OnCreate(LPCREATESTRUCT lpCreateStruct)
  437. {
  438. if (CListBox::OnCreate(lpCreateStruct) == -1)
  439. return -1;
  440. // TODO: Add your specialized creation code here
  441. if( GetStyle() & LBS_MULTIPLESEL ) // It is multiselection listbox
  442. m_bIsMultiselection = true;
  443. else // It is not multiselection listbox
  444. m_bIsMultiselection = false;
  445. return 0;
  446. }
  447. /*****************************************************************************/