#include "windows.h" #include <string.h> #define PRINTING // needed so constants are defined #ifndef _GLOBALINC #include "global.h" #endif #include <drivinit.h> // contains printing constants typedef unsigned int word; extern unsigned char venster_xrb, venster_ylo, venster_xlo; extern char scherm[50][99]; extern unsigned long kleurtbl[9]; PRINTINFOSTRUCT pis; extern HINSTANCE hInst;; BOOL ShowPrintSetup (HWND hWnd) { PRINTDLG pdPrint; pdPrint.lStructSize = sizeof(PRINTDLG); pdPrint.hwndOwner = hWnd; pdPrint.hDevMode = (HANDLE)pis.hCurrDevMode; pdPrint.hDevNames = (HANDLE)pis.hCurrDevNames; pdPrint.Flags = PD_PRINTSETUP; pdPrint.hInstance = (HINSTANCE)hInst; if (PrintDlg(&pdPrint)) { // set global handles pis.hCurrDevMode = pdPrint.hDevMode; pis.hCurrDevNames = pdPrint.hDevNames; return TRUE; } else { // set global handles pis.hCurrDevMode = pdPrint.hDevMode; pis.hCurrDevNames = pdPrint.hDevNames; return FALSE; } } //*** ShowPrintSetup BOOL ShowPrintBox (HWND hWnd) { PRINTDLG pdPrint; BOOL bResult; pdPrint.lStructSize = sizeof(PRINTDLG); pdPrint.hwndOwner = hWnd; pdPrint.hDevMode = (HANDLE)pis.hCurrDevMode; pdPrint.hDevNames = (HANDLE)pis.hCurrDevNames; pdPrint.nCopies = 1; pdPrint.Flags = PD_ALLPAGES | PD_NOPAGENUMS | PD_COLLATE | PD_NOSELECTION | PD_HIDEPRINTTOFILE; pdPrint.hInstance = (HINSTANCE)hInst; if (PrintDlg(&pdPrint)) { // set global handles pis.hCurrDevMode = pdPrint.hDevMode; pis.hCurrDevNames = pdPrint.hDevNames; // send printdlg info to printing routines bResult = PrintIt(hWnd, &pdPrint); return bResult; } else { // set global handles pis.hCurrDevMode = pdPrint.hDevMode; pis.hCurrDevNames = pdPrint.hDevNames; return FALSE; } } //*** ShowPrintBox HDC GetPrinterDC (void) { LPDEVMODE lpdmDevMode = NULL; LPDEVNAMES lpdnDevNames = NULL; // LPSTR lpstrDevNames = NULL; // lock handle to DEVMODE struct and send info to CreateDC // This causes a DC to be created that will have the same // DEVMODE attributes as what the user picked in the Print // Setup dialog. if (pis.hCurrDevMode != NULL) lpdmDevMode = (LPDEVMODE)GlobalLock(pis.hCurrDevMode); // lock handle to DEVNAMES struct and send info to CreateDC // This causes a DC to be created and linked to the device // (printer) that the user selected in the Print Setup // dialog. If hCurrDevNames is NULL then we create a DC // linked to the default printer. if (pis.hCurrDevNames != NULL) { lpdnDevNames = (LPDEVNAMES)GlobalLock(pis.hCurrDevNames); pis.szDevice = (LPSTR)lpdnDevNames + lpdnDevNames->wDeviceOffset; pis.szDriver = (LPSTR)lpdnDevNames + lpdnDevNames->wDriverOffset; pis.szOutput = (LPSTR)lpdnDevNames + lpdnDevNames->wOutputOffset; return CreateDC (pis.szDriver, pis.szDevice, pis.szOutput, (LPSTR)lpdmDevMode); } else { // create a DC that is linked to the default printer GetProfileString ("windows", "device", "", pis.szPrinter, 64) ; if ((pis.szDevice = (LPSTR)strtok (pis.szPrinter, "," )) && (pis.szDriver = (LPSTR)strtok (NULL, ", ")) && (pis.szOutput = (LPSTR)strtok (NULL, ", "))) return CreateDC (pis.szDriver, pis.szDevice, pis.szOutput, (LPSTR)lpdmDevMode); } return NULL; } //*** GetPrinterDC BOOL PrintIt (HWND hWnd, LPPRINTDLG lpPD) { HDC hPrnDC; TEXTMETRIC tm; DOCINFO di; FARPROC lpAbortProc, lpAbortDlg; HFONT hOldFont; int dxPrinter, dyPrinter, iPageNum, iCopyNum; char szBuffer[35]; int i; int dHeight, dWidth; HFONT hFont; HFONT hTmpfnt; LOGFONT hLogFont; ABC abc[26]; int yy, yp; // get a printer dc if ((hPrnDC = GetPrinterDC ()) == NULL) { MessageBox(hWnd, "Cannot create printer DC!", "ERROR", MB_APPLMODAL|MB_ICONSTOP|MB_OK); return FALSE; } // set user abort flag pis.bAbort = FALSE; // Initialize the abort procedure. lpAbortProc = MakeProcInstance((FARPROC)PrintAbortProc, hInst); lpAbortDlg = MakeProcInstance((FARPROC)PrintAbortDlg, hInst); // disable main application window EnableWindow(hWnd, FALSE); // display abort dialog pis.hDlgAbort = CreateDialog(hInst, "PrintDLG", hWnd, (FARPROC)lpAbortDlg); // set abort procedure if (SetAbortProc(hPrnDC, lpAbortProc) < 0) { MessageBox(NULL, "The SetAbortProc function failed.", "ERROR", MB_APPLMODAL|MB_ICONSTOP|MB_OK); DeleteDC(hPrnDC); return FALSE; } // need for StartDoc di.cbSize = sizeof(DOCINFO); di.lpszDocName = (LPSTR)"WinAtom"; di.lpszOutput = NULL; // issue STARTDOC if (StartDoc(hPrnDC, &di) < 0) { MessageBox(NULL, "STARTDOC escape problem", "ERROR", MB_APPLMODAL|MB_ICONSTOP|MB_OK); DeleteDC(hPrnDC); return FALSE; } // get page resolution dxPrinter = GetDeviceCaps(hPrnDC, HORZRES); dyPrinter = GetDeviceCaps(hPrnDC, VERTRES); dWidth = dxPrinter/ 64; //venster_xrb; dHeight = dyPrinter/ 16; //venster_ylo+1; // start copy hTmpfnt= CreateFont( -dHeight, 0, 0, // no sloping 0, // no escaping FW_NORMAL, // dik is in FALSE, // geen italic FALSE, // geen streep er onder FALSE, // geen doorhaling 0, OUT_RASTER_PRECIS, CLIP_STROKE_PRECIS, PROOF_QUALITY, FIXED_PITCH, "IBViewdata" ); hOldFont =(HFONT)SelectObject(hPrnDC, hTmpfnt); GetTextMetrics(hPrnDC, &tm); hLogFont.lfHeight = -tm.tmHeight; hLogFont.lfWidth = dWidth; hLogFont.lfWeight = tm.tmWeight; hLogFont.lfEscapement = 0; hLogFont.lfOrientation = 0; hLogFont.lfItalic = FALSE; hLogFont.lfUnderline = FALSE; hLogFont.lfStrikeOut = FALSE; hLogFont.lfCharSet = ANSI_CHARSET; hLogFont.lfOutPrecision = OUT_RASTER_PRECIS, hLogFont.lfClipPrecision = CLIP_STROKE_PRECIS, hLogFont.lfQuality = PROOF_QUALITY, hLogFont.lfPitchAndFamily = FIXED_PITCH, strcpy(hLogFont.lfFaceName, "IBViewdata"); hFont = CreateFontIndirect(&hLogFont); DeleteObject(hTmpfnt); SelectObject(hPrnDC, hFont); GetTextMetrics(hPrnDC, &tm); dWidth= tm.tmAveCharWidth; dHeight = tm.tmHeight - tm.tmInternalLeading; GetCharABCWidths(hPrnDC, (UINT) 'a', (UINT) 'z', (LPABC) &abc); dWidth = (abc['b' - 'a'].abcA + abc['b' - 'a'].abcB + abc['b' - 'a'].abcC); // einde copy // do the number of collated copies for (iCopyNum=1; iCopyNum <= (int)lpPD->nCopies; iCopyNum++) { for (iPageNum=1; iPageNum <2 && !pis.bAbort; iPageNum++) { // update abort dialog box page number wsprintf(szBuffer, "Now printing Page %d.", iPageNum); SetDlgItemText(pis.hDlgAbort, 100, (LPSTR)szBuffer); hOldFont = SelectObject(hPrnDC, hFont); // start printing a page StartPage(hPrnDC); // TextOut(hPrnDC, 0, 0, "Vier", 4); // SetBkColor(hPrnDC, kleurtbl[0]); // SetTextColor(hPrnDC, kleurtbl[7]); // SelectObject(hPrnDC, hFont); if (venster_ylo>24) venster_ylo=24; for (yy=0; yy<venster_ylo; yy++) { yp = yy * dHeight; wsprintf(szBuffer, "Printing line: %d", yy); SetDlgItemText(pis.hDlgAbort, 100, (LPSTR)szBuffer); TextOut(hPrnDC, 0, yp, scherm[yy], (venster_xrb-venster_xlo)); // TextOut(hPrnDC, 0, yp, "een", 3); } // */ // end the page; resets the dc to normal defaults EndPage(hPrnDC); } } // only do if user has not aborted if (!pis.bAbort) { if (EndDoc(hPrnDC) < 0) { // end the document MessageBox(NULL, "EndDoc function problem", "ERROR", MB_APPLMODAL|MB_ICONSTOP|MB_OK); return FALSE; } } // enable main application window EnableWindow(hWnd, TRUE); // get rid of abort dialog DestroyWindow(pis.hDlgAbort); // clean up FreeProcInstance(lpAbortProc); FreeProcInstance(lpAbortDlg); // deselect font, if changed if (hFont != NULL) { SelectObject(hPrnDC, hOldFont); DeleteObject(hFont); } DeleteDC(hPrnDC); return TRUE; } //*** PrintIt #pragma argsused BOOL FAR PASCAL PrintAbortProc (HDC hDC, short code) { MSG msg; while (!pis.bAbort && PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) if (!IsDialogMessage(pis.hDlgAbort, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (!pis.bAbort); } //*** PrintAbortProc #pragma argsused int FAR PASCAL PrintAbortDlg (HWND hWnd, unsigned msg, WORD wParam, LONG lParam) { char szMsgBuffer[64]; switch (msg) { case WM_INITDIALOG: SetFocus(hWnd); EnableMenuItem(GetSystemMenu(hWnd, FALSE), SC_CLOSE, MF_GRAYED); // set printing info to abort dialog SetDlgItemText(hWnd, 100, "Now printing Page 1 of 1"); wsprintf((LPSTR)szMsgBuffer, "'%.25s' on the", (LPSTR)"WinAtom"); SetDlgItemText(hWnd, 101, (LPSTR)szMsgBuffer); wsprintf((LPSTR)szMsgBuffer, "%.20s on %.20s",(LPSTR)pis.szDevice,(LPSTR)pis.szOutput); SetDlgItemText(hWnd, 102, (LPSTR)szMsgBuffer); return TRUE; case WM_COMMAND: switch (wParam) { case IDCANCEL: pis.bAbort = TRUE; return TRUE; } } return FALSE; } // PrintAbortDlg()