dlgprnt.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 <dlgs.h> // for standard control IDs for commdlg
  12. #ifdef AFX_AUX_SEG
  13. #pragma code_seg(AFX_AUX_SEG)
  14. #endif
  15. #ifdef _DEBUG
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19. #define new DEBUG_NEW
  20. /////////////////////////////////////////////////////////////////////////////
  21. // Page Setup dialog
  22. CPageSetupDialog::CPageSetupDialog(DWORD dwFlags, CWnd* pParentWnd) :
  23. CCommonDialog(pParentWnd)
  24. {
  25. memset(&m_psd, 0, sizeof(m_psd));
  26. m_psd.lStructSize = sizeof(m_psd);
  27. m_psd.Flags = (dwFlags | PSD_ENABLEPAGESETUPHOOK | PSD_ENABLEPAGEPAINTHOOK);
  28. if (!afxData.bWin4 && AfxHelpEnabled())
  29. m_psd.Flags |= PSD_SHOWHELP;
  30. m_psd.lpfnPageSetupHook = (COMMDLGPROC)_AfxCommDlgProc;
  31. m_psd.lpfnPagePaintHook = (COMMDLGPROC)CPageSetupDialog::PaintHookProc;
  32. }
  33. int CPageSetupDialog::DoModal()
  34. {
  35. ASSERT_VALID(this);
  36. ASSERT(m_psd.Flags & PSD_ENABLEPAGESETUPHOOK);
  37. ASSERT(m_psd.Flags & PSD_ENABLEPAGEPAINTHOOK);
  38. ASSERT(m_psd.lpfnPageSetupHook != NULL); // can still be a user hook
  39. ASSERT(m_psd.lpfnPagePaintHook != NULL); // can still be a user hook
  40. m_psd.hwndOwner = PreModal();
  41. int nResult = ::PageSetupDlg(&m_psd);
  42. PostModal();
  43. return nResult ? nResult : IDCANCEL;
  44. }
  45. ////////////////////////////////////////////////////////////////////////////
  46. // CPageSetupDialog attributes
  47. LPDEVMODE CPageSetupDialog::GetDevMode() const
  48. {
  49. if (m_psd.hDevMode == NULL)
  50. return NULL;
  51. return (LPDEVMODE)::GlobalLock(m_psd.hDevMode);
  52. }
  53. CString CPageSetupDialog::GetDriverName() const
  54. {
  55. if (m_psd.hDevNames == NULL)
  56. return afxEmptyString;
  57. LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_psd.hDevNames);
  58. return (LPCTSTR)lpDev + lpDev->wDriverOffset;
  59. }
  60. CString CPageSetupDialog::GetDeviceName() const
  61. {
  62. if (m_psd.hDevNames == NULL)
  63. return afxEmptyString;
  64. LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_psd.hDevNames);
  65. return (LPCTSTR)lpDev + lpDev->wDeviceOffset;
  66. }
  67. CString CPageSetupDialog::GetPortName() const
  68. {
  69. if (m_psd.hDevNames == NULL)
  70. return afxEmptyString;
  71. LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_psd.hDevNames);
  72. return (LPCTSTR)lpDev + lpDev->wOutputOffset;
  73. }
  74. // Create an HDC from the devnames and devmode.
  75. HDC CPageSetupDialog::CreatePrinterDC()
  76. {
  77. ASSERT_VALID(this);
  78. return AfxCreateDC(m_psd.hDevNames, m_psd.hDevMode);
  79. }
  80. void CPageSetupDialog::GetMargins(LPRECT lpRectMargins, LPRECT lpRectMinMargins) const
  81. {
  82. if (lpRectMargins != NULL)
  83. memcpy(lpRectMargins, &m_psd.rtMargin, sizeof(RECT));
  84. if (lpRectMinMargins != NULL)
  85. memcpy(lpRectMinMargins, &m_psd.rtMinMargin, sizeof(RECT));
  86. }
  87. ////////////////////////////////////////////////////////////////////////////
  88. // CPageSetupDialog diagnostics
  89. #ifdef _DEBUG
  90. void CPageSetupDialog::Dump(CDumpContext& dc) const
  91. {
  92. CDialog::Dump(dc);
  93. dc << "m_psd.hwndOwner = " << (UINT)m_psd.hwndOwner;
  94. dc << "\nm_psd.Flags = " << (LPVOID)m_psd.Flags;
  95. dc << "\nm_psd.ptPaperSize = " << m_psd.ptPaperSize;
  96. dc << "\nm_psd.rtMinMargin = " << m_psd.rtMinMargin;
  97. dc << "\nm_psd.rtMinMargin = " << m_psd.rtMinMargin;
  98. if (m_psd.lpfnPageSetupHook == (COMMDLGPROC)_AfxCommDlgProc)
  99. dc << "\nsetup hook function set to standard MFC hook function";
  100. else
  101. dc << "\nsetup hook function set to non-standard hook function";
  102. if (m_psd.lpfnPagePaintHook == (COMMDLGPROC)_AfxCommDlgProc)
  103. dc << "\nprint hook function set to standard MFC hook function";
  104. else
  105. dc << "\nprint hook function set to non-standard hook function";
  106. dc << "\n";
  107. }
  108. #endif //_DEBUG
  109. ////////////////////////////////////////////////////////////////////////////
  110. // CPageSetupDialog hook
  111. UINT CPageSetupDialog::PreDrawPage(WORD /*wPaperType*/, WORD /*wFlags*/,
  112. LPPAGESETUPDLG)
  113. {
  114. return 0;
  115. //return 1 to prevent any more drawing
  116. }
  117. UINT CPageSetupDialog::OnDrawPage(CDC*, UINT /*nMessage*/, LPRECT)
  118. {
  119. return 0; // do the default
  120. }
  121. UINT CALLBACK CPageSetupDialog::PaintHookProc(HWND hWnd, UINT message, WPARAM wParam,
  122. LPARAM lParam)
  123. {
  124. if (hWnd == NULL)
  125. return 0;
  126. // Get our Window
  127. // assume it is already wired up to a permanent one
  128. // the hWnd is the HWND of a control in the page setup proc
  129. CPageSetupDialog* pDlg = DYNAMIC_DOWNCAST(CPageSetupDialog,
  130. CWnd::FromHandlePermanent(::GetParent(hWnd)));
  131. if (pDlg == NULL)
  132. return 0;
  133. switch (message)
  134. {
  135. case WM_PSD_PAGESETUPDLG:
  136. return pDlg->PreDrawPage(LOWORD(wParam), HIWORD(wParam),
  137. (LPPAGESETUPDLG) lParam);
  138. break;
  139. case WM_PSD_FULLPAGERECT:
  140. case WM_PSD_MINMARGINRECT:
  141. case WM_PSD_MARGINRECT:
  142. case WM_PSD_GREEKTEXTRECT:
  143. case WM_PSD_ENVSTAMPRECT:
  144. case WM_PSD_YAFULLPAGERECT:
  145. return pDlg->OnDrawPage(CDC::FromHandle((HDC)wParam), message, (LPRECT)lParam);
  146. break;
  147. }
  148. return 0;
  149. }
  150. /////////////////////////////////////////////////////////////////////////////
  151. // Print/Print Setup dialog
  152. BEGIN_MESSAGE_MAP(CPrintDialog, CCommonDialog)
  153. //{{AFX_MSG_MAP(CPrintDialog)
  154. ON_COMMAND(psh1, OnPrintSetup) // print setup button when print is displayed
  155. //}}AFX_MSG_MAP
  156. END_MESSAGE_MAP()
  157. CPrintDialog::CPrintDialog(BOOL bPrintSetupOnly,
  158. DWORD dwFlags, CWnd* pParentWnd)
  159. : m_pd(m_pdActual), CCommonDialog(pParentWnd)
  160. {
  161. memset(&m_pdActual, 0, sizeof(m_pdActual));
  162. m_pd.lStructSize = sizeof(m_pdActual);
  163. m_pd.Flags = dwFlags;
  164. if (!afxData.bWin4 && AfxHelpEnabled())
  165. {
  166. m_pd.Flags |= PD_SHOWHELP | PD_ENABLEPRINTHOOK | PD_ENABLESETUPHOOK;
  167. m_pd.lpfnPrintHook = (COMMDLGPROC)_AfxCommDlgProc;
  168. m_pd.lpfnSetupHook = (COMMDLGPROC)_AfxCommDlgProc;
  169. }
  170. if (bPrintSetupOnly)
  171. {
  172. m_nIDHelp = AFX_IDD_PRINTSETUP;
  173. m_pd.Flags |= PD_PRINTSETUP;
  174. }
  175. else
  176. {
  177. m_nIDHelp = AFX_IDD_PRINT;
  178. m_pd.Flags |= PD_RETURNDC;
  179. }
  180. m_pd.Flags &= ~PD_RETURNIC; // do not support information context
  181. }
  182. // Helper ctor for AttachOnSetup
  183. CPrintDialog::CPrintDialog(PRINTDLG& pdInit)
  184. : m_pd(pdInit), CCommonDialog(NULL)
  185. {
  186. }
  187. // Function to keep m_pd in sync after user invokes Setup from
  188. // the print dialog (via the Setup button)
  189. // If you decide to handle any messages/notifications and wish to
  190. // handle them differently between Print/PrintSetup then override
  191. // this function and create an object of a derived class
  192. CPrintDialog* CPrintDialog::AttachOnSetup()
  193. {
  194. ASSERT_VALID(this);
  195. CPrintDialog* pDlgSetup;
  196. pDlgSetup = new CPrintDialog(m_pd);
  197. pDlgSetup->m_hWnd = NULL;
  198. pDlgSetup->m_pParentWnd = m_pParentWnd;
  199. pDlgSetup->m_nIDHelp = AFX_IDD_PRINTSETUP;
  200. return pDlgSetup;
  201. }
  202. void CPrintDialog::OnPrintSetup()
  203. {
  204. ASSERT_VALID(this);
  205. CPrintDialog* pDlgSetup = AttachOnSetup();
  206. ASSERT(pDlgSetup != NULL);
  207. AfxHookWindowCreate(pDlgSetup);
  208. Default();
  209. AfxUnhookWindowCreate();
  210. delete pDlgSetup;
  211. }
  212. int CPrintDialog::DoModal()
  213. {
  214. ASSERT_VALID(this);
  215. m_pd.hwndOwner = PreModal();
  216. int nResult = ::PrintDlg(&m_pd);
  217. PostModal();
  218. return nResult ? nResult : IDCANCEL;
  219. }
  220. // Create an HDC without calling DoModal.
  221. HDC CPrintDialog::CreatePrinterDC()
  222. {
  223. ASSERT_VALID(this);
  224. m_pd.hDC = AfxCreateDC(m_pd.hDevNames, m_pd.hDevMode);
  225. return m_pd.hDC;
  226. }
  227. int CPrintDialog::GetCopies() const
  228. {
  229. ASSERT_VALID(this);
  230. if (m_pd.Flags & PD_USEDEVMODECOPIES)
  231. return GetDevMode()->dmCopies;
  232. else
  233. return m_pd.nCopies;
  234. }
  235. LPDEVMODE CPrintDialog::GetDevMode() const
  236. {
  237. if (m_pd.hDevMode == NULL)
  238. return NULL;
  239. return (LPDEVMODE)::GlobalLock(m_pd.hDevMode);
  240. }
  241. CString CPrintDialog::GetDriverName() const
  242. {
  243. if (m_pd.hDevNames == NULL)
  244. return afxEmptyString;
  245. LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_pd.hDevNames);
  246. return (LPCTSTR)lpDev + lpDev->wDriverOffset;
  247. }
  248. CString CPrintDialog::GetDeviceName() const
  249. {
  250. if (m_pd.hDevNames == NULL)
  251. return afxEmptyString;
  252. LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_pd.hDevNames);
  253. return (LPCTSTR)lpDev + lpDev->wDeviceOffset;
  254. }
  255. CString CPrintDialog::GetPortName() const
  256. {
  257. if (m_pd.hDevNames == NULL)
  258. return afxEmptyString;
  259. LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_pd.hDevNames);
  260. return (LPCTSTR)lpDev + lpDev->wOutputOffset;
  261. }
  262. // this function must not be in afxdlgs.inl because of DLL delay loading
  263. BOOL CPrintDialog::GetDefaults()
  264. {
  265. m_pd.Flags |= PD_RETURNDEFAULT;
  266. return ::PrintDlg(&m_pd);
  267. }
  268. ////////////////////////////////////////////////////////////////////////////
  269. // CPrintDialog diagnostics
  270. #ifdef _DEBUG
  271. void CPrintDialog::Dump(CDumpContext& dc) const
  272. {
  273. CDialog::Dump(dc);
  274. dc << "m_pd.hwndOwner = " << (UINT)m_pd.hwndOwner;
  275. if (m_pd.hDC != NULL)
  276. dc << "\nm_pd.hDC = " << CDC::FromHandle(m_pd.hDC);
  277. dc << "\nm_pd.Flags = " << (LPVOID)m_pd.Flags;
  278. dc << "\nm_pd.nFromPage = " << m_pd.nFromPage;
  279. dc << "\nm_pd.nToPage = " << m_pd.nToPage;
  280. dc << "\nm_pd.nMinPage = " << m_pd.nMinPage;
  281. dc << "\nm_pd.nMaxPage = " << m_pd.nMaxPage;
  282. dc << "\nm_pd.nCopies = " << m_pd.nCopies;
  283. if (m_pd.lpfnSetupHook == (COMMDLGPROC)_AfxCommDlgProc)
  284. dc << "\nsetup hook function set to standard MFC hook function";
  285. else
  286. dc << "\nsetup hook function set to non-standard hook function";
  287. if (m_pd.lpfnPrintHook == (COMMDLGPROC)_AfxCommDlgProc)
  288. dc << "\nprint hook function set to standard MFC hook function";
  289. else
  290. dc << "\nprint hook function set to non-standard hook function";
  291. dc << "\n";
  292. }
  293. #endif //_DEBUG
  294. ////////////////////////////////////////////////////////////////////////////
  295. // AfxCreateDC
  296. HDC AFXAPI AfxCreateDC(HGLOBAL hDevNames, HGLOBAL hDevMode)
  297. {
  298. if (hDevNames == NULL)
  299. return NULL;
  300. LPDEVNAMES lpDevNames = (LPDEVNAMES)::GlobalLock(hDevNames);
  301. LPDEVMODE lpDevMode = (hDevMode != NULL) ?
  302. (LPDEVMODE)::GlobalLock(hDevMode) : NULL;
  303. if (lpDevNames == NULL)
  304. return NULL;
  305. HDC hDC = ::CreateDC((LPCTSTR)lpDevNames + lpDevNames->wDriverOffset,
  306. (LPCTSTR)lpDevNames + lpDevNames->wDeviceOffset,
  307. (LPCTSTR)lpDevNames + lpDevNames->wOutputOffset,
  308. lpDevMode);
  309. ::GlobalUnlock(hDevNames);
  310. if (hDevMode != NULL)
  311. ::GlobalUnlock(hDevMode);
  312. return hDC;
  313. }
  314. #ifdef AFX_INIT_SEG
  315. #pragma code_seg(AFX_INIT_SEG)
  316. #endif
  317. IMPLEMENT_DYNAMIC(CPrintDialog, CDialog)
  318. IMPLEMENT_DYNAMIC(CPageSetupDialog, CDialog)
  319. ////////////////////////////////////////////////////////////////////////////