home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / progwin / chap15 / print4.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-12  |  4.6 KB  |  153 lines

  1. /*---------------------------------------
  2.    PRINT4.C -- Printing with Banding
  3.                (c) Charles Petzold, 1990
  4.   ---------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. HDC  GetPrinterDC (void) ;              // in PRINT.C
  9.  
  10. typedef BOOL (FAR PASCAL * ABORTPROC) (HDC, short) ;
  11.  
  12. HANDLE hInst ;
  13. char   szAppName [] = "Print4" ;
  14. char   szCaption [] = "Print Program 4 (Banding)" ;
  15.  
  16. BOOL   bUserAbort ;
  17. HWND   hDlgPrint ;
  18.  
  19. BOOL FAR PASCAL PrintDlgProc (HWND hDlg, WORD message, WORD wParam, LONG lParam)
  20.      {
  21.      switch (message)
  22.           {
  23.           case WM_INITDIALOG:
  24.                SetWindowText (hDlg, szAppName) ;
  25.                EnableMenuItem (GetSystemMenu (hDlg, FALSE), SC_CLOSE,
  26.                                                             MF_GRAYED) ;
  27.                return TRUE ;
  28.  
  29.           case WM_COMMAND:
  30.                bUserAbort = TRUE ;
  31.                EnableWindow (GetParent (hDlg), TRUE) ;
  32.                DestroyWindow (hDlg) ;
  33.                hDlgPrint = 0 ;
  34.                return TRUE ;
  35.           }
  36.      return FALSE ;
  37.      }          
  38.  
  39. BOOL FAR PASCAL AbortProc (HDC hdcPrn, short nCode)
  40.      {
  41.      MSG   msg ;
  42.  
  43.      while (!bUserAbort && PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  44.           {
  45.           if (!hDlgPrint || !IsDialogMessage (hDlgPrint, &msg))
  46.                {
  47.                TranslateMessage (&msg) ;
  48.                DispatchMessage (&msg) ;
  49.                }
  50.           }
  51.      return !bUserAbort ;
  52.      }
  53.  
  54. BOOL PrintMyPage (HWND hwnd)
  55.      {
  56.      static char szSpMsg [] = "Print4: Printing" ; 
  57.      static char szText  [] = "Hello Printer!" ;
  58.      ABORTPROC   lpfnAbortProc ;
  59.      BOOL        bError = FALSE ;
  60.      DWORD       dwExtent ;
  61.      FARPROC     lpfnPrintDlgProc ;
  62.      HDC         hdcPrn ;
  63.      POINT       ptExtent ;
  64.      RECT        rect ;
  65.      short       xPage, yPage ;
  66.  
  67.      if (NULL == (hdcPrn = GetPrinterDC ()))
  68.           return TRUE ;
  69.  
  70.      xPage = GetDeviceCaps (hdcPrn, HORZRES) ;
  71.      yPage = GetDeviceCaps (hdcPrn, VERTRES) ;
  72.  
  73.      EnableWindow (hwnd, FALSE) ;
  74.  
  75.      bUserAbort = FALSE ;
  76.      lpfnPrintDlgProc = MakeProcInstance (PrintDlgProc, hInst) ;
  77.      hDlgPrint = CreateDialog (hInst, "PrintDlgBox", hwnd, lpfnPrintDlgProc) ;
  78.  
  79.      lpfnAbortProc = MakeProcInstance (AbortProc, hInst) ;
  80.      Escape (hdcPrn, SETABORTPROC, 0, (LPSTR) lpfnAbortProc, NULL) ;
  81.                                         
  82.      if (Escape (hdcPrn, STARTDOC, sizeof szSpMsg - 1, szSpMsg, NULL) > 0 &&
  83.          Escape (hdcPrn, NEXTBAND, 0, NULL, (LPSTR) &rect) > 0)
  84.           {
  85.           while (!IsRectEmpty (&rect) && !bUserAbort)
  86.                {
  87.                (*lpfnAbortProc) (hdcPrn, 0) ;
  88.  
  89.                Rectangle (hdcPrn, rect.left, rect.top, rect.right, 
  90.                                                        rect.bottom) ;
  91.                (*lpfnAbortProc) (hdcPrn, 0) ;
  92.           
  93.                MoveTo (hdcPrn, 0, 0) ;
  94.                LineTo (hdcPrn, xPage, yPage) ;
  95.  
  96.                (*lpfnAbortProc) (hdcPrn, 0) ;
  97.  
  98.                MoveTo (hdcPrn, xPage, 0) ;
  99.                LineTo (hdcPrn, 0, yPage) ;
  100.  
  101.                SaveDC (hdcPrn) ;
  102.  
  103.                SetMapMode (hdcPrn, MM_ISOTROPIC) ;
  104.                SetWindowExt   (hdcPrn, 1000, 1000) ;
  105.                SetViewportExt (hdcPrn, xPage / 2, -yPage / 2) ;
  106.                SetViewportOrg (hdcPrn, xPage / 2,  yPage / 2) ;
  107.  
  108.                (*lpfnAbortProc) (hdcPrn, 0) ;
  109.  
  110.                Ellipse (hdcPrn, -500, 500, 500, -500) ;
  111.  
  112.                (*lpfnAbortProc) (hdcPrn, 0) ;
  113.  
  114.                dwExtent = GetTextExtent (hdcPrn, szText, sizeof szText - 1) ;
  115.                ptExtent = MAKEPOINT (dwExtent) ;
  116.                TextOut (hdcPrn, -ptExtent.x / 2, ptExtent.y / 2, szText,
  117.                                                   sizeof szText - 1) ;
  118.  
  119.                RestoreDC (hdcPrn, -1) ;
  120.  
  121.                (*lpfnAbortProc) (hdcPrn, 0) ;
  122.  
  123.                if (Escape (hdcPrn, NEXTBAND, 0, NULL, (LPSTR) &rect) < 0)
  124.                     {
  125.                     bError = TRUE ;
  126.                     break ;
  127.                     }
  128.                }
  129.           }
  130.      else
  131.           bError = TRUE ;
  132.  
  133.      if (!bError)
  134.           {
  135.           if (bUserAbort)
  136.                Escape (hdcPrn, ABORTDOC, 0, NULL, NULL) ;
  137.           else
  138.                Escape (hdcPrn, ENDDOC, 0, NULL, NULL) ;
  139.           }
  140.  
  141.      if (!bUserAbort)
  142.           {
  143.           EnableWindow (hwnd, TRUE) ;     
  144.           DestroyWindow (hDlgPrint) ;
  145.           }
  146.  
  147.      FreeProcInstance (lpfnPrintDlgProc) ;
  148.      FreeProcInstance (lpfnAbortProc) ;
  149.      DeleteDC (hdcPrn) ;
  150.  
  151.      return bError || bUserAbort ;
  152.      }
  153.