home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / porttool / ptprint.c < prev    next >
C/C++ Source or Header  |  1996-06-12  |  8KB  |  307 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples.
  4. *       Copyright (C) 1993-96 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.  
  12. #include "PortTool.h"
  13.  
  14. HWND    hCancelDlg = 0;
  15. PRINTDLG    pdPrint;
  16.  
  17.  
  18. /* get default printer configuration and save in hWnd extra bytes for use later */
  19. BOOL WINAPI GetPrinterConfig (
  20.     HWND    hWnd)
  21. {
  22.     pdPrint.lStructSize = sizeof (PRINTDLG);
  23.     pdPrint.Flags        = PD_RETURNDEFAULT;
  24.     pdPrint.hwndOwner    = hWnd;
  25.     pdPrint.hDevMode    = NULL;
  26.     pdPrint.hDevNames    = NULL;
  27.     pdPrint.hDC         = NULL;
  28.  
  29.     PrintDlg (&pdPrint);
  30.  
  31.     SetWindowLong (hWnd, WL_HPTRDEVNAMES, (LONG) pdPrint.hDevNames);
  32.  
  33.     return TRUE;
  34. }
  35.  
  36.  
  37.  
  38. /* abort proc called by gdi during print download process */
  39. int WINAPI AbortProc (
  40.     HDC     hdc,
  41.     int     nErr)
  42. {
  43.     BOOL    fContinue = TRUE;
  44.     MSG     msg;
  45.  
  46.     /* process messages for cancel dialog and other apps */
  47.     while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  48.     {
  49.     if (msg.message == UM_CANCELPRINT)
  50.         {
  51.         fContinue = FALSE;
  52.         break;
  53.         }
  54.  
  55.     else if (!hCancelDlg || !IsDialogMessage (hCancelDlg, &msg))
  56.         {
  57.         TranslateMessage (&msg);
  58.         DispatchMessage  (&msg);
  59.         }
  60.     }
  61.  
  62.     return fContinue;
  63. }
  64.  
  65.  
  66.  
  67.  
  68. BOOL WINAPI CancelDlgProc (
  69.     HWND    hWnd,
  70.     UINT    uMsg,
  71.     UINT    uParam,
  72.     LONG    lParam)
  73. {
  74.  
  75.     switch (uMsg)
  76.     {
  77.     case WM_INITDIALOG:
  78.         {
  79.         char    *lpdn;
  80.         char    lpszTitle[MAX_PATH];
  81.         HWND    hWndOwner = GetWindow(hWnd, GW_OWNER);
  82.  
  83.         /* initialize dialog control information */
  84.         lpdn = LocalLock (pdPrint.hDevNames);
  85.  
  86.         SetDlgItemText (hWnd,
  87.                 IDC_PRINTDEVICE,
  88.                 lpdn + sizeof (DEVNAMES) +
  89.                 ((DEVNAMES *)lpdn)->wDeviceOffset);
  90.         SetDlgItemText (hWnd,
  91.                 IDC_PRINTPORT,
  92.                 lpdn + sizeof (DEVNAMES) +
  93.                 ((DEVNAMES *)lpdn)->wOutputOffset);
  94.         LocalUnlock (pdPrint.hDevNames);
  95.         GetWindowText (hWndOwner, lpszTitle, sizeof (lpszTitle));
  96.         SetDlgItemText (hWnd, IDC_PRINTTITLE, lpszTitle);
  97.         }
  98.         break;
  99.  
  100.     case WM_COMMAND:
  101.         /* if cancel button selected, post message to cancel print job */
  102.         if (LOWORD (uParam) == IDCANCEL)
  103.         {
  104.         PostMessage (GetParent (hWnd), UM_CANCELPRINT, 0, 0);
  105.         DestroyWindow (hWnd);
  106.         }
  107.         break;
  108.  
  109.     default:
  110.         return FALSE;
  111.     }
  112.     return TRUE;
  113. }
  114.  
  115.  
  116.  
  117. /* put up the print common dialog, and print */
  118. int WINAPI PrintFile (
  119.     HWND    hWnd)
  120. {
  121.     char    *lpEditData;
  122.     HANDLE    hEditData;
  123.     SIZE    sLine;
  124.     int     yLineExt;
  125.     int     yExt;
  126.     int     yPageExt;
  127.     UINT    uLine;
  128.     UINT    uNumLines;
  129.     UINT    uOffset;
  130.     UINT    uLineLen;
  131.     UINT    uTitleLen;
  132.     HWND    hWndEdit;
  133.     // PRINTDLG    pdPrint;
  134.     DOCINFO    diPrint;
  135.     char    lpszJobName[MAX_PATH];
  136.     HFONT    hFont;
  137.     HFONT    hFontPrt = NULL;
  138.     int        nPageCnt = 1;
  139.  
  140.  
  141.     /* call common print dialog to get initialized printer DC */
  142.     pdPrint.hwndOwner = hWnd;
  143.     pdPrint.hDC = NULL;
  144.     pdPrint.Flags = PD_RETURNDC ;
  145.  
  146.  
  147.     /* call common print dialog */
  148.     if (!PrintDlg (&pdPrint))
  149.     return IDS_PTRCOMMDLGFAILED;
  150.  
  151.     /* start cancel dialog box */
  152.     hCancelDlg = CreateDialog ((HANDLE)GetModuleHandle (NULL),
  153.                    IDD_CANCELDLG,
  154.                    hWnd,
  155.                    CancelDlgProc);
  156.  
  157.  
  158.     if (!hCancelDlg)
  159.     return IDS_CANCELDLGFAILED;
  160.  
  161.     ShowWindow (hCancelDlg, SW_SHOW);
  162.     UpdateWindow (hCancelDlg);
  163.  
  164.     /* set AbortProc callback */
  165.     if (SetAbortProc (pdPrint.hDC, (PROC)AbortProc) < 0)
  166.     {
  167.     /* on error, clean up and go away */
  168.     DestroyWindow (hCancelDlg);
  169.     DeleteDC (pdPrint.hDC);
  170.     return IDS_SETABORTPROCFAILED;
  171.     }
  172.  
  173.     hWndEdit = (HWND)GetWindowLong (hWnd, WL_HWNDEDIT);
  174.     if (NULL == (hFont = (HFONT)SendMessage(hWndEdit, WM_GETFONT, 0, 0L))) {
  175.         hFont = GetStockObject(SYSTEM_FONT);
  176.     } else {
  177.         HDC     hdcScreen;
  178.         LOGFONT lf;
  179.  
  180.         if (NULL != (hdcScreen = GetDC(hWnd))) {
  181.             if(GetObject(hFont, sizeof(LOGFONT), &lf)) {
  182.                 lf.lfWidth = 0;
  183.                 lf.lfHeight = lf.lfHeight
  184.                             * GetDeviceCaps(pdPrint.hDC, LOGPIXELSX)
  185.                             / GetDeviceCaps(hdcScreen, LOGPIXELSX);
  186.                 if (NULL != (hFontPrt = CreateFontIndirect(&lf))) {
  187.                     hFont = hFontPrt;
  188.                 }
  189.             }
  190.             ReleaseDC(hWnd, hdcScreen);
  191.         }
  192.     }
  193.     hFont = SelectObject(pdPrint.hDC, hFont);
  194.  
  195.     /* initialize printer for job */
  196.     GetWindowText (hWnd, lpszJobName, sizeof (lpszJobName));
  197.     diPrint.cbSize = sizeof (DOCINFO);
  198.     diPrint.lpszDocName = lpszJobName;
  199.     diPrint.lpszOutput = NULL;
  200.     if (StartDoc (pdPrint.hDC, &diPrint) == SP_ERROR ||  StartPage (pdPrint.hDC) < 0)
  201.     {
  202.     /* on error, clean up and go away */
  203.     DestroyWindow (hCancelDlg);
  204.     SelectObject(pdPrint.hDC, hFont);
  205.         if (hFontPrt) {
  206.         DeleteObject(hFontPrt);
  207.         }
  208.     DeleteDC (pdPrint.hDC);
  209.     return IDS_STARTDOCFAILED;
  210.     }
  211.  
  212.     /* job started, so display cancel dialog */
  213.     ShowWindow (hCancelDlg, SW_SHOW);
  214.     UpdateWindow (hCancelDlg);
  215.  
  216.     /* retrieve dimensions for printing and init loop variables */
  217.     /* 'hWndEdit' has already been got. */
  218.     hWndEdit = (HWND)GetWindowLong(hWnd, WL_HWNDEDIT);
  219.     hEditData = (HANDLE)SendMessage (hWndEdit, EM_GETHANDLE, 0, 0L);
  220.     uNumLines = (WORD)SendMessage (hWndEdit, EM_GETLINECOUNT, 0, 0L);
  221.     GetTextExtentPoint (pdPrint.hDC, "CC", 2, &sLine);
  222.     yLineExt = sLine.cy;
  223.     yPageExt = GetDeviceCaps (pdPrint.hDC, VERTRES);
  224.     yExt = 0;
  225.     uLine = 0;
  226.     if (pdPrint.Flags & PD_PAGENUMS) {
  227.     uLine = (pdPrint.nFromPage - 1) * (yPageExt / yLineExt);
  228.     }
  229.  
  230.     /* print text line by line from top to bottom */
  231.     while (uLine < uNumLines)
  232.     {
  233.     if ((pdPrint.Flags & PD_PAGENUMS) &&
  234.         (uLine >= (UINT)(pdPrint.nToPage * (yPageExt / yLineExt)))) {
  235.         break;
  236.     }
  237.     /* if at end of page, start a new page */
  238.     if ((yExt + yLineExt) > yPageExt)
  239.         {
  240.         if (!EndPage (pdPrint.hDC) || StartPage (pdPrint.hDC) < 0)
  241.         {
  242.         DestroyWindow (hCancelDlg);
  243.         SelectObject(pdPrint.hDC, hFont);
  244.                 if (hFontPrt) {
  245.                     DeleteObject(hFontPrt);
  246.                 }
  247.         DeleteDC (pdPrint.hDC);
  248.         return IDS_PRINTABORTED;
  249.         }
  250.         yExt = 0;
  251.         }
  252.  
  253.     /* determine buffer offset for current line and line length */
  254.     uOffset = SendMessage (hWndEdit, EM_LINEINDEX, uLine, 0);
  255.     uLineLen = SendMessage (hWndEdit, EM_LINELENGTH, uOffset, 0);
  256.  
  257. #if defined (WIN32)
  258.     uTitleLen=GetWindowTextLength(hWndEdit);
  259.     hEditData=LocalAlloc(LHND,uTitleLen+1); 
  260.     lpEditData=LocalLock(hEditData); 
  261.     GetWindowText(hWndEdit, lpEditData, uTitleLen+1);
  262. #else
  263.     hEditData = (HANDLE)SendMessage (hWndEdit, EM_GETHANDLE, 0, 0L);
  264.     lpEditData = LocalLock (hEditData);
  265. #endif
  266.  
  267.     /* print current the line and unlock the text handle */
  268.     TextOut (pdPrint.hDC, 0, yExt, (char *)lpEditData+uOffset, uLineLen);
  269.     LocalUnlock (hEditData);
  270.  
  271.     /* increment page position */
  272.     yExt += yLineExt;
  273.     uLine++;
  274.     }
  275.  
  276.     /* end the last page and document */
  277.     EndPage (pdPrint.hDC);
  278.     EndDoc (pdPrint.hDC);
  279.  
  280.     SelectObject(pdPrint.hDC, hFont);
  281.     if (hFontPrt) {
  282.         DeleteObject(hFontPrt);
  283.     }
  284.     /* end cancel dialog box, clean up and exit */
  285.     DestroyWindow (hCancelDlg);
  286.     DeleteDC(pdPrint.hDC);
  287.     return TRUE;
  288. }
  289.  
  290.  
  291.  
  292. /* printer setup common dialog */
  293. int WINAPI PrinterSetup (
  294.     HWND    hWnd)
  295. {
  296.     // PRINTDLG    pdPrint;
  297.  
  298.     pdPrint.Flags        = PD_PRINTSETUP;
  299.     pdPrint.hwndOwner    = hWnd;
  300.  
  301.     /* call common print dialog */
  302.     if (!PrintDlg (&pdPrint) && CommDlgExtendedError ())
  303.         return IDS_PTRCOMMDLGFAILED;
  304.     else
  305.         return 0;
  306. }
  307.