home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / WRITEPAD.PAK / PRINTDLG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  5.4 KB  |  230 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE: printdlg.c
  9. //
  10. //  PURPOSE:
  11. //    To show the use of the "Print" and "Print Setup" common dialog
  12. //    boxes.
  13. //
  14. //  FUNCTIONS:
  15. //    CmdFilePrint   - Present the print dialog to the user and process 
  16. //                       the results.
  17. //    CmdFilePrintSU -Present the print setup common dialog to the user.
  18. //
  19. //  COMMENTS:
  20. //
  21. //
  22. //
  23. //  SPECIAL INSTRUCTIONS: N/A
  24. //
  25.  
  26. #include <windows.h>            // required for all Windows applications
  27. #include "globals.h"            // prototypes specific to this application
  28.  
  29. static HANDLE hDevMode = NULL;
  30. static HANDLE hDevNames = NULL;
  31.  
  32. //
  33. //  FUNCTION: CmdFilePrint(HWND, WORD, WORD, HWND)
  34. //
  35. //  PURPOSE: Present the print dialog to the user and process the results.
  36. //
  37. //  PARAMETERS:
  38. //    hwnd     - The window handle.
  39. //    wCommand - IDM_PRINT (Unused)
  40. //    wNotify  - Notification number (unused)
  41. //    hwndCtrl - NULL (Unused)
  42. //
  43. //  RETURN VALUE:
  44. //    Always returns 0 - Command handled.
  45. //
  46. //  COMMENTS:
  47. //
  48. //
  49.  
  50. #pragma argsused
  51. LRESULT CmdFilePrint(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  52. {
  53.     PRINTDLG pd = {0};
  54.  
  55.     static DWORD Flags     = PD_ALLPAGES | PD_SHOWHELP | PD_RETURNDC;
  56.     static WORD  nFromPage = 0xFFFF;
  57.     static WORD  nToPage   = 0xFFFF;
  58.     static WORD  nCopies   = 1;
  59.  
  60.     pd.lStructSize = sizeof(pd);
  61.     pd.hwndOwner   = hwnd;
  62.     pd.hDevMode    = hDevMode;
  63.     pd.hDevNames   = hDevNames;
  64.     pd.Flags       = Flags;
  65.     pd.nFromPage   = nFromPage;
  66.      pd.nToPage     = nToPage;
  67.     pd.nMinPage    = 0;
  68.     pd.nMaxPage    = 0xFFFF;
  69.     pd.nCopies     = nCopies;
  70.  
  71.     if (PrintDlg(&pd))
  72.     {
  73.         Print(hwnd,
  74.               pd.hDC,
  75.               MAKEBOOL(pd.Flags & PD_PAGENUMS),
  76.               MAKEBOOL(pd.Flags & PD_SELECTION),
  77.               MAKEBOOL(pd.Flags & PD_COLLATE),
  78.               MAKEBOOL(pd.Flags & PD_PRINTTOFILE),
  79.               pd.nFromPage,
  80.               pd.nToPage,
  81.                   pd.nCopies,
  82.               pd.hDevNames);
  83.         hDevMode   = pd.hDevMode;
  84.         hDevNames  = pd.hDevNames;
  85.         Flags      = pd.Flags;
  86.         nFromPage  = pd.nFromPage;
  87.         nToPage    = pd.nToPage;
  88.         nCopies    = pd.nCopies;
  89.     }
  90.  
  91.     return 0;
  92. }
  93.  
  94.  
  95. //
  96. //  FUNCTION: CmdPrintSU(HWND, WORD, WORD, HWND)
  97. //
  98. //  PURPOSE: Present the print setup common dialog to the user.
  99. //
  100. //  PARAMETERS:
  101. //    hwnd     - The window handle.
  102. //    wCommand - IDM_PRINTSU (Unused)
  103. //    wNotify   - Notification number (unused)
  104. //    hwndCtrl - NULL (Unused)
  105. //
  106. //  RETURN VALUE:
  107. //    Always returns 0 - Command handled.
  108. //
  109. //  COMMENTS:
  110. //
  111. //
  112.  
  113. #pragma argsused
  114. LRESULT CmdFilePrintSU(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  115. {
  116.     PRINTDLG pd = {0};
  117.  
  118.     pd.lStructSize = sizeof(pd);
  119.     pd.hwndOwner   = hwnd;
  120.     pd.hDevMode    = hDevMode;
  121.     pd.hDevNames   = hDevNames;
  122.     pd.Flags = PD_SHOWHELP | PD_PRINTSETUP;
  123.  
  124.     if (PrintDlg(&pd))
  125.     {
  126.         hDevMode = pd.hDevMode;
  127.         hDevNames = pd.hDevNames;
  128.     }
  129.  
  130.     return 0;
  131. }
  132.  
  133.  
  134. //
  135. //  FUNCTION: GetCurrentPrinterDC(void)
  136. //
  137. //  PURPOSE: Retrieve a DC for the current printer or default
  138. //
  139. //  PARAMETERS:
  140. //    none
  141. //
  142. //  RETURN VALUE:
  143. //    Printer DC or NULL on error
  144. //
  145. //  COMMENTS:
  146. //    If no printer has been selected yet, a DC for the default
  147. //    printer is retrieved using PrintDlg.  Otherwise, a DC is
  148. //    created from the existing hDevNames and hDevMode.
  149. //
  150.  
  151. HDC GetCurrentPrinterDC(void)
  152. {
  153.     HDC hdc = NULL;
  154.  
  155.     if (hDevNames)
  156.     {
  157.         LPDEVNAMES lpdn = (LPDEVNAMES)GlobalLock(hDevNames);
  158.         LPDEVMODE lpdm = NULL;
  159.  
  160.         if (hDevMode)
  161.             lpdm = (LPDEVMODE)GlobalLock(hDevMode);
  162.  
  163.         hdc = CreateDC((LPSTR)lpdn + lpdn->wDriverOffset,
  164.                        (LPSTR)lpdn + lpdn->wDeviceOffset,
  165.                        (LPSTR)lpdn + lpdn->wOutputOffset,
  166.                        lpdm);
  167.     }
  168.     else
  169.     {
  170.         PRINTDLG pd = {0};
  171.  
  172.         if (hDevMode)
  173.             GlobalFree(hDevMode);
  174.  
  175.         pd.lStructSize = sizeof(pd);
  176.         pd.hwndOwner   = NULL;
  177.         pd.Flags = PD_RETURNDEFAULT | PD_RETURNDC;
  178.  
  179.         if (PrintDlg(&pd))
  180.         {
  181.             hDevMode = pd.hDevMode;
  182.             hDevNames = pd.hDevNames;
  183.             hdc = pd.hDC;
  184.         }
  185.     }
  186.  
  187.     return hdc;
  188. }
  189.  
  190.  
  191. //
  192. //  FUNCTION: CmdPrintPreview(HWND, WORD, WORD, HWND)
  193. //
  194. //  PURPOSE: Do WYSIWYG print preview for the current document
  195. //
  196. //  PARAMETERS:
  197. //    hwnd     - The window handle.
  198. //    wCommand - IDM_PRINTPREVIEW (Unused)
  199. //    wNotify   - Notification number (unused)
  200. //    hwndCtrl - NULL (Unused)
  201. //
  202. //  RETURN VALUE:
  203. //    Always returns 0 - Command handled.
  204. //
  205. //  COMMENTS:
  206. //
  207. //
  208.  
  209. #pragma argsused
  210. LRESULT CmdFilePrintPreview(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  211. {
  212.      HDC hdc;
  213.      HWND hwndEdit = GetEditWindow(NULL);
  214.  
  215.     if (!hwndEdit)
  216.         return 0;
  217.  
  218.     hdc = GetCurrentPrinterDC();
  219.  
  220.     if (hdc)
  221.     {
  222.         PrintPreview(hwnd, hwndEdit, hdc);
  223.         DeleteDC(hdc);
  224.     }
  225.     else
  226.         MessageBox(hwnd, "Unable to create printer DC.", NULL, MB_OK);
  227.  
  228.      return 0;
  229. }
  230.