home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / SAMPLES / FONTEST / PRINT.C_ / PRINT.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  3.8 KB  |  177 lines

  1. /***************************************************************************
  2.  * print.c
  3.  *
  4.  * routines used for printing
  5.  *
  6.  *
  7.  * NOTE: be sure to place these in your def file
  8.  *     AbortProc
  9.  *     PrintDlgProc
  10.  *
  11.  ***************************************************************************/
  12.  
  13. //Order of INCLUDES matters for compile time ... /YX
  14.  
  15. #include <windows.h>
  16. #include "print.h"
  17.  
  18. FARPROC lpfnAbortProc = NULL;
  19. FARPROC lpfnPrintDlgProc = NULL;
  20. HWND hDlgPrint = NULL;
  21. BOOL bError;
  22. BOOL bUserAbort;
  23. char szNull[] = "";
  24.  
  25.  
  26. BOOL FAR PASCAL __export AbortProc(HDC, short);
  27. BOOL FAR PASCAL __export PrintDlgProc(HWND, unsigned, WORD, DWORD);
  28.  
  29. #pragma alloc_text(_PRINT, AbortProc, PrintDlgProc)
  30.  
  31.  
  32. HDC PASCAL GetPrinterDC()
  33. {
  34.     char    msgbuf[128];
  35.     LPSTR     pch;
  36.     LPSTR     pchFile;
  37.     LPSTR     pchPort;
  38.  
  39.     if (!GetProfileString("windows", "device", szNull, msgbuf, sizeof(msgbuf)))
  40.         return NULL;
  41.  
  42.     for (pch = msgbuf; *pch && *pch != ','; pch = AnsiNext(pch));
  43.  
  44.     if (*pch)
  45.         *pch++ = 0;
  46.  
  47.     /* Skip tabs, control chars */
  48.     while (*pch && *pch <= ' ')
  49.         pch=AnsiNext(pch);
  50.  
  51.     pchFile = pch;
  52.     while (*pch && *pch != ',' && *pch > ' ')
  53.         pch = AnsiNext(pch);
  54.  
  55.     if (*pch)
  56.         *pch++ = 0;
  57.  
  58.     while (*pch && (*pch <= ' ' || *pch == ','))
  59.         pch = AnsiNext(pch);
  60.  
  61.     pchPort = pch;
  62.     while (*pch && *pch > ' ')
  63.         pch = AnsiNext(pch);
  64.  
  65.     *pch = 0;
  66.  
  67.     return CreateDC(pchFile, msgbuf, pchPort, NULL);
  68. }
  69.  
  70.  
  71.  
  72. BOOL PASCAL InitPrinting(HDC hdc, HWND hWnd, LPSTR msg)
  73. {
  74.     HANDLE hInst = GetWindowWord(hWnd, GWW_HINSTANCE);
  75.  
  76.     bError = FALSE;        /* no errors yet */
  77.     bUserAbort = FALSE;    /* user hasn't aborted */
  78.  
  79.     hWnd = hWnd;    /* save for Enable at Term time */
  80.  
  81.     lpfnPrintDlgProc = MakeProcInstance(PrintDlgProc, hInst);
  82.     lpfnAbortProc    = MakeProcInstance(AbortProc, hInst);
  83.  
  84.     hDlgPrint = CreateDialogParam(hInst, "PRTDLG", hWnd, lpfnPrintDlgProc, NULL);
  85.  
  86.     if (!hDlgPrint)
  87.         return FALSE;
  88.  
  89.     SetWindowText(hDlgPrint, msg);
  90.  
  91.     EnableWindow(hWnd, FALSE);    /* disable parent */
  92.  
  93.     if ((Escape(hdc, SETABORTPROC, 0, (LPSTR)lpfnAbortProc, NULL) > 0) &&
  94.         (Escape(hdc, STARTDOC, lstrlen(msg), msg, NULL) > 0))
  95.         bError = FALSE;
  96.     else
  97.         bError = TRUE;
  98.  
  99.     return !bError;
  100. }
  101.  
  102.  
  103. void PASCAL TermPrinting(HDC hdc, HWND hWnd)
  104. {
  105.     if (!bError) {
  106.         Escape(hdc, ENDDOC, 0, NULL, NULL);
  107.     }
  108.  
  109.     if (bUserAbort) {        /* already been done? */
  110.         Escape (hdc, ABORTDOC, 0, NULL, NULL) ;
  111.     } else {
  112.         EnableWindow(hWnd, TRUE);/* turn parent back on */
  113.         DestroyWindow(hDlgPrint);/* toast out dialog */
  114.     }
  115.  
  116.     FreeProcInstance(lpfnAbortProc);
  117.     FreeProcInstance(lpfnPrintDlgProc);
  118. }
  119.  
  120.  
  121.  
  122. /*
  123.  * PrintDlgProc()    printing dialog proc (has a cancle button on it)
  124.  *
  125.  * globals used:
  126.  *    szAppName    name of the app for dialog caption
  127.  *    bUserAbort    sets this when the user hits cancel
  128.  *    hDlgPrint
  129.  */
  130.  
  131. BOOL FAR PASCAL __export PrintDlgProc (HWND hDlg, unsigned iMessage, WORD wParam, DWORD lParam)
  132. {
  133.     switch (iMessage) {
  134.     case WM_INITDIALOG:
  135.  
  136.         EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
  137.         // SetWindowLong(hDlg, DWL_USER, lParam);
  138.         break;
  139.  
  140.     case WM_COMMAND:
  141.         // lpPrintData = GetWindowLong(hDlg, DWL_USER);
  142.         // lpPrintData->bUserAbort = TRUE;
  143.         bUserAbort = TRUE;
  144.         EnableWindow(GetParent(hDlg), TRUE);
  145.         DestroyWindow(hDlg);
  146.         hDlgPrint = 0;
  147.         break;
  148.  
  149.     default:
  150.         return FALSE;
  151.     }
  152.     return TRUE;
  153. }
  154.  
  155.  
  156. /*
  157.  * AbortProc()    printing abort procedure
  158.  *
  159.  * globals used:
  160.  *    bUserAbort     indicates the user hit CANCLE on the print dialog
  161.  *    hDlgPrint    handle of the print dialog
  162.  *
  163.  */
  164.  
  165. BOOL FAR PASCAL __export AbortProc (HDC hPrnDC, short nCode)
  166. {
  167.     MSG   msg;
  168.  
  169.     while (!bUserAbort && PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {
  170.         if (!hDlgPrint || !IsDialogMessage(hDlgPrint, &msg)) {
  171.             TranslateMessage (&msg);
  172.             DispatchMessage (&msg);
  173.         }
  174.     }
  175.     return !bUserAbort;
  176. }
  177.