Annotation of mstools/samples/showdib/print.c, revision 1.1.1.2

1.1       root        1: /*******************************************************************************
                      2:  *                                                                            *
                      3:  *  MODULE     : Print.c                                                      *
                      4:  *                                                                            *
                      5:  *  DESCRIPTION : Routines used for printing.                                 *
                      6:  *                                                                            *
                      7:  *  FUNCTIONS  : GetPrinterDC()   - Gets default printer from WIN.INI and     *
                      8:  *                                  creates a DC for it.                      *
                      9:  *                                                                            *
                     10:  *               InitPrinting()   - Initializes print job.                    *
                     11:  *                                                                            *
                     12:  *               TermPrinting()   - Terminates print job.                     *
                     13:  *                                                                            *
                     14:  *               PrintDlgProc()   - Dialog function for the "Cancel Printing" *
                     15:  *                                  dialog.                                   *
                     16:  *                                                                            *
                     17:  *               AbortProc()      - Peeks at message queue for messages from  *
                     18:  *                                  the print dialog.                         *
                     19:  *                                                                            *
                     20:  *******************************************************************************/
                     21: 
                     22: #include <windows.h>
                     23: #include <string.h>
                     24: #include "showdib.h"
                     25: 
                     26: FARPROC  lpfnAbortProc   = NULL;
                     27: FARPROC  lpfnPrintDlgProc = NULL;
                     28: HWND    hWndParent       = NULL;
                     29: HWND    hDlgPrint        = NULL;
                     30: BOOL    bError;
                     31: BOOL    bUserAbort;
                     32: 
                     33: 
                     34: BOOL APIENTRY AbortProc (HDC, SHORT);
                     35: BOOL APIENTRY PrintDlgProc (HWND, WORD, UINT, DWORD);
                     36: 
                     37: /****************************************************************************
                     38:  *                                                                         *
                     39:  *  FUNCTION   : GetPrinterDC()                                            *
                     40:  *                                                                         *
                     41:  *  PURPOSE    : Read WIN.INI for default printer and create a DC for it.   *
                     42:  *                                                                         *
                     43:  *  RETURNS    : A handle to the DC if successful or NULL otherwise.       *
                     44:  *                                                                         *
                     45:  ****************************************************************************/
                     46: HDC PASCAL GetPrinterDC()
                     47: {
                     48:     static CHAR szPrinter [80];
                     49:     CHAR    *szDevice, *szDriver, *szOutput;
                     50: 
                     51:     GetProfileString ("windows", "device", "", szPrinter, sizeof(szPrinter));
                     52: 
                     53:     if ((szDevice = strtok (szPrinter, "," )) &&
                     54:        (szDriver = strtok (NULL,      ", ")) &&
                     55:        (szOutput = strtok (NULL,      ", ")))
                     56: 
                     57:        return CreateDC (szDriver, szDevice, szOutput, NULL) ;
                     58: 
                     59:     return NULL;
                     60: }
                     61: /****************************************************************************
                     62:  *                                                                         *
                     63:  *  FUNCTION   : InitPrinting(HDC hDC, HWND hWnd, HANDLE hInst, LPSTR msg)  *
                     64:  *                                                                         *
                     65:  *  PURPOSE    : Makes preliminary driver calls to set up print job.       *
                     66:  *                                                                         *
                     67:  *  RETURNS    : TRUE  - if successful.                                    *
                     68:  *              FALSE - otherwise.                                         *
                     69:  *                                                                         *
                     70:  ****************************************************************************/
                     71: BOOL PASCAL InitPrinting(HDC hDC, HWND hWnd, HANDLE hInst, LPSTR msg)
                     72: {
                     73: 
                     74:     bError     = FALSE;     /* no errors yet */
                     75:     bUserAbort = FALSE;     /* user hasn't aborted */
                     76: 
                     77:     hWndParent = hWnd;     /* save for Enable at Term time */
                     78: 
                     79: #ifdef WIN16
                     80:     lpfnPrintDlgProc = MakeProcInstance (PrintDlgProc, hInst);
                     81:     lpfnAbortProc    = MakeProcInstance (AbortProc, hInst);
                     82: #endif
1.1.1.2 ! root       83:     hDlgPrint = CreateDialog (hInst, "PRTDLG", hWndParent, lpfnPrintDlgProc);
1.1       root       84: 
                     85:     if (!hDlgPrint)
                     86:        return FALSE;
                     87: 
                     88:     SetWindowText (hDlgPrint, msg);
                     89:     EnableWindow (hWndParent, FALSE);       /* disable parent */
                     90: 
                     91:     if ((Escape (hDC, SETABORTPROC, 0, (LPSTR)lpfnAbortProc, NULL) > 0) &&
                     92:        (Escape (hDC, STARTDOC, lstrlen(msg), msg, NULL) > 0))
                     93:        bError = FALSE;
                     94:     else
                     95:        bError = TRUE;
                     96: 
                     97:     /* might want to call the abort proc here to allow the user to
                     98:      * abort just before printing begins */
                     99:     return TRUE;
                    100: }
                    101: /****************************************************************************
                    102:  *                                                                         *
                    103:  *  FUNCTION   :  TermPrinting(HDC hDC)                                    *
                    104:  *                                                                         *
                    105:  *  PURPOSE    :  Terminates print job.                                    *
                    106:  *                                                                         *
                    107:  ****************************************************************************/
                    108: VOID PASCAL TermPrinting(HDC hDC)
                    109: {
                    110:     if (!bError)
                    111:        Escape(hDC, ENDDOC, 0, NULL, NULL);
                    112: 
                    113:     if (bUserAbort)
                    114:        Escape (hDC, ABORTDOC, 0, NULL, NULL) ;
                    115:     else {
                    116:        EnableWindow(hWndParent, TRUE);
                    117:        DestroyWindow(hDlgPrint);
                    118:     }
                    119: 
                    120:     FreeProcInstance(lpfnAbortProc);
                    121:     FreeProcInstance(lpfnPrintDlgProc);
                    122: }
                    123: /****************************************************************************
                    124:  *                                                                         *
                    125:  *  FUNCTION   :PrintDlgProc (HWND, unsigned , WORD , DWORD )              *
                    126:  *                                                                         *
                    127:  *  PURPOSE    :Dialog function for the "Cancel Printing" dialog. It sets   *
                    128:  *             the abort flag if the user presses <Cancel>.                *
                    129:  *                                                                         *
                    130:  ****************************************************************************/
                    131: BOOL APIENTRY PrintDlgProc (HWND hDlg, WORD iMessage, UINT wParam, DWORD lParam)
                    132: {
                    133:     switch (iMessage) {
                    134:     case WM_INITDIALOG:
                    135: 
                    136:            EnableMenuItem (GetSystemMenu (hDlg, FALSE), (WORD)SC_CLOSE, (WORD)MF_GRAYED);
                    137:            break;
                    138: 
                    139:     case WM_COMMAND:
                    140:            bUserAbort = TRUE;
                    141:            EnableWindow (hWndParent, TRUE);
                    142:            DestroyWindow (hDlg);
                    143:            hDlgPrint = 0;
                    144:            break;
                    145: 
                    146:     default:
                    147:            return FALSE;
                    148:     }
                    149:     return TRUE;
                    150:        UNREFERENCED_PARAMETER(wParam);
                    151:        UNREFERENCED_PARAMETER(lParam);
                    152: }
                    153: 
                    154: /****************************************************************************
                    155:  *                                                                         *
                    156:  *  FUNCTION   :AbortProc (HDC hPrnDC, short nCode)                        *
                    157:  *                                                                         *
                    158:  *  PURPOSE    :Checks message queue for messages from the "Cancel Printing"*
                    159:  *             dialog. If it sees a message, (this will be from a print    *
                    160:  *             cancel command), it terminates.                             *
                    161:  *                                                                         *
                    162:  *  RETURNS    :Inverse of Abort flag                                      *
                    163:  *                                                                         *
                    164:  ****************************************************************************/
                    165: BOOL APIENTRY 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:        UNREFERENCED_PARAMETER(hPrnDC);
                    177:        UNREFERENCED_PARAMETER(nCode);
                    178: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.