home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP15 / PRINT4.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  3.2 KB  |  113 lines

  1. /*---------------------------------------
  2.    PRINT4.C -- Printing with Banding
  3.                (c) Charles Petzold, 1996
  4.   ---------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. HDC  GetPrinterDC (void) ;              // in PRINT.C
  9. void PageGDICalls (HDC, int, int) ;
  10.  
  11. HINSTANCE hInst ;
  12. char      szAppName[] = "Print4" ;
  13. char      szCaption[] = "Print Program 4 (Banding)" ;
  14.  
  15. BOOL   bUserAbort ;
  16. HWND   hDlgPrint ;
  17.  
  18. BOOL CALLBACK PrintDlgProc (HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  19.      {
  20.      switch (msg)
  21.           {
  22.           case WM_INITDIALOG :
  23.                SetWindowText (hDlg, szAppName) ;
  24.                EnableMenuItem (GetSystemMenu (hDlg, FALSE), SC_CLOSE,
  25.                                                             MF_GRAYED) ;
  26.                return TRUE ;
  27.  
  28.           case WM_COMMAND :
  29.                bUserAbort = TRUE ;
  30.                EnableWindow (GetParent (hDlg), TRUE) ;
  31.                DestroyWindow (hDlg) ;
  32.                hDlgPrint = 0 ;
  33.                return TRUE ;
  34.           }
  35.      return FALSE ;
  36.      }
  37.  
  38. BOOL CALLBACK AbortProc (HDC hdcPrn, int iCode)
  39.      {
  40.      MSG   msg ;
  41.  
  42.      while (!bUserAbort && PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  43.           {
  44.           if (!hDlgPrint || !IsDialogMessage (hDlgPrint, &msg))
  45.                {
  46.                TranslateMessage (&msg) ;
  47.                DispatchMessage (&msg) ;
  48.                }
  49.           }
  50.      return !bUserAbort ;
  51.      }
  52.  
  53. BOOL PrintMyPage (HWND hwnd)
  54.      {
  55.      static DOCINFO di     = { sizeof (DOCINFO), "Print4: Printing", NULL } ;
  56.      BOOL           bError = FALSE ;
  57.      HDC            hdcPrn ;
  58.      RECT           rect ;
  59.      int            xPage, yPage ;
  60.  
  61.      if (NULL == (hdcPrn = GetPrinterDC ()))
  62.           return TRUE ;
  63.  
  64.      xPage = GetDeviceCaps (hdcPrn, HORZRES) ;
  65.      yPage = GetDeviceCaps (hdcPrn, VERTRES) ;
  66.  
  67.      EnableWindow (hwnd, FALSE) ;
  68.  
  69.      bUserAbort = FALSE ;
  70.      hDlgPrint = CreateDialog (hInst, "PrintDlgBox", hwnd, PrintDlgProc) ;
  71.  
  72.      SetAbortProc (hdcPrn, AbortProc) ;
  73.  
  74.      if (StartDoc (hdcPrn, &di) > 0 && StartPage (hdcPrn) > 0 &&
  75.          ExtEscape (hdcPrn, NEXTBAND, 0, NULL, sizeof (rect), (LPSTR) &rect) > 0)
  76.           {
  77.           while (!IsRectEmpty (&rect) && !bUserAbort)
  78.                {
  79.                Rectangle (hdcPrn, rect.left, rect.top, rect.right, 
  80.                                                        rect.bottom) ;
  81.                PageGDICalls (hdcPrn, xPage, yPage) ;
  82.  
  83.                if (ExtEscape (hdcPrn, NEXTBAND, 0, NULL, sizeof (rect), 
  84.                    (LPSTR) &rect) < 0)
  85.                     {
  86.                     bError = TRUE ;          // If error, set flag and
  87.                     break ;                  //   break out of loop
  88.                     }
  89.                }
  90.           }
  91.      else
  92.           bError = TRUE ;
  93.  
  94.      if (!bError)
  95.           {
  96.           if (bUserAbort)
  97.                AbortDoc (hdcPrn) ;
  98.           else
  99.                if (EndPage (hdcPrn))
  100.                     EndDoc (hdcPrn) ;
  101.           }
  102.  
  103.      if (!bUserAbort)
  104.           {
  105.           EnableWindow (hwnd, TRUE) ;     
  106.           DestroyWindow (hDlgPrint) ;
  107.           }
  108.  
  109.      DeleteDC (hdcPrn) ;
  110.  
  111.      return bError || bUserAbort ;
  112.      }
  113.