home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap13 / Print3 / Print3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  2.6 KB  |  99 lines

  1. /*---------------------------------------
  2.    PRINT3.C -- Printing with Dialog Box
  3.                (c) Charles Petzold, 1998
  4.   ---------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. HDC  GetPrinterDC (void) ;              // in GETPRNDC.C
  9. void PageGDICalls (HDC, int, int) ;     // in PRINT.C
  10.  
  11. HINSTANCE hInst ;
  12. TCHAR     szAppName[] = TEXT ("Print3") ;
  13. TCHAR     szCaption[] = TEXT ("Print Program 3 (Dialog Box)") ;
  14.  
  15. BOOL bUserAbort ;
  16. HWND hDlgPrint ;
  17.  
  18. BOOL CALLBACK PrintDlgProc (HWND hDlg, UINT message, 
  19.                             WPARAM wParam, LPARAM lParam)
  20. {
  21.      switch (message)
  22.      {
  23.      case WM_INITDIALOG:
  24.           SetWindowText (hDlg, szAppName) ;
  25.           EnableMenuItem (GetSystemMenu (hDlg, FALSE), SC_CLOSE, MF_GRAYED) ;
  26.           return TRUE ;
  27.           
  28.      case WM_COMMAND:
  29.           bUserAbort = TRUE ;
  30.           EnableWindow (GetParent (hDlg), TRUE) ;
  31.           DestroyWindow (hDlg) ;
  32.           hDlgPrint = NULL ;
  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), TEXT ("Print3: Printing") } ;
  56.      BOOL           bSuccess = TRUE ;
  57.      HDC            hdcPrn ;
  58.      int            xPage, yPage ;
  59.      
  60.      if (NULL == (hdcPrn = GetPrinterDC ()))
  61.           return FALSE ;
  62.      
  63.      xPage = GetDeviceCaps (hdcPrn, HORZRES) ;
  64.      yPage = GetDeviceCaps (hdcPrn, VERTRES) ;
  65.      
  66.      EnableWindow (hwnd, FALSE) ;
  67.      
  68.      bUserAbort = FALSE ;
  69.      hDlgPrint = CreateDialog (hInst, TEXT ("PrintDlgBox"), 
  70.                                hwnd, PrintDlgProc) ;
  71.      
  72.      SetAbortProc (hdcPrn, AbortProc) ;
  73.      
  74.      if (StartDoc (hdcPrn, &di) > 0)
  75.      {
  76.           if (StartPage (hdcPrn) > 0)
  77.           {
  78.                PageGDICalls (hdcPrn, xPage, yPage) ;
  79.                
  80.                if (EndPage (hdcPrn) > 0)
  81.                     EndDoc (hdcPrn) ;
  82.                else
  83.                     bSuccess = FALSE ;
  84.           }
  85.      }
  86.      else
  87.           bSuccess = FALSE ;
  88.  
  89.      if (!bUserAbort)
  90.      {
  91.           EnableWindow (hwnd, TRUE) ;
  92.           DestroyWindow (hDlgPrint) ;
  93.      }
  94.      
  95.      DeleteDC (hdcPrn) ;
  96.      
  97.      return bSuccess && !bUserAbort ;
  98. }
  99.