PRINT.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:10k
源码类别:

Windows编程

开发平台:

Visual C++

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