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 / printer / getcaps.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  8KB  |  293 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples. 
  4. *       Copyright (C) 1993-1997 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. /******************************************************************************\
  13. *
  14. *  PROGRAM:     GETCAPS.C
  15. *
  16. *  PURPOSE:     Handles display of information returned by call to
  17. *               GetDeviceCaps. GetDeviceCaps is called for the
  18. *               currently selected deivce (in the tool bar comobobox),
  19. *               and the results are formatted and displayed in a dialog
  20. *               box.
  21. *
  22. *  FUNTIONS:    GetDeviceCapsDlgProc - handles messages for dialog
  23. *               DisplayDeviceCapsInfo- retrieves device caps info
  24. *               TranslateDeviceCaps  - displays a capability in listbox
  25. *               ComplexDeviceCapsLine- formats a bitfield capability
  26. *
  27. \******************************************************************************/
  28.  
  29. #include <windows.h>
  30. #include <winspool.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33. #include <winspool.h>
  34. #include "common.h"
  35. #include "getcaps.h"
  36.  
  37.  
  38.  
  39. /******************************************************************************\
  40. *
  41. *  FUNCTION:    GetDeviceCapsDlgProc (standard dialog procedure INPUTS/RETURNS)
  42. *
  43. *  COMMENTS:    Processes messages for GetDeviceCaps dialog box
  44. *
  45. \******************************************************************************/
  46.  
  47. LRESULT CALLBACK   GetDeviceCapsDlgProc (HWND   hwnd, UINT msg, WPARAM wParam,
  48.                                          LPARAM lParam)
  49. {
  50.   switch (msg)
  51.   {
  52.     case WM_INITDIALOG:
  53.     {
  54.       BOOL bReturn;
  55.       char buf[BUFSIZE];
  56.  
  57.       ghwndDevCaps = hwnd;
  58.  
  59.       //
  60.       // shove all the caps info in the list box
  61.       //
  62.  
  63.       SetCursor (LoadCursor (NULL, IDC_WAIT));
  64.       bReturn = DisplayDeviceCapsInfo ();
  65.       SetCursor (LoadCursor (NULL, IDC_ARROW));
  66.  
  67.       if (!bReturn)
  68.       {
  69.         EndDialog (hwnd, TRUE);
  70.       }
  71.  
  72.       //
  73.       // set window title to reflect current device
  74.       //
  75.  
  76.       else
  77.       {
  78.         sprintf (buf, "GetDeviceCaps: %s;%s;%s", gszDeviceName, gszPort,
  79.                  gszDriverName);
  80.  
  81.         SetWindowText (hwnd, (LPCSTR) buf);
  82.       }
  83.  
  84.       break;
  85.     }
  86.  
  87.     case WM_COMMAND:
  88.  
  89.       switch (LOWORD (wParam))
  90.       {
  91.         case DID_OK:
  92.  
  93.           EndDialog (hwnd, TRUE);
  94.           return 1;
  95.       }
  96.       break;
  97.   }
  98.   return 0;
  99. }
  100.  
  101.  
  102.  
  103. /******************************************************************************\
  104. *
  105. *  FUNCTION:    DisplayDeviceCapsInfo
  106. *
  107. *  RETURNS:     TRUE if successful,
  108. *               FALSE otherwise
  109. *
  110. *  COMMENTS:    Retrieves all device caps for current deivce & calls
  111. *                 TranslateCaps to insert them in the dialog's listbox.
  112. *
  113. \******************************************************************************/
  114.  
  115. BOOL DisplayDeviceCapsInfo ()
  116. {
  117.   HDC hdc;
  118.   int i, iValue;
  119.  
  120.   if (!strcmp (gszDeviceName, "Display"))
  121.   {
  122.     if (!(hdc = GetDC (ghwndDevCaps)))
  123.     {
  124.       ErrMsgBox (GetStringRes(IDS_GETDCFAIL), ERR_MOD_NAME);
  125.       return FALSE;
  126.     }
  127.   }
  128.  
  129.   else
  130.   {
  131.     if (!(hdc = CreateDC (gszDriverName, gszDeviceName, gszPort, NULL)))
  132.     {
  133.       char buf[BUFSIZE];
  134.  
  135.       sprintf (buf, GetStringRes(IDS_FMT_CREDCFAIL),
  136.                gszDriverName, gszDeviceName, gszPort);
  137.       ErrMsgBox (buf, ERR_MOD_NAME);
  138.       return FALSE;
  139.     }
  140.   }
  141.  
  142.   for (i = 0; i < MAX_DEVICE_CAPS; i++)
  143.   {
  144.     iValue = GetDeviceCaps (hdc, gaCaps[i].iValue);
  145.     TranslateDeviceCaps (i, gaCaps[i].iValue, iValue);
  146.   }
  147.   DeleteDC (hdc);
  148.  
  149.   return TRUE;
  150. }
  151.  
  152.  
  153.  
  154. /******************************************************************************\
  155. *
  156. *  FUNCTION:    TranslateDeviceCaps
  157. *
  158. *  INPUTS:      arrayIndex - index into gaCaps[]
  159. *               capIndex   - devcap index (eg. TECHNOLOGY, CURVECAPS)
  160. *               iValue     - value returned by GetDeviceCaps
  161. *
  162. *  COMMENTS:    For simple devcaps (eg. tjose with single numeric return
  163. *               value), appends caps value to string and inserts into
  164. *               listbox. For "complex" caps (those returning multiple
  165. *               bit-values) calls ComplexCapsLine which handles text
  166. *               formattting & insertion.
  167. *
  168. \******************************************************************************/
  169.  
  170. void TranslateDeviceCaps (int arrayIndex, int capIndex, int iValue)
  171. {
  172.   char buf[BUFSIZE];
  173.  
  174.   strcpy (buf, gaCaps[arrayIndex].szValue);
  175.  
  176.   switch (capIndex)
  177.   {
  178.     case TECHNOLOGY:
  179.     {
  180.       int     i;
  181.       
  182.       for (i = 0; i < MAX_TECHNOLOGY_CAPS; i++)
  183.         if (iValue == (gaTechnologyCaps + i)->iValue)
  184.         {
  185.           strcat(buf, (gaTechnologyCaps + i)->szValue);
  186.           SendDlgItemMessage (ghwndDevCaps, DID_LISTBOX, LB_INSERTSTRING,
  187.                               (UINT)-1, (LONG) buf);
  188.           break;
  189.         }
  190.             
  191.       break;
  192.     }
  193.  
  194.     case CURVECAPS:
  195.  
  196.       ComplexDeviceCapsLine (buf, gaCurveCaps, MAX_CURVE_CAPS, iValue);
  197.       break;
  198.  
  199.     case LINECAPS:
  200.  
  201.       ComplexDeviceCapsLine (buf, gaLineCaps, MAX_LINE_CAPS, iValue);
  202.       break;
  203.  
  204.     case POLYGONALCAPS:
  205.  
  206.       ComplexDeviceCapsLine (buf, gaPolygonCaps, MAX_POLYGON_CAPS, iValue);
  207.       break;
  208.  
  209.     case TEXTCAPS:
  210.  
  211.       ComplexDeviceCapsLine (buf, gaTextCaps, MAX_TEXT_CAPS, iValue);
  212.       break;
  213.  
  214.     case CLIPCAPS:
  215.  
  216.       ComplexDeviceCapsLine (buf, gaClipCaps, MAX_CLIP_CAPS, iValue);
  217.       break;
  218.  
  219.     case RASTERCAPS:
  220.  
  221.       ComplexDeviceCapsLine (buf, gaRasterCaps, MAX_RASTER_CAPS, iValue);
  222.       break;
  223.  
  224.     default:
  225.  
  226.       wsprintf(buf, gaCaps[arrayIndex].szValue, iValue);
  227.  
  228.       SendDlgItemMessage (ghwndDevCaps, DID_LISTBOX, LB_INSERTSTRING,
  229.                           (UINT)-1, (LONG) buf);
  230.       break;
  231.   }
  232. }
  233.  
  234.  
  235.  
  236. /******************************************************************************\
  237. *
  238. *  FUNCTION:     ComplexDeviceCapsLine
  239. *
  240. *  INPUTS:       pbuf        - pointer to buffer containing a cap-type
  241. *                              string
  242. *                pLkUp       - pointer to a CAPSLOOKUP table
  243. *                iMaxEntries - # of enries in table pointed at by pLkUp
  244. *                iValue      - an integer containing 1+ bit-value flags.
  245. *
  246. *  COMMENTS:     This function is used to expand an int containing
  247. *                multiple bit-values into a set of strings which are
  248. *                inserted into the DevCapsDlg listbox. The iValue
  249. *                parameter is checked against each iIndex entry in the
  250. *                CAPSLOOKUP table pointed at by pLkUp, and when matches
  251. *                are found the corresponding (lpszValue) string is
  252. *                inserted.
  253. *
  254. *                The buffer pointed to by pbuf will be destroyed.
  255. *
  256. \******************************************************************************/
  257.  
  258. void ComplexDeviceCapsLine (char *pbuf, CAPSLOOKUP *pLkUp, int iMaxEntries,
  259.                      int iValue)
  260. {
  261.   int  i;
  262.   BOOL bNewLine = FALSE;
  263.  
  264.   for (i = 0; i < iMaxEntries; i++)
  265.  
  266.     if (iValue & (pLkUp + i)->iValue)
  267.     {
  268.       if (bNewLine)
  269.       {
  270.         //
  271.         // Keep the first symbolic constant on the same line as the
  272.         //   cap type, eg:  "TECHNOLOGY:     DT_RASDISPLAY".
  273.         //
  274.  
  275.         strcpy (pbuf, BLANKS);
  276.         strcat (pbuf, (pLkUp + i)->szValue);
  277.       }
  278.       else
  279.       {
  280.         //
  281.         // Put symbolic constant on new line, eg:
  282.         //                  "                DT_RASPRINTER".
  283.         //
  284.  
  285.         strcat (pbuf, (pLkUp + i)->szValue);
  286.         bNewLine = TRUE;
  287.       }
  288.       SendDlgItemMessage (ghwndDevCaps, DID_LISTBOX, LB_INSERTSTRING,
  289.                           (UINT)-1, (LONG) pbuf);
  290.    }
  291. }
  292.  
  293.