home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / SAMPLES / WMFDCODE / WMFPRINT.C_ / WMFPRINT.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  8.1 KB  |  313 lines

  1. /***********************************************************************
  2.  
  3.   MODULE     : WMFPRINT.C
  4.  
  5.   FUNCTIONS  : PrintWMF
  6.                GetPrinterDC
  7.                AbortProc
  8.                AbortDlg
  9.  
  10.   COMMENTS   :
  11.  
  12. ************************************************************************/
  13.  
  14. #include "windows.h"
  15. #include "wmfdcode.h"
  16.  
  17. /***********************************************************************
  18.  
  19.   FUNCTION   : PrintWMF
  20.  
  21.   PARAMETERS : void
  22.  
  23.   PURPOSE    : draw the metafile on a printer dc
  24.  
  25.   CALLS      : WINDOWS
  26.                  wsprintf
  27.                  MessageBox
  28.                  MakeProcInstance
  29.                  Escape
  30.                  CreateDialog
  31.                  SetMapMode
  32.                  SetViewportOrg
  33.                  SetViewportExt
  34.                  EnableWindow
  35.                  PlayMetaFile
  36.                  DestroyWindow
  37.                  DeleteDC
  38.  
  39.                APP
  40.                  WaitCursor
  41.                  GetPrinterDC
  42.                  SetPlaceableExts
  43.                  SetClipMetaExts
  44.  
  45.   MESSAGES   : none
  46.  
  47.   RETURNS    : BOOL - 0 if unable to print 1 if successful
  48.  
  49.   COMMENTS   :
  50.  
  51.   HISTORY    : 1/16/91 - created - drc - modification of code originally
  52.                contained in SDK sample app PRNTFILE
  53.  
  54. ************************************************************************/
  55.  
  56. BOOL PrintWMF()
  57. {
  58.   char str[50];
  59.  
  60.   /* display the hourglass cursor */
  61.   WaitCursor(TRUE);
  62.  
  63.   /* get a DC for the printer */
  64.   hPr = GetPrinterDC();
  65.  
  66.   /* if a DC could not be created then report the error and return */
  67.   if (!hPr) {
  68.     WaitCursor(FALSE);
  69.     wsprintf((LPSTR)str, "Cannot print %s", (LPSTR)fnameext);
  70.     MessageBox(hWndMain, (LPSTR)str, NULL, MB_OK | MB_ICONHAND);
  71.     return (FALSE);
  72.   }
  73.  
  74.   /* get a proc address for the abort dialog and procedure */
  75.   lpAbortDlg =  MakeProcInstance(AbortDlg, hInst);
  76.   lpAbortProc = MakeProcInstance(AbortProc, hInst);
  77.  
  78.   /* define the abort function */
  79.   Escape(hPr, SETABORTPROC, NULL,
  80.         (LPSTR) (long) lpAbortProc,
  81.         (LPSTR) NULL);
  82.  
  83.   /* start the print job */
  84.   if (Escape(hPr, STARTDOC, 4, "Metafile", (LPSTR) NULL) < 0)  {
  85.     MessageBox(hWndMain, "Unable to start print job",
  86.                NULL, MB_OK | MB_ICONHAND);
  87.     FreeProcInstance(lpAbortDlg);
  88.     FreeProcInstance(lpAbortProc);
  89.                         DeleteDC(hPr);
  90.   }
  91.  
  92.   /* clear the abort flag */
  93.   bAbort = FALSE;
  94.  
  95.   /* Create the Abort dialog box (modeless) */
  96.   hAbortDlgWnd = CreateDialog(hInst, "AbortDlg", hWndMain, lpAbortDlg);
  97.  
  98.   /* if the dialog was not created report the error */
  99.   if (!hAbortDlgWnd) {
  100.     WaitCursor(FALSE);
  101.     MessageBox(hWndMain, "NULL Abort window handle",
  102.                NULL, MB_OK | MB_ICONHAND);
  103.     return (FALSE);
  104.   }
  105.  
  106.   /* show Abort dialog */
  107.   ShowWindow (hAbortDlgWnd, SW_NORMAL);
  108.  
  109.   /* disable the main window to avoid reentrancy problems */
  110.   EnableWindow(hWndMain, FALSE);
  111.   WaitCursor(FALSE);
  112.  
  113.   /* if we are still committed to printing */
  114.   if (!bAbort) {
  115.  
  116.     /* if this is a placeable metafile then set its origins and extents */
  117.     if (bAldusMeta)
  118.         SetPlaceableExts(hPr, aldusMFHeader, WMFPRINTER);
  119.  
  120.     /* if this is a metafile contained within a clipboard file then set
  121.        its origins and extents accordingly */
  122.     if ( (bMetaInRam) && (!bAldusMeta) )
  123.         SetClipMetaExts(hPr, MFP, WMFPRINTER);
  124.  
  125.     /* if this is a "traditional" windows metafile */
  126.     if (!bMetaInRam)
  127.         {
  128.           SetMapMode(hPr, MM_TEXT);
  129.           SetViewportOrg(hPr, 0, 0);
  130.  
  131.           /* set the extents to the driver supplied values for horizontal
  132.              and vertical resolution */
  133.           SetViewportExt(hPr, GetDeviceCaps(hPr, HORZRES),
  134.                               GetDeviceCaps(hPr, VERTRES) );
  135.         }
  136.  
  137.     /* play the metafile directly to the printer.  No enumeration involved
  138.        here */
  139.     PlayMetaFile(hPr, hMF);
  140.   }
  141.  
  142.   /* end the print job */
  143.   Escape(hPr, NEWFRAME, 0, 0L, 0L);
  144.   Escape(hPr, ENDDOC, 0, 0L, 0L);
  145.  
  146.   EnableWindow(hWndMain, TRUE);
  147.  
  148.   /* destroy the Abort dialog box */
  149.   DestroyWindow(hAbortDlgWnd);
  150.  
  151.   FreeProcInstance(lpAbortDlg);
  152.   FreeProcInstance(lpAbortProc);
  153.  
  154.   DeleteDC(hPr);
  155.  
  156.   return(TRUE);
  157. }
  158.  
  159. /***********************************************************************
  160.  
  161.   FUNCTION   : GetPrinterDC
  162.  
  163.   PARAMETERS : void
  164.  
  165.   PURPOSE    : Get hDc for current device on current output port according
  166.                to info in WIN.INI.
  167.  
  168.   CALLS      : WINDOWS
  169.                  GetProfileString
  170.                  AnsiNext
  171.                  CreateDC
  172.  
  173.   MESSAGES   : none
  174.  
  175.   RETURNS    : HANDLE - hDC > 0 if success  hDC = 0 if failure
  176.  
  177.   COMMENTS   : Searches WIN.INI for information about what printer is
  178.                connected, and if found, creates a DC for the printer.
  179.  
  180.   HISTORY    : 1/16/91 - created - originally from the PRNTFILE sample
  181.                supplied with the SDK
  182.  
  183. ************************************************************************/
  184.  
  185. HANDLE GetPrinterDC()
  186. {
  187.   char pPrintInfo[80];
  188.   LPSTR lpTemp;
  189.   LPSTR lpPrintType;
  190.   LPSTR lpPrintDriver;
  191.   LPSTR lpPrintPort;
  192.  
  193.   /* get the current printer from the win.ini */
  194.   if (!GetProfileString("windows", "Device", (LPSTR)"", pPrintInfo, 80))
  195.       return (NULL);
  196.  
  197.   lpTemp = lpPrintType = pPrintInfo;
  198.   lpPrintDriver = lpPrintPort = 0;
  199.   while (*lpTemp) {
  200.       if (*lpTemp == ',') {
  201.           *lpTemp++ = 0;
  202.           while (*lpTemp == ' ')
  203.               lpTemp = AnsiNext(lpTemp);
  204.           if (!lpPrintDriver)
  205.               lpPrintDriver = lpTemp;
  206.           else {
  207.               lpPrintPort = lpTemp;
  208.               break;
  209.           }
  210.       }
  211.       else
  212.           lpTemp = AnsiNext(lpTemp);
  213.   }
  214.  
  215.   /* return a dc for the printer */
  216.   return (CreateDC(lpPrintDriver, lpPrintType, lpPrintPort, (LPSTR) NULL));
  217. }
  218.  
  219. /***********************************************************************
  220.  
  221.   FUNCTION   : AbortProc
  222.  
  223.   PARAMETERS : HDC hPr - printer DC
  224.                int Code - printing status
  225.  
  226.   PURPOSE    : process messages for the abort dialog box
  227.  
  228.   CALLS      : WINDOWS
  229.                  PeekMessage
  230.                  IsDialogMessage
  231.                  TranslateMessage
  232.                  DispatchMessage
  233.  
  234.   MESSAGES   : none
  235.  
  236.   RETURNS    : int
  237.  
  238.   COMMENTS   :
  239.  
  240.   HISTORY    : 1/16/91 - created - originally from the SDK sample app
  241.                PRNTFILE
  242.  
  243. ************************************************************************/
  244.  
  245. int FAR PASCAL __export AbortProc(hPr, Code)
  246. HDC hPr;
  247. int Code;
  248. {
  249.   MSG msg;
  250.  
  251.   /* Process messages intended for the abort dialog box */
  252.  
  253.   while (!bAbort && PeekMessage(&msg, NULL, NULL, NULL, TRUE))
  254.       if (!IsDialogMessage(hAbortDlgWnd, &msg)) {
  255.           TranslateMessage(&msg);
  256.           DispatchMessage(&msg);
  257.       }
  258.  
  259.   /* bAbort is TRUE (return is FALSE) if the user has aborted */
  260.  
  261.   return (!bAbort);
  262. }
  263.  
  264. /***********************************************************************
  265.  
  266.   FUNCTION   : AbortDlg
  267.  
  268.   PARAMETERS : HWND hDlg;
  269.                unsigned msg;
  270.                WORD wParam;
  271.                LONG lParam;
  272.  
  273.   PURPOSE    : Processes messages for printer abort dialog box
  274.  
  275.   CALLS      : WINDOWS
  276.                  SetFocus
  277.  
  278.   MESSAGES   : WM_INITDIALOG - initialize dialog box
  279.                WM_COMMAND    - Input received
  280.  
  281.   RETURNS    : int
  282.  
  283.   COMMENTS   : This dialog box is created while the program is printing,
  284.                and allows the user to cancel the printing process.
  285.  
  286.   HISTORY    : 1/16/91 - created - originally from the SDK sample app
  287.                PRNTFILE
  288.  
  289. ************************************************************************/
  290.  
  291. int FAR PASCAL __export AbortDlg(hDlg, msg, wParam, lParam)
  292. HWND hDlg;
  293. unsigned msg;
  294. WORD wParam;
  295. LONG lParam;
  296. {
  297.     switch(msg) {
  298.  
  299.         /* Watch for Cancel button, RETURN key, ESCAPE key, or SPACE BAR */
  300.  
  301.         case WM_COMMAND:
  302.             return (bAbort = TRUE);
  303.  
  304.         case WM_INITDIALOG:
  305.  
  306.             /* Set the focus to the Cancel box of the dialog */
  307.  
  308.             SetFocus(GetDlgItem(hDlg, IDCANCEL));
  309.             return (TRUE);
  310.         }
  311.     return (FALSE);
  312. }
  313.