home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / gdi / mandel / printer.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  5KB  |  168 lines

  1. /******************************Module*Header*******************************\
  2. * Module Name: printer.c
  3. *
  4. * Contains functions for enermerating printers
  5. *
  6. * Created: 16-Apr-1992 11:19:00
  7. *
  8. * Copyright (C) 1993-1997 Microsoft Corporation
  9. *
  10. * Before printing the bInitPrinter is called for enumerating printers
  11. * and doing the setup for printing
  12. *
  13. * When Mandelbrot Dream exits, bCleanupPrinter is called to free up
  14. * memory
  15. *
  16. * Dependencies:
  17. *
  18. *   (#defines)
  19. *   (#includes)
  20. *
  21. \**************************************************************************/
  22. #include <windows.h>
  23. #include <winspool.h>
  24. #include <drivinit.h>
  25. #include "julia.h"
  26. #include "printer.h"
  27.  
  28. //
  29. // Globals for printing
  30. //
  31. PPRINTER_INFO_1     gpPrinters       = NULL;
  32. PSZ                *gpszPrinterNames = NULL;
  33. PSZ                *gpszDeviceNames  = NULL;
  34.  
  35. extern HMENU  hPrinterMenu;
  36. extern INT    giNPrinters;
  37. extern HWND   ghwndMain;
  38.  
  39. BOOL bInitPrinter(HWND);
  40. BOOL bCleanupPrinter(VOID);
  41.  
  42.  
  43. /******************************Public*Routine******************************\
  44. *
  45. * bInitPrinter
  46. *
  47. * Effects: Enumerating printers...
  48. *
  49. * Warnings: Globals alert!!
  50. *
  51. \**************************************************************************/
  52.  
  53. BOOL bInitPrinter(HWND hwnd) {
  54.     BOOL        bSuccess;
  55.     DWORD       cbPrinters;
  56.     DWORD       cbNeeded, cReturned, j;
  57.     int         i;
  58.  
  59.  
  60.     bSuccess = TRUE;
  61.     cbPrinters = 4096L;
  62.     
  63.     if (!(gpPrinters = (PPRINTER_INFO_1)LocalAlloc((LMEM_FIXED | LMEM_ZEROINIT),
  64.                                                   cbPrinters)))
  65.     {
  66.         OutputDebugString( "InitPrint: LocalAlloc for gpPrinters failed.");
  67.         return (FALSE);
  68.     }
  69.  
  70.     if (!EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, 1, (LPBYTE)gpPrinters,
  71.                       cbPrinters, &cbNeeded, &cReturned))
  72.     {
  73.         if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) 
  74.         {
  75.             LocalFree((LOCALHANDLE)gpPrinters);
  76.             gpPrinters = (PPRINTER_INFO_1)LocalAlloc((LMEM_FIXED | LMEM_ZEROINIT),
  77.                                                cbNeeded);
  78.             cbPrinters = cbNeeded;
  79.  
  80.             if (!EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, 1, (LPBYTE)gpPrinters,
  81.                               cbPrinters, &cbNeeded, &cReturned))
  82.             {
  83.                 MessageBox(ghwndMain, 
  84.                            GetStringRes (IDS_ERR_CANT_ENUM_PRINTERS),
  85.                            NULL, MB_OK);
  86.                 return (FALSE);
  87.             }
  88.  
  89.         } 
  90.         else 
  91.         {
  92.             MessageBox(ghwndMain, 
  93.                        GetStringRes (IDS_ERR_CANT_ENUM_PRINTERS),
  94.                        NULL, MB_OK);
  95.             return (FALSE);
  96.         }
  97.     }
  98.  
  99.     // allocate some memory.
  100.  
  101.     gpszPrinterNames = (PSZ *)LocalAlloc((LMEM_FIXED | LMEM_ZEROINIT),
  102.                                         cReturned * (DWORD)sizeof(PSZ));
  103.  
  104.     gpszDeviceNames = (PSZ *)LocalAlloc((LMEM_FIXED | LMEM_ZEROINIT),
  105.                                         cReturned * (DWORD)sizeof(PSZ));
  106.  
  107.     if (giNPrinters != 0) {
  108.         for (i = 0; i < giNPrinters; i++) {
  109.             RemoveMenu(hPrinterMenu, 3, MF_BYPOSITION);
  110.         }
  111.         giNPrinters = 0;
  112.     }
  113.  
  114.     // insert each printer name into the menu.
  115.  
  116.     j = giNPrinters = cReturned;
  117.     for (i = 0; i < (INT) cReturned; i++)
  118.     {
  119.         // insert into menu from bottom up.
  120.  
  121.         j--;        
  122.         InsertMenu(hPrinterMenu, 4, MF_BYCOMMAND | MF_STRING,
  123.                    MM_PRINTER + i, (LPSTR)gpPrinters[j].pName);
  124.  
  125.         // save a list of printer names, so we can associate them
  126.         // with their menu indices later.
  127.  
  128.         gpszPrinterNames[i] = gpPrinters[j].pName;
  129.         gpszDeviceNames[i] = gpPrinters[j].pDescription;
  130.     }
  131. #if 0
  132.     //
  133.     // Use this if this is called in the MDI child instead
  134.     //
  135.     DrawMenuBar(GetParent(GetParent(hwnd)));
  136. #endif
  137.     //
  138.     // Use this instead if this is called in InitializeApp
  139.     //
  140.     DrawMenuBar(hwnd);
  141.     return (bSuccess);
  142. }
  143.  
  144.  
  145.  
  146.  
  147. /******************************Public*Routine******************************\
  148. *
  149. * bCleanupPrinter
  150. *
  151. * Effects:  Local freeing
  152. *
  153. * Warnings: globals!!!
  154. *
  155. \**************************************************************************/
  156.  
  157. BOOL bCleanupPrinter(VOID)
  158. {
  159.     if (gpPrinters != NULL)
  160.         LocalFree((LOCALHANDLE)gpPrinters);
  161.     if (gpszPrinterNames != NULL)
  162.         LocalFree((LOCALHANDLE)gpszPrinterNames);
  163.     if (gpszDeviceNames  != NULL)
  164.         LocalFree((LOCALHANDLE)gpszDeviceNames);
  165.  
  166.     return TRUE;
  167. }
  168.