home *** CD-ROM | disk | FTP | other *** search
/ BUG 4 / BUGCD1997_05.BIN / aplic / clip4win / clip4win.exe / C4W30E.HUF / SOURCE / PRINTDC.ZIP / PRINTDC.C < prev    next >
C/C++ Source or Header  |  1994-09-23  |  7KB  |  249 lines

  1.  
  2.  
  3. #include "c4w.h"
  4. #include <print.h>
  5.  
  6. int PrinterDC();
  7.  
  8. CLIPPER PRINTERDC()
  9.         {
  10.         char  *lpPrintType     = _parc( 1, 1 );
  11.         char  *lpPrintDriver   = _parc( 1, 2 );
  12.         char  *lpPrintPort     = _parc( 1, 3 );
  13.         int   nMode           = _parni( 2 );
  14.         int   aOptions[ 12 ];
  15.  
  16.         aOptions[  0 ] = _parni( 1,  7 );
  17.         aOptions[  1 ] = _parni( 1,  8 );
  18.         aOptions[  2 ] = _parni( 1,  9 );
  19.         aOptions[  3 ] = _parni( 1, 10 );
  20.         aOptions[  4 ] = _parni( 1, 11 );
  21.         aOptions[  5 ] = _parni( 1, 12 );
  22.         aOptions[  6 ] = _parni( 1, 13 );
  23.         aOptions[  7 ] = _parni( 1, 14 );
  24.         aOptions[  8 ] = _parni( 1, 15 );
  25.         aOptions[  9 ] = _parni( 1, 16 );
  26.         aOptions[ 10 ] = _parni( 1, 17 );
  27.         aOptions[ 11 ] = _parni( 1, 18 );
  28.  
  29.         _retni( PrinterDC( lpPrintType,
  30.                            lpPrintDriver,
  31.                            lpPrintPort,
  32.                            aOptions,
  33.                            nMode ) );
  34.         }
  35.  
  36. int PrinterDC( char *lpPrintType,
  37.                char *lpPrintDriver,
  38.                char *lpPrintPort,
  39.                int  nOptions[12],
  40.                int  nMode )
  41.     {
  42.     HANDLE        hDriver;
  43.     HANDLE        hDevMode;
  44.     LPDEVMODE     lpDevMode;
  45.     LPFNDEVMODE   lpfnExtDeviceMode;
  46.     HDC           hDC;
  47.     int           count;
  48.  
  49.     if ( ( hDriver = LoadLibrary( lpPrintDriver) ) < (HANDLE ) 32 )
  50.        {
  51.        MessageBox( 0, "Could Not Load Driver!", "PrintDC Error", MB_OK);
  52.        return( 0 );
  53.        }
  54.  
  55.     // Get a function pointer to the ExtDeviceMode() function.
  56.     // ExtDeviceMode() resides in the driver so we can't call it
  57.     // directly.
  58.  
  59.     lpfnExtDeviceMode = (LPFNDEVMODE) GetProcAddress(hDriver,(LPSTR) "EXTDEVICEMODE");
  60.  
  61.     if ( lpfnExtDeviceMode == NULL )
  62.        {
  63.        MessageBox( 0, "ExtDeviceMode Not Supported By Printer Driver!",
  64.                       "PrintDC Error", MB_OK);
  65.        FreeLibrary(hDriver);
  66.        return( 0 );
  67.        }
  68.  
  69.     // Get the number of bytes in the full DEVMODE buffer.
  70.     // This includes the device-dependent part at the end
  71.     // of the DEVMODE struct.
  72.  
  73.     count = lpfnExtDeviceMode(0,
  74.                               hDriver,
  75.                               NULL,
  76.                               lpPrintType,
  77.                               lpPrintPort,
  78.                               NULL,
  79.                               NULL,
  80.                               0);    // 0 = get buffer size
  81.  
  82.     if ( count == -1)
  83.        {
  84.        MessageBox( 0, "Invalid DEVMODE Size!", "PrintDC Error", MB_OK);
  85.        FreeLibrary( hDriver );
  86.        return( 0 );
  87.        }
  88.  
  89.     // Allocate storage for the DEVMODE buffer.
  90.  
  91.     hDevMode = GlobalAlloc(GHND, count);
  92.     if ( hDevMode == NULL )
  93.        {
  94.        MessageBox( 0, "Memory Allocation Failure!", "PrintDC Error", MB_OK);
  95.        FreeLibrary( hDriver );
  96.        return( 0 );
  97.        }
  98.  
  99.     lpDevMode = (LPDEVMODE)GlobalLock(hDevMode);
  100.  
  101.     // Get the current printer settings.
  102.  
  103.     count = lpfnExtDeviceMode(0,
  104.                               hDriver,
  105.                               lpDevMode,   // Output buffer
  106.                               lpPrintType,
  107.                               lpPrintPort,
  108.                               NULL,
  109.                               NULL,
  110.                               DM_OUT_BUFFER); // aka DM_COPY
  111.  
  112.  
  113.  
  114.     if( !( lpDevMode->dmFields & DM_ORIENTATION ) )
  115.          nOptions[ 0 ] = NIL;
  116.  
  117.     if( !( lpDevMode->dmFields & DM_PAPERSIZE ) )
  118.          nOptions[ 1 ] = NIL;
  119.  
  120.     if( !( lpDevMode->dmFields & DM_PAPERLENGTH ) )
  121.          nOptions[ 2 ] = NIL;
  122.  
  123.     if( !( lpDevMode->dmFields & DM_PAPERWIDTH ) )
  124.          nOptions[ 3 ] = NIL;
  125.  
  126.     if( !( lpDevMode->dmFields & DM_SCALE ) )
  127.          nOptions[ 4 ] = NIL;
  128.  
  129.     if( !( lpDevMode->dmFields & DM_COPIES ) )
  130.          nOptions[ 5 ] = NIL;
  131.  
  132.     if( !( lpDevMode->dmFields & DM_DEFAULTSOURCE ) )
  133.          nOptions[ 6 ] = NIL;
  134.  
  135.     if( !( lpDevMode->dmFields & DM_PRINTQUALITY ) )
  136.          nOptions[ 7 ] = NIL;
  137.  
  138.     if( !( lpDevMode->dmFields & DM_COLOR ) )
  139.          nOptions[ 8 ] = NIL;
  140.  
  141.     if( !( lpDevMode->dmFields & DM_DUPLEX ) )
  142.          nOptions[ 9 ] = NIL;
  143.  
  144.     if( !( lpDevMode->dmFields & DM_YRESOLUTION ) )
  145.         nOptions[ 10 ] = NIL;
  146.  
  147.     if( !( lpDevMode->dmFields & DM_TTOPTION ) )
  148.         nOptions[ 11 ] = NIL;
  149.  
  150.     lpDevMode->dmFields = 0;
  151.  
  152.     if( nOptions[ 0 ] != NIL )
  153.       {
  154.       lpDevMode->dmOrientation = nOptions[ 0 ];
  155.       lpDevMode->dmFields      = lpDevMode->dmFields | DM_ORIENTATION;
  156.       }
  157.  
  158.     if( nOptions[ 1 ] != NIL )
  159.       {
  160.       lpDevMode->dmPaperSize = nOptions[ 1 ];
  161.       lpDevMode->dmFields    = lpDevMode->dmFields | DM_PAPERSIZE;
  162.       }
  163.  
  164.     if( nOptions[ 2 ] != NIL )
  165.       {
  166.       lpDevMode->dmPaperLength = nOptions[ 2 ];
  167.       lpDevMode->dmFields      = lpDevMode->dmFields | DM_PAPERLENGTH;
  168.       }
  169.  
  170.     if( nOptions[ 3 ] != NIL )
  171.       {
  172.       lpDevMode->dmPaperWidth = nOptions[ 3 ];
  173.       lpDevMode->dmFields     = lpDevMode->dmFields | DM_PAPERWIDTH;
  174.       }
  175.  
  176.     if( nOptions[ 4 ] != NIL )
  177.       {
  178.       lpDevMode->dmScale  = nOptions[ 4 ];
  179.       lpDevMode->dmFields = lpDevMode->dmFields | DM_SCALE;
  180.       }
  181.  
  182.     if( nOptions[ 5 ] != NIL )
  183.       {
  184.       lpDevMode->dmCopies = nOptions[ 5 ];
  185.       lpDevMode->dmFields = lpDevMode->dmFields | DM_COPIES;
  186.       }
  187.  
  188.  
  189.     if( nOptions[ 6 ] != NIL )
  190.       {
  191.       lpDevMode->dmDefaultSource = nOptions[ 6 ];
  192.       lpDevMode->dmFields        = lpDevMode->dmFields | DM_DEFAULTSOURCE;
  193.       }
  194.  
  195.     if( nOptions[ 7 ] != NIL )
  196.       {
  197.       lpDevMode->dmPrintQuality = nOptions[ 7 ];
  198.       lpDevMode->dmFields       = lpDevMode->dmFields | DM_PRINTQUALITY;
  199.       }
  200.  
  201.     if( nOptions[ 8 ] != NIL )
  202.       {
  203.       lpDevMode->dmColor  = nOptions[ 8 ];
  204.       lpDevMode->dmFields = lpDevMode->dmFields | DM_COLOR;
  205.       }
  206.  
  207.     if( nOptions[ 9 ] != NIL )
  208.       {
  209.       lpDevMode->dmDuplex = nOptions[ 9 ];
  210.       lpDevMode->dmFields = lpDevMode->dmFields | DM_DUPLEX;
  211.       }
  212.  
  213.     if( nOptions[ 10 ] != NIL )
  214.       {
  215.       lpDevMode->dmYResolution = nOptions[ 10 ];
  216.       lpDevMode->dmFields      = lpDevMode->dmFields | DM_YRESOLUTION;
  217.       }
  218.  
  219.     if( nOptions[ 11 ] != NIL )
  220.       {
  221.       lpDevMode->dmTTOption = nOptions[ 11 ];
  222.       lpDevMode->dmFields   = lpDevMode->dmFields | DM_TTOPTION;
  223.       }
  224.  
  225.  
  226.  
  227.     // Call ExtDeviceMode() once more to allow the driver
  228.     // to change the device-dependent portion of the
  229.     // DEVMODE buffer if it needs to.
  230.  
  231.     if ( nMode == 0 )
  232.        nMode = DM_COPY | DM_MODIFY;
  233.  
  234.     count = lpfnExtDeviceMode(0,
  235.                               hDriver,
  236.                               lpDevMode,  // Output buffer
  237.                               lpPrintType,
  238.                               lpPrintPort,
  239.                               lpDevMode,  // Input buffer
  240.                               NULL,
  241.                               nMode);
  242.  
  243.     hDC = CreateDC(lpPrintDriver,lpPrintType,lpPrintPort, (void FAR*)lpDevMode);
  244.     GlobalUnlock(hDevMode);
  245.     GlobalFree(hDevMode);
  246.     FreeLibrary(hDriver);
  247.     return( (int ) hDC );
  248.     }
  249.