home *** CD-ROM | disk | FTP | other *** search
- //*************************************************************
- // File name: PRNCDLG.c
- //
- // Description:
- //
- // WinMain and window procedure routines.
- //
- // This sample demonstrates the usage of new printing API available
- // in Windows 3.10. The sample demonstrates the SetAbortProc(),
- // StartDoc(), EndDoc(), StartPage(), and EndPage() printing API.
- //
- // The sample also demonstrates how to use the ChooseFont and Print
- // common dialogs. The sample allows the user to pick a font and
- // display a line of text using that font to the screen. Also, the
- // user can print out the same line of text to a printer selected
- // through the Print common dialog.
- //
- // Development Team:
- //
- // Don Miller
- //
- //
- // Written by Microsoft Product Support Services, Windows Developer Support
- // Copyright (c) 1992 Microsoft Corporation. All rights reserved.
- //*************************************************************
- #define PRINTING // needed so constants are defined
-
- #ifndef _GLOBALINC
- #include "global.h"
- #endif
- #include <string.h>
- #include <drivinit.h> // contains printing constants
-
- HANDLE ghInst = NULL;
- HWND ghWndMain = NULL;
-
- char szMainMenu[] = "MainMenu";
- char szMainClass[] = "PRNCDLGClass";
-
- //*************************************************************
- //
- // WinMain()
- //
- // Purpose:
- //
- // Entry point for all Windows applications.
- //
- //
- // Parameters:
- //
- // HANDLE hInstance - Handle to current instance
- // HANDLE hPrevInstance - Handle to previous instance
- // LPSTR lpCmdLine - Command-line arguments
- // int nCmdShow - How window is initially displayed
- //
- //
- // Return: (int PASCAL)
- //
- //
- // Comments:
- //
- //
- // History: Date Author Comment
- // 1/2/92 DAM Created
- //
- //*************************************************************
-
- int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- MSG msg;
-
- if (!hPrevInstance && !InitApplication(hInstance))
- return(FALSE);
-
- if (!InitInstance(hInstance, nCmdShow))
- return(FALSE);
-
- while (GetMessage(&msg, NULL, NULL, NULL))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return(msg.wParam);
-
- } //*** WinMain
-
-
- //*************************************************************
- //
- // MainWndProc()
- //
- // Purpose:
- //
- // Main Window procedure.
- //
- //
- // Parameters:
- //
- // HWND hWnd - Handle to main window
- // unsigned msg - Message passed to application
- // WORD wParam - Additional message information
- // LONG lParam - Additional message information
- //
- //
- // Return: (long FAR PASCAL)
- //
- //
- // Comments:
- //
- //
- // History: Date Author Comment
- // 1/2/92 DAM Created
- //
- //*************************************************************
-
- long FAR PASCAL MainWndProc (HWND hWnd, unsigned msg, WORD wParam, LONG lParam)
- {
- FARPROC lpProc;
-
- switch (msg)
- {
- case WM_CREATE:
- // set global handles to NULL
- pis.hCurrDevMode = NULL;
- pis.hCurrDevNames = NULL;
- pis.hPrinterFont = NULL;
-
- // set default text color to black
- sis.crTextColor = RGB(0,0,0);
- break;
-
- case WM_PAINT:
- {
- PAINTSTRUCT ps;
- int nxLogPix, nyLogPix, iOldMode;
- HFONT hOldFont;
- DWORD dwOldColor;
-
- BeginPaint(hWnd, &ps);
-
- // get logical pixels per inch
- nxLogPix = GetDeviceCaps(ps.hdc, LOGPIXELSX);
- nyLogPix = GetDeviceCaps(ps.hdc, LOGPIXELSY);
-
- // set so text background color is same as window backround color
- iOldMode = SetBkMode(ps.hdc, TRANSPARENT);
-
- // if a screen font has been made use it
- if (sis.hScreenFont != NULL)
- hOldFont = SelectObject(ps.hdc, sis.hScreenFont);
-
- // set text color to color selected by ChooseFont dialog
- dwOldColor = SetTextColor(ps.hdc, sis.crTextColor);
-
- // output a line of text to the screen
- TextOut(ps.hdc, nxLogPix, nyLogPix, "This a line of text", 19);
-
- // reset text color
- SetTextColor(ps.hdc, dwOldColor);
-
- // reset background mode back to previous mode
- SetBkMode(ps.hdc, iOldMode);
-
- // reselect old font back in to dc
- if (sis.hScreenFont != NULL)
- SelectObject(ps.hdc, hOldFont);
-
- EndPaint(hWnd, &ps);
- }
- break;
-
- case WM_COMMAND:
- switch ( wParam )
- {
- case IDM_FONTS:
- // show ChooseFont common dialog box
- ShowFontBox(hWnd);
- break;
-
- case IDM_PRINT:
- // show Print common dialog box
- ShowPrintBox(hWnd);
- break;
-
- case IDM_PRINTSETUP:
- // show Print Setup common dialog box
- ShowPrintSetup(hWnd);
- break;
-
- case IDM_EXIT:
- PostMessage( hWnd, WM_SYSCOMMAND, SC_CLOSE, 0L );
- break;
-
- case IDM_ABOUT:
- // display About dialog box
- lpProc = MakeProcInstance(About, ghInst);
- DialogBox(ghInst, "AboutBox", hWnd, lpProc);
- FreeProcInstance(lpProc);
- break;
- }
- break;
-
- case WM_DESTROY:
- // clean up printer font
- if (pis.hPrinterFont != NULL)
- DeleteObject(pis.hPrinterFont);
-
- // clean up screen font
- if (sis.hScreenFont != NULL)
- DeleteObject(sis.hScreenFont);
-
- // had to keep around until app is done using the info
- if (pis.hCurrDevMode != NULL)
- GlobalFree(pis.hCurrDevMode);
-
- // had to keep around until app is done using the info
- if (pis.hCurrDevNames != NULL)
- GlobalFree(pis.hCurrDevNames);
-
- PostQuitMessage(0);
- break;
- }
- return(DefWindowProc(hWnd, msg, wParam, lParam));
-
- } //*** MainWndProc
-
-
- //*************************************************************
- //
- // About()
- //
- // Purpose:
- //
- // The About dialog box procedure.
- //
- //
- // Parameters:
- //
- // HWND hDlg - Handle to dialog window
- // unsigned msg - Message passed to dialog window
- // WORD wParam - Additional message information
- // LONG lParam - Additional message information
- //
- //
- // Return: (BOOL FAR PASCAL)
- //
- // TRUE - About dialog procedure handled the message.
- // FALSE - About dialog procedure did not handle the message.
- //
- // Comments:
- //
- //
- // History: Date Author Comment
- // 1/2/92 DAM Created
- //
- //*************************************************************
-
- BOOL FAR PASCAL About (HWND hDlg, unsigned msg, WORD wParam, LONG lParam)
- {
- switch (msg)
- {
- case WM_INITDIALOG:
- return(TRUE);
-
- case WM_COMMAND:
- if (wParam == IDOK || wParam == IDCANCEL)
- {
- EndDialog(hDlg, TRUE);
- return(TRUE);
- }
- break;
- }
- return(FALSE); // Didn't process a message
-
- } //*** About
-
-
- //*************************************************************
- //
- // ShowPrintSetup
- //
- // Purpose:
- //
- // Function responsible for displaying Print Setup common
- // dialog box. Stores a handle to the current DEVMODE and a
- // handle to the current DEVNAMES in a global PRINTINFOSTRUCT.
- //
- // Parameters:
- // HWND hWnd
- //
- //
- // Return: (BOOL)
- //
- //
- // Comments:
- //
- //
- // History: Date Author Comment
- // 1/6/92 DAM Created
- //*************************************************************
-
- 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 = (HANDLE)ghInst;
-
- 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
-
-
- //*************************************************************
- //
- // ShowFontBox
- //
- // Purpose:
- //
- // Function is responsible for displaying ChooseFont common
- // dialog. Also, the function creates a printer font and a
- // screen font and stores them in their respective global
- // structures.
- //
- // Parameters:
- // HWND hWnd
- //
- //
- // Return: (void)
- //
- //
- // Comments:
- //
- //
- // History: Date Author Comment
- // 1/6/92 DAM Created
- //*************************************************************
-
- void ShowFontBox (HWND hWnd)
- {
- CHOOSEFONT cf;
- HDC hPrnDC;
- LOGFONT lfTemp;
- int nyLogPix;
-
- if ((hPrnDC = GetPrinterDC ()) != NULL)
- {
- cf.lStructSize = sizeof(CHOOSEFONT);
- cf.hwndOwner = hWnd;
- cf.hDC = hPrnDC;
- cf.lpLogFont = &pis.lfCurrFont;
- cf.Flags = CF_WYSIWYG | CF_BOTH | CF_EFFECTS | CF_SCALABLEONLY | CF_INITTOLOGFONTSTRUCT;
- cf.lCustData = 0L;
- cf.rgbColors = sis.crTextColor;
- cf.lpfnHook = (FARPROC)NULL;
- cf.lpTemplateName = (LPSTR)NULL;
- cf.hInstance = ghInst;
- cf.lpszStyle = (LPSTR)NULL;
- cf.nFontType = PRINTER_FONTTYPE;
- cf.nSizeMin = 0;
- cf.nSizeMax = 0;
-
-
- if (ChooseFont(&cf))
- {
- // get rid of old screen font, if one
- if (sis.hScreenFont != NULL)
- {
- DeleteObject(sis.hScreenFont);
- sis.hScreenFont = NULL;
- }
- // store text color for screen
- sis.crTextColor = (COLORREF)cf.rgbColors;
-
- //create screen font
- sis.hScreenFont = CreateFontIndirect(&pis.lfCurrFont);
-
- // create printer font
- // use temp logfont because global logfont is for screen */
- lfTemp = pis.lfCurrFont;
-
- // adjust font height for correct point size on printer */
- nyLogPix = GetDeviceCaps(hPrnDC, LOGPIXELSY);
- lfTemp.lfHeight = -((cf.iPointSize/10) * nyLogPix)/72;
-
- // get rid of old printer font, if one
- if (pis.hPrinterFont != NULL)
- {
- DeleteObject(pis.hPrinterFont);
- pis.hPrinterFont = NULL;
- }
- pis.hPrinterFont = CreateFontIndirect(&lfTemp);
-
- // redraw the client
- InvalidateRect(hWnd, NULL, TRUE);
- }
- DeleteDC(hPrnDC);
- }
- } //*** ShowFontBox
-
-
- //*************************************************************
- //
- // ShowPrintBox
- //
- // Purpose:
- //
- // Function responsible for displaying Print common
- // dialog box. Stores a handle to the current DEVMODE and a
- // handle to the current DEVNAMES in a global PRINTINFOSTRUCT.
- //
- // Parameters:
- // HWND hWnd
- //
- //
- // Return: (BOOL)
- //
- //
- // Comments:
- //
- //
- // History: Date Author Comment
- // 1/6/92 DAM Created
- //*************************************************************
-
- 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 = (HANDLE)ghInst;
-
- 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
-
-
- //*************************************************************
- //
- // GetPrinterDC
- //
- // Purpose:
- //
- // Function that retrieves a printer dc. Retrieves a dc with
- // respect to the data in the current DEVMODE and DEVNAMES.
- //
- // Parameters:
- // void
- //
- //
- // Return: (HDC)
- //
- //
- // Comments:
- //
- //
- // History: Date Author Comment
- // 1/6/92 DAM Created
- //*************************************************************
-
- 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
-
-
- //*************************************************************
- //
- // PrintIt
- //
- // Purpose:
- //
- // Function that handles all the printing. Manual banding is
- // not implemented.
- //
- // Parameters:
- // HWND hWnd
- //
- //
- // Return: (BOOL)
- //
- //
- // Comments:
- //
- //
- // History: Date Author Comment
- // 1/6/92 DAM Created
- //*************************************************************
-
- BOOL PrintIt (HWND hWnd, LPPRINTDLG lpPD)
- {
- HDC hPrnDC;
- DOCINFO di;
- FARPROC lpAbortProc, lpAbortDlg;
- HFONT hOldFont;
- int nxLogPix, nyLogPix, dxPrinter, dyPrinter, iPageNum, iCopyNum;
- char szBuffer[35];
-
- // 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, ghInst);
- lpAbortDlg = MakeProcInstance((FARPROC)PrintAbortDlg, ghInst);
-
- // disable main application window
- EnableWindow(hWnd, FALSE);
-
- // display abort dialog
- pis.hDlgAbort = CreateDialog(ghInst, "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)"PRNCDLG";
- 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 logical pixels per inch
- nxLogPix = GetDeviceCaps(hPrnDC, LOGPIXELSX);
- nyLogPix = GetDeviceCaps(hPrnDC, LOGPIXELSY);
-
- // get page resolution
- dxPrinter = GetDeviceCaps(hPrnDC, HORZRES);
- dyPrinter = GetDeviceCaps(hPrnDC, VERTRES);
-
- // 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 of", iPageNum);
- SetDlgItemText(pis.hDlgAbort, 100, (LPSTR)szBuffer);
-
- // select font in
- if (pis.hPrinterFont != NULL)
- hOldFont = SelectObject(hPrnDC, pis.hPrinterFont);
-
- // start printing a page
- StartPage(hPrnDC);
-
- // write something to printer
- wsprintf(szBuffer, "This is a line of text on page %d", iPageNum);
- TextOut(hPrnDC, nxLogPix, nyLogPix, szBuffer, strlen(szBuffer));
-
- // 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 (pis.hPrinterFont != NULL)
- SelectObject(hPrnDC, hOldFont);
-
- DeleteDC(hPrnDC);
- } //*** PrintIt
-
-
- // -------------------- Abort Routines --------------------
-
-
- //*************************************************************
- //
- // PrintAbortProc
- //
- // Purpose:
- //
- // Function is used to give application some "multi-tasking"
- // through the use of a PeekMessage(). Also, it gives the
- // application some error control during printing.
- //
- // Parameters:
- // HDC hDC
- // short code
- //
- //
- // Return: (BOOL FAR PASCAL)
- //
- //
- // Comments:
- //
- //
- // History: Date Author Comment
- // 1/6/92 DAM Created
- //*************************************************************
-
- 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
-
-
- //*************************************************************
- //
- // PrintAbortDlg
- //
- // Purpose:
- //
- // Dialog procedure for print abort dialog.
- //
- // Parameters:
- // HWND hWnd
- // unsigned msg
- // WORD wParam
- // LONG lParam
- //
- //
- // Return: (int FAR PASCAL)
- //
- //
- // Comments:
- //
- //
- // History: Date Author Comment
- // 1/6/92 DAM Created
- //*************************************************************
-
- 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");
-
- wsprintf((LPSTR)szMsgBuffer, "'%.25s' on the", (LPSTR)"PRNCDLG");
- 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:
- pis.bAbort = TRUE;
- return TRUE;
- }
-
- return FALSE;
- } // PrintAbortDlg()
-
- /*** EOF: PRNCDLG.c ***/