home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / gdi / showdib / print.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  10KB  |  235 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples. 
  4. *       Copyright (C) 1993-1997 Microsoft Corporation.
  5. *       All rights reserved. 
  6. *       This source code is only intended as a supplement to 
  7. *       Microsoft Development Tools and/or WinHelp documentation.
  8. *       See these sources for detailed information regarding the 
  9. *       Microsoft samples programs.
  10. \******************************************************************************/
  11.  
  12. /*******************************************************************************
  13.  *                                                                             *
  14.  *  MODULE      : Print.c                                                      *
  15.  *                                                                             *
  16.  *  DESCRIPTION : Routines used for printing.                                  *
  17.  *                                                                             *
  18.  *  FUNCTIONS   : GetPrinterDC()   - Gets default printer from WIN.INI and     *
  19.  *                                   creates a DC for it.                      *
  20.  *                                                                             *
  21.  *                InitPrinting()   - Initializes print job.                    *
  22.  *                                                                             *
  23.  *                TermPrinting()   - Terminates print job.                     *
  24.  *                                                                             *
  25.  *                PrintDlgProc()   - Dialog function for the "Cancel Printing" *
  26.  *                                   dialog.                                   *
  27.  *                                                                             *
  28.  *                AbortProc()      - Peeks at message queue for messages from  *
  29.  *                                   the print dialog.                         *
  30.  *                                                                             *
  31.  *******************************************************************************/
  32.  
  33. #include <windows.h>
  34. #include <string.h>
  35. #include <commdlg.h>
  36. #define _MBCS
  37. #include <mbstring.h>
  38. #include "showdib.h"
  39.  
  40. FARPROC  lpfnAbortProc    = NULL;
  41. FARPROC  lpfnPrintDlgProc = NULL;
  42. HWND     hWndParent       = NULL;
  43. HWND     hDlgPrint        = NULL;
  44. BOOL     bError;
  45. BOOL     bUserAbort;
  46.  
  47.  
  48. BOOL APIENTRY AbortProc (HDC, SHORT);
  49. BOOL APIENTRY PrintDlgProc (HWND, WORD, UINT, DWORD);
  50.  
  51. /****************************************************************************
  52.  *                                                                          *
  53.  *  FUNCTION   : GetPrinterDC()                                             *
  54.  *                                                                          *
  55.  *  PURPOSE    : Read WIN.INI for default printer and create a DC for it.   *
  56.  *                                                                          *
  57.  *  RETURNS    : A handle to the DC if successful or NULL otherwise.        *
  58.  *                                                                          *
  59.  ****************************************************************************/
  60. HDC PASCAL GetPrinterDC() {
  61.  
  62.     PRINTDLG pd;
  63.  
  64.     memset(&pd, 0, sizeof(PRINTDLG));
  65.     pd.lStructSize = sizeof(PRINTDLG);
  66.     pd.Flags = PD_RETURNDC;
  67.     pd.hwndOwner = hWndApp;
  68.     pd.hInstance = (HANDLE) NULL;
  69.  
  70.     // Display the PRINT dialog box. */
  71.  
  72.     if( PrintDlg(&pd) == TRUE ) {
  73.         return( pd.hDC );        
  74.     }
  75.     else
  76.         return( NULL );
  77.  
  78. }
  79.  
  80. //
  81. // Getting the printer DC the old fashion way...
  82. // called to enable or disable the IDM_PRINT menu item.
  83. //
  84. HDC PASCAL GetPrinterDC1()
  85. {
  86.     static CHAR szPrinter [80];
  87.     CHAR    *szDevice, *szDriver, *szOutput;
  88.  
  89.     GetProfileString ("windows", "device", "", szPrinter, sizeof(szPrinter));
  90.  
  91.     if ((szDevice = _mbstok (szPrinter, "," )) &&
  92.         (szDriver = _mbstok (NULL,      ", ")) &&
  93.         (szOutput = _mbstok (NULL,      ", ")))
  94.  
  95.         return CreateDC (szDriver, szDevice, szOutput, NULL) ;
  96.  
  97.     return NULL;
  98. }
  99.  
  100. /****************************************************************************
  101.  *                                                                          *
  102.  *  FUNCTION   : InitPrinting(HDC hDC, HWND hWnd, HANDLE hInst, LPSTR msg)  *
  103.  *                                                                          *
  104.  *  PURPOSE    : Makes preliminary driver calls to set up print job.        *
  105.  *                                                                          *
  106.  *  RETURNS    : TRUE  - if successful.                                     *
  107.  *               FALSE - otherwise.                                         *
  108.  *                                                                          *
  109.  ****************************************************************************/
  110. BOOL PASCAL InitPrinting(HDC hDC, HWND hWnd, HANDLE hInst, LPSTR msg)
  111. {
  112.     DOCINFO         DocInfo;
  113.  
  114.     bError     = FALSE;     /* no errors yet */
  115.     bUserAbort = FALSE;     /* user hasn't aborted */
  116.  
  117.     hWndParent = hWnd;      /* save for Enable at Term time */
  118.  
  119.     lpfnPrintDlgProc = (DLGPROC) MakeProcInstance (PrintDlgProc, hInst);
  120.     lpfnAbortProc    = (WNDPROC) MakeProcInstance (AbortProc, hInst);
  121.  
  122.     hDlgPrint = CreateDialog (hInst, "PRTDLG", hWndParent, 
  123.         (DLGPROC)lpfnPrintDlgProc);
  124.  
  125.     if (!hDlgPrint)
  126.         return FALSE;
  127.  
  128.     SetWindowText (hDlgPrint, msg);
  129.     EnableWindow (hWndParent, FALSE);        /* disable parent */
  130.  
  131. //
  132. // Use new printing APIs...Petrus Wong 12-May-1993
  133. //
  134.     if (SetAbortProc(hDC, (ABORTPROC)lpfnAbortProc) <= 0) {
  135.         bError = TRUE;
  136.         return FALSE;
  137.     }
  138.  
  139.     memset(&DocInfo, 0, sizeof(DOCINFO));
  140.     DocInfo.cbSize      = sizeof(DOCINFO);
  141.     DocInfo.lpszDocName = (LPTSTR) msg;
  142.     DocInfo.lpszOutput  = NULL;
  143.  
  144.     if (StartDoc(hDC, &DocInfo) <= 0) {
  145.         bError = TRUE;
  146.         return FALSE;
  147.     }
  148.     bError = FALSE;
  149.  
  150.     /* might want to call the abort proc here to allow the user to
  151.      * abort just before printing begins */
  152.     return TRUE;
  153. }
  154. /****************************************************************************
  155.  *                                                                          *
  156.  *  FUNCTION   :  TermPrinting(HDC hDC)                                     *
  157.  *                                                                          *
  158.  *  PURPOSE    :  Terminates print job.                                     *
  159.  *                                                                          *
  160.  ****************************************************************************/
  161. VOID PASCAL TermPrinting(HDC hDC)
  162. {
  163. //
  164. // Use new printing APIs...Petrus Wong 12-May-1993
  165. //
  166.     if (!bError)
  167.         EndDoc(hDC);
  168.  
  169.     if (bUserAbort)
  170.         AbortDoc(hDC);
  171.     else {
  172.         EnableWindow(hWndParent, TRUE);
  173.         DestroyWindow(hDlgPrint);
  174.     }
  175.  
  176.     FreeProcInstance(lpfnAbortProc);
  177.     FreeProcInstance(lpfnPrintDlgProc);
  178. }
  179. /****************************************************************************
  180.  *                                                                          *
  181.  *  FUNCTION   :PrintDlgProc (HWND, unsigned , WORD , DWORD )               *
  182.  *                                                                          *
  183.  *  PURPOSE    :Dialog function for the "Cancel Printing" dialog. It sets   *
  184.  *              the abort flag if the user presses <Cancel>.                *
  185.  *                                                                          *
  186.  ****************************************************************************/
  187. BOOL APIENTRY PrintDlgProc (HWND hDlg, WORD iMessage, UINT wParam, DWORD lParam)
  188. {
  189.     switch (iMessage) {
  190.     case WM_INITDIALOG:
  191.  
  192.             EnableMenuItem (GetSystemMenu (hDlg, FALSE), (WORD)SC_CLOSE, (WORD)MF_GRAYED);
  193.             break;
  194.  
  195.     case WM_COMMAND:
  196.             bUserAbort = TRUE;
  197.             EnableWindow (hWndParent, TRUE);
  198.             DestroyWindow (hDlg);
  199.             hDlgPrint = 0;
  200.             break;
  201.  
  202.     default:
  203.             return FALSE;
  204.     }
  205.     return TRUE;
  206.         UNREFERENCED_PARAMETER(wParam);
  207.         UNREFERENCED_PARAMETER(lParam);
  208. }
  209.  
  210. /****************************************************************************
  211.  *                                                                          *
  212.  *  FUNCTION   :AbortProc (HDC hPrnDC, short nCode)                         *
  213.  *                                                                          *
  214.  *  PURPOSE    :Checks message queue for messages from the "Cancel Printing"*
  215.  *              dialog. If it sees a message, (this will be from a print    *
  216.  *              cancel command), it terminates.                             *
  217.  *                                                                          *
  218.  *  RETURNS    :Inverse of Abort flag                                       *
  219.  *                                                                          *
  220.  ****************************************************************************/
  221. BOOL APIENTRY AbortProc (HDC hPrnDC, SHORT nCode)
  222. {
  223.     MSG   msg;
  224.  
  225.     while (!bUserAbort && PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {
  226.         if (!hDlgPrint || !IsDialogMessage(hDlgPrint, &msg)) {
  227.             TranslateMessage (&msg);
  228.             DispatchMessage (&msg);
  229.         }
  230.     }
  231.     return !bUserAbort;
  232.         UNREFERENCED_PARAMETER(hPrnDC);
  233.         UNREFERENCED_PARAMETER(nCode);
  234. }
  235.