home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / d / d020_1_4 / 6.ddi / SHOWDIB / PRINT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-01  |  5.7 KB  |  176 lines

  1. /*******************************************************************************
  2.  *                                           *
  3.  *  MODULE    : Print.c                               *
  4.  *                                           *
  5.  *  DESCRIPTION : Routines used for printing.                       *
  6.  *                                           *
  7.  *  FUNCTIONS    : GetPrinterDC()   - Gets default printer from WIN.INI and     *
  8.  *                     creates a DC for it.               *
  9.  *                                           *
  10.  *          InitPrinting()   - Initializes print job.               *
  11.  *                                           *
  12.  *          TermPrinting()   - Terminates print job.               *
  13.  *                                           *
  14.  *          PrintDlgProc()   - Dialog function for the "Cancel Printing" *
  15.  *                     dialog.                       *
  16.  *                                           *
  17.  *          AbortProc()       - Peeks at message queue for messages from  *
  18.  *                     the print dialog.                   *
  19.  *                                           *
  20.  *******************************************************************************/
  21.  
  22. #include <windows.h>
  23. #include <string.h>
  24. #include "showdib.h"
  25.  
  26. FARPROC  lpfnAbortProc      = NULL;
  27. FARPROC  lpfnPrintDlgProc = NULL;
  28. HWND     hWndParent      = NULL;
  29. HWND     hDlgPrint      = NULL;
  30. BOOL     bError;
  31. BOOL     bUserAbort;
  32.  
  33.  
  34. BOOL FAR PASCAL AbortProc (HDC, short);
  35. BOOL FAR PASCAL PrintDlgProc (HWND, unsigned, WORD, DWORD);
  36.  
  37. #pragma alloc_text(_PRINT, AbortProc, PrintDlgProc)
  38.  
  39. /****************************************************************************
  40.  *                                        *
  41.  *  FUNCTION   : GetPrinterDC()                         *
  42.  *                                        *
  43.  *  PURPOSE    : Read WIN.INI for default printer and create a DC for it.   *
  44.  *                                        *
  45.  *  RETURNS    : A handle to the DC if successful or NULL otherwise.        *
  46.  *                                        *
  47.  ****************************************************************************/
  48. HDC PASCAL GetPrinterDC()
  49. {
  50.     static char szPrinter [80];
  51.     char    *szDevice, *szDriver, *szOutput;
  52.  
  53.     GetProfileString ("windows", "device", "", szPrinter, sizeof(szPrinter));
  54.  
  55.     if ((szDevice = strtok (szPrinter, "," )) &&
  56.     (szDriver = strtok (NULL,      ", ")) &&
  57.     (szOutput = strtok (NULL,      ", ")))
  58.  
  59.     return CreateDC (szDriver, szDevice, szOutput, NULL) ;
  60.  
  61.     return NULL;
  62. }
  63. /****************************************************************************
  64.  *                                        *
  65.  *  FUNCTION   : InitPrinting(HDC hDC, HWND hWnd, HANDLE hInst, LPSTR msg)  *
  66.  *                                        *
  67.  *  PURPOSE    : Makes preliminary driver calls to set up print job.        *
  68.  *                                        *
  69.  *  RETURNS    : TRUE  - if successful.                     *
  70.  *         FALSE - otherwise.                        *
  71.  *                                        *
  72.  ****************************************************************************/
  73. BOOL PASCAL InitPrinting(HDC hDC, HWND hWnd, HANDLE hInst, LPSTR msg)
  74. {
  75.  
  76.     bError     = FALSE;     /* no errors yet */
  77.     bUserAbort = FALSE;     /* user hasn't aborted */
  78.  
  79.     hWndParent = hWnd;        /* save for Enable at Term time */
  80.  
  81.     lpfnPrintDlgProc = MakeProcInstance (PrintDlgProc, hInst);
  82.     lpfnAbortProc    = MakeProcInstance (AbortProc, hInst);
  83.  
  84.     hDlgPrint = CreateDialog (hInst, "PRTDLG", hWndParent, lpfnPrintDlgProc);
  85.  
  86.     if (!hDlgPrint)
  87.     return FALSE;
  88.  
  89.     SetWindowText (hDlgPrint, msg);
  90.     EnableWindow (hWndParent, FALSE);         /* disable parent */
  91.  
  92.     if ((Escape (hDC, SETABORTPROC, 0, (LPSTR)lpfnAbortProc, NULL) > 0) &&
  93.     (Escape (hDC, STARTDOC, lstrlen(msg), msg, NULL) > 0))
  94.     bError = FALSE;
  95.     else
  96.     bError = TRUE;
  97.  
  98.     /* might want to call the abort proc here to allow the user to
  99.      * abort just before printing begins */
  100.     return TRUE;
  101. }
  102. /****************************************************************************
  103.  *                                        *
  104.  *  FUNCTION   :  TermPrinting(HDC hDC)                     *
  105.  *                                        *
  106.  *  PURPOSE    :  Terminates print job.                     *
  107.  *                                        *
  108.  ****************************************************************************/
  109. void PASCAL TermPrinting(HDC hDC)
  110. {
  111.     if (!bError)
  112.     Escape(hDC, ENDDOC, 0, NULL, NULL);
  113.  
  114.     if (bUserAbort)
  115.     Escape (hDC, ABORTDOC, 0, NULL, NULL) ;
  116.     else {
  117.     EnableWindow(hWndParent, TRUE);
  118.     DestroyWindow(hDlgPrint);
  119.     }
  120.  
  121.     FreeProcInstance(lpfnAbortProc);
  122.     FreeProcInstance(lpfnPrintDlgProc);
  123. }
  124. /****************************************************************************
  125.  *                                        *
  126.  *  FUNCTION   :PrintDlgProc (HWND, unsigned , WORD , DWORD )            *
  127.  *                                        *
  128.  *  PURPOSE    :Dialog function for the "Cancel Printing" dialog. It sets   *
  129.  *        the abort flag if the user presses <Cancel>.            *
  130.  *                                        *
  131.  ****************************************************************************/
  132. BOOL FAR PASCAL PrintDlgProc (HWND hDlg, unsigned iMessage, WORD wParam, DWORD lParam)
  133. {
  134.     switch (iMessage) {
  135.     case WM_INITDIALOG:
  136.  
  137.         EnableMenuItem (GetSystemMenu (hDlg, FALSE), SC_CLOSE, MF_GRAYED);
  138.         break;
  139.  
  140.     case WM_COMMAND:
  141.         bUserAbort = TRUE;
  142.         EnableWindow (hWndParent, TRUE);
  143.         DestroyWindow (hDlg);
  144.         hDlgPrint = 0;
  145.         break;
  146.  
  147.     default:
  148.         return FALSE;
  149.     }
  150.     return TRUE;
  151. }
  152.  
  153. /****************************************************************************
  154.  *                                        *
  155.  *  FUNCTION   :AbortProc (HDC hPrnDC, short nCode)                *
  156.  *                                        *
  157.  *  PURPOSE    :Checks message queue for messages from the "Cancel Printing"*
  158.  *        dialog. If it sees a message, (this will be from a print    *
  159.  *        cancel command), it terminates.                 *
  160.  *                                        *
  161.  *  RETURNS    :Inverse of Abort flag                        *
  162.  *                                        *
  163.  ****************************************************************************/
  164. BOOL FAR PASCAL AbortProc (HDC hPrnDC, short nCode)
  165. {
  166.     MSG   msg;
  167.  
  168.     while (!bUserAbort && PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {
  169.     if (!hDlgPrint || !IsDialogMessage(hDlgPrint, &msg)) {
  170.         TranslateMessage (&msg);
  171.         DispatchMessage (&msg);
  172.     }
  173.     }
  174.     return !bUserAbort;
  175. }
  176.