PRINTER.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:5k
源码类别:

Windows编程

开发平台:

Visual C++

  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. // Globals for printing
  29. //
  30. PPRINTER_INFO_1     gpPrinters       = NULL;
  31. PSZ                *gpszPrinterNames = NULL;
  32. PSZ                *gpszDeviceNames  = NULL;
  33. extern HMENU  hPrinterMenu;
  34. extern INT    giNPrinters;
  35. extern HWND   ghwndMain;
  36. BOOL bInitPrinter(HWND);
  37. BOOL bCleanupPrinter(VOID);
  38. /******************************Public*Routine******************************
  39. *
  40. * bInitPrinter
  41. *
  42. * Effects: Enumerating printers...
  43. *
  44. * Warnings: Globals alert!!
  45. *
  46. **************************************************************************/
  47. BOOL bInitPrinter(HWND hwnd) {
  48.     BOOL        bSuccess;
  49.     DWORD       cbPrinters;
  50.     DWORD       cbNeeded, cReturned, j;
  51.     int         i;
  52.     bSuccess = TRUE;
  53.     cbPrinters = 4096L;
  54.     
  55.     if (!(gpPrinters = (PPRINTER_INFO_1)LocalAlloc((LMEM_FIXED | LMEM_ZEROINIT),
  56.                                                   cbPrinters)))
  57.     {
  58.         OutputDebugString( "InitPrint: LocalAlloc for gpPrinters failed.");
  59.         return (FALSE);
  60.     }
  61.     if (!EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, 1, (LPBYTE)gpPrinters,
  62.                       cbPrinters, &cbNeeded, &cReturned))
  63.     {
  64.         if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) 
  65.         {
  66.             LocalFree((LOCALHANDLE)gpPrinters);
  67.             gpPrinters = (PPRINTER_INFO_1)LocalAlloc((LMEM_FIXED | LMEM_ZEROINIT),
  68.                                                cbNeeded);
  69.             cbPrinters = cbNeeded;
  70.             if (!EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, 1, (LPBYTE)gpPrinters,
  71.                               cbPrinters, &cbNeeded, &cReturned))
  72.             {
  73.                 MessageBox(ghwndMain, 
  74.        GetStringRes (IDS_ERR_CANT_ENUM_PRINTERS),
  75.        NULL, MB_OK);
  76.                 return (FALSE);
  77.             }
  78.         } 
  79.         else 
  80.         {
  81.             MessageBox(ghwndMain, 
  82.        GetStringRes (IDS_ERR_CANT_ENUM_PRINTERS),
  83.    NULL, MB_OK);
  84.             return (FALSE);
  85.         }
  86.     }
  87.     // allocate some memory.
  88.     gpszPrinterNames = (PSZ *)LocalAlloc((LMEM_FIXED | LMEM_ZEROINIT),
  89.                                         cReturned * (DWORD)sizeof(PSZ));
  90.     gpszDeviceNames = (PSZ *)LocalAlloc((LMEM_FIXED | LMEM_ZEROINIT),
  91.                                         cReturned * (DWORD)sizeof(PSZ));
  92.     if (giNPrinters != 0) {
  93.         for (i = 0; i < giNPrinters; i++) {
  94.             RemoveMenu(hPrinterMenu, 3, MF_BYPOSITION);
  95.         }
  96.         giNPrinters = 0;
  97.     }
  98.     // insert each printer name into the menu.
  99.     j = giNPrinters = cReturned;
  100.     for (i = 0; i < (INT) cReturned; i++)
  101.     {
  102.         // insert into menu from bottom up.
  103.         j--;        
  104.         InsertMenu(hPrinterMenu, 4, MF_BYCOMMAND | MF_STRING,
  105.                    MM_PRINTER + i, (LPSTR)gpPrinters[j].pName);
  106.         // save a list of printer names, so we can associate them
  107.         // with their menu indices later.
  108.         gpszPrinterNames[i] = gpPrinters[j].pName;
  109.         gpszDeviceNames[i] = gpPrinters[j].pDescription;
  110.     }
  111. #if 0
  112.     //
  113.     // Use this if this is called in the MDI child instead
  114.     //
  115.     DrawMenuBar(GetParent(GetParent(hwnd)));
  116. #endif
  117.     //
  118.     // Use this instead if this is called in InitializeApp
  119.     //
  120.     DrawMenuBar(hwnd);
  121.     return (bSuccess);
  122. }
  123. /******************************Public*Routine******************************
  124. *
  125. * bCleanupPrinter
  126. *
  127. * Effects:  Local freeing
  128. *
  129. * Warnings: globals!!!
  130. *
  131. **************************************************************************/
  132. BOOL bCleanupPrinter(VOID)
  133. {
  134.     if (gpPrinters != NULL)
  135.         LocalFree((LOCALHANDLE)gpPrinters);
  136.     if (gpszPrinterNames != NULL)
  137.         LocalFree((LOCALHANDLE)gpszPrinterNames);
  138.     if (gpszDeviceNames  != NULL)
  139.         LocalFree((LOCALHANDLE)gpszDeviceNames);
  140.     return TRUE;
  141. }