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