appprnt.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include "stdafx.h"
  11. #include <cderr.h> // Commdlg Error definitions
  12. #include <winspool.h>
  13. #ifdef AFX_PRINT_SEG
  14. #pragma code_seg(AFX_PRINT_SEG)
  15. #endif
  16. #ifdef _DEBUG
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. /////////////////////////////////////////////////////////////////////////////
  21. // WinApp support for printing
  22. BOOL CWinApp::GetPrinterDeviceDefaults(PRINTDLG* pPrintDlg)
  23. {
  24. UpdatePrinterSelection(m_hDevNames == NULL); //force default if no current
  25. if (m_hDevNames == NULL)
  26. return FALSE; // no printer defaults
  27. pPrintDlg->hDevNames = m_hDevNames;
  28. pPrintDlg->hDevMode = m_hDevMode;
  29. ::GlobalUnlock(m_hDevNames);
  30. ::GlobalUnlock(m_hDevMode);
  31. return TRUE;
  32. }
  33. void CWinApp::UpdatePrinterSelection(BOOL bForceDefaults)
  34. {
  35. if (!bForceDefaults && m_hDevNames != NULL)
  36. {
  37. LPDEVNAMES lpDevNames = (LPDEVNAMES)::GlobalLock(m_hDevNames);
  38. ASSERT(lpDevNames != NULL);
  39. if (lpDevNames->wDefault & DN_DEFAULTPRN)
  40. {
  41. CPrintDialog pd(TRUE);
  42. pd.GetDefaults();
  43. if (pd.m_pd.hDevNames == NULL)
  44. {
  45. // Printer was default, but now there are no printers at all!
  46. if (m_hDevMode != NULL)
  47. AfxGlobalFree(m_hDevMode);
  48. AfxGlobalFree(m_hDevNames);
  49. m_hDevMode = NULL;
  50. m_hDevNames = NULL;
  51. }
  52. else if (
  53. lstrcmp((LPCTSTR)lpDevNames + lpDevNames->wDriverOffset,
  54. pd.GetDriverName()) != 0 ||
  55. lstrcmp((LPCTSTR)lpDevNames + lpDevNames->wDeviceOffset,
  56. pd.GetDeviceName()) != 0 ||
  57. lstrcmp((LPCTSTR)lpDevNames + lpDevNames->wOutputOffset,
  58. pd.GetPortName()) != 0)
  59. {
  60. // Printer was default, and default has changed...assume default
  61. if (m_hDevMode != NULL)
  62. AfxGlobalFree(m_hDevMode);
  63. AfxGlobalFree(m_hDevNames);
  64. m_hDevMode = pd.m_pd.hDevMode;
  65. m_hDevNames = pd.m_pd.hDevNames;
  66. }
  67. else
  68. {
  69. // Printer was default, and still is...keep the same
  70. if (pd.m_pd.hDevMode != NULL)
  71. AfxGlobalFree(pd.m_pd.hDevMode);
  72. if (pd.m_pd.hDevNames != NULL)
  73. AfxGlobalFree(pd.m_pd.hDevNames);
  74. }
  75. }
  76. }
  77. else
  78. {
  79. // First time or Forced -- Get defaults
  80. CPrintDialog pd(TRUE);
  81. pd.GetDefaults();
  82. if (m_hDevMode != NULL)
  83. AfxGlobalFree(m_hDevMode);
  84. if (m_hDevNames != NULL)
  85. AfxGlobalFree(m_hDevNames);
  86. m_hDevMode = pd.m_pd.hDevMode;
  87. m_hDevNames = pd.m_pd.hDevNames;
  88. }
  89. }
  90. int CWinApp::DoPrintDialog(CPrintDialog* pPD)
  91. {
  92. UpdatePrinterSelection(FALSE);
  93. pPD->m_pd.hDevMode = m_hDevMode;
  94. pPD->m_pd.hDevNames = m_hDevNames;
  95. int nResponse = pPD->DoModal();
  96. // if OK or Cancel is selected we need to update cached devMode/Names
  97. while (nResponse != IDOK && nResponse != IDCANCEL)
  98. {
  99. switch (::CommDlgExtendedError())
  100. {
  101. // CommDlg cannot give these errors after NULLing these handles
  102. case PDERR_PRINTERNOTFOUND:
  103. case PDERR_DNDMMISMATCH:
  104. case PDERR_DEFAULTDIFFERENT:
  105. if (pPD->m_pd.hDevNames != NULL)
  106. {
  107. ASSERT(m_hDevNames == pPD->m_pd.hDevNames);
  108. AfxGlobalFree(pPD->m_pd.hDevNames);
  109. pPD->m_pd.hDevNames = NULL;
  110. m_hDevNames = NULL;
  111. }
  112. if (pPD->m_pd.hDevMode)
  113. {
  114. ASSERT(m_hDevMode == pPD->m_pd.hDevMode);
  115. AfxGlobalFree(pPD->m_pd.hDevMode);
  116. pPD->m_pd.hDevMode = NULL;
  117. m_hDevMode = NULL;
  118. }
  119. break;
  120. default:
  121. return nResponse; // do not update cached devMode/Names
  122. }
  123. nResponse = pPD->DoModal();
  124. }
  125. // refresh current CWinApp cache of printer device information
  126. m_hDevMode = pPD->m_pd.hDevMode;
  127. m_hDevNames = pPD->m_pd.hDevNames;
  128. return nResponse;
  129. }
  130. void CWinApp::OnFilePrintSetup()
  131. {
  132. CPrintDialog pd(TRUE);
  133. DoPrintDialog(&pd);
  134. }
  135. void CWinApp::SelectPrinter(HANDLE hDevNames, HANDLE hDevMode, BOOL bFreeOld)
  136. {
  137. if (m_hDevNames != hDevNames)
  138. {
  139. if (m_hDevNames != NULL && bFreeOld)
  140. AfxGlobalFree(m_hDevNames);
  141. m_hDevNames = hDevNames;
  142. }
  143. if (m_hDevMode != hDevMode)
  144. {
  145. if (m_hDevMode != NULL && bFreeOld)
  146. AfxGlobalFree(m_hDevMode);
  147. m_hDevMode = hDevMode;
  148. }
  149. }
  150. BOOL CWinApp::CreatePrinterDC(CDC& dc)
  151. {
  152. HDC hDC = AfxCreateDC(m_hDevNames, m_hDevMode);
  153. if (hDC != NULL)
  154. {
  155. dc.DeleteDC();
  156. VERIFY(dc.Attach(hDC));
  157. return TRUE;
  158. }
  159. return FALSE;
  160. }
  161. /////////////////////////////////////////////////////////////////////////////