home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / progwin / chap15 / devcaps2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-12  |  10.2 KB  |  272 lines

  1. /*------------------------------------------------------------------
  2.    DEVCAPS2.C -- Displays Device Capability Information (Version 2)
  3.                  (c) Charles Petzold, 1990
  4.   ------------------------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <string.h>
  8. #include "devcaps2.h"
  9.  
  10. void DoBasicInfo (HDC, HDC, short, short) ;            // in DEVCAPS.C
  11. void DoOtherInfo (HDC, HDC, short, short) ;
  12. void DoBitCodedCaps (HDC, HDC, short, short, short) ;
  13.  
  14. typedef VOID (FAR PASCAL *DEVMODEPROC) (HWND, HANDLE, LPSTR, LPSTR) ;
  15.  
  16. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;
  17.  
  18. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  19.                     LPSTR lpszCmdLine, int nCmdShow)
  20.      {
  21.      static char szAppName[] = "DevCaps2" ;
  22.      HWND        hwnd ;
  23.      MSG         msg ;
  24.      WNDCLASS    wndclass ;
  25.  
  26.      if (!hPrevInstance) 
  27.           {
  28.           wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  29.           wndclass.lpfnWndProc   = WndProc ;
  30.           wndclass.cbClsExtra    = 0 ;
  31.           wndclass.cbWndExtra    = 0 ;
  32.           wndclass.hInstance     = hInstance ;
  33.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  34.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  35.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  36.           wndclass.lpszMenuName  = szAppName ;
  37.           wndclass.lpszClassName = szAppName ;
  38.  
  39.           RegisterClass (&wndclass) ;
  40.           }
  41.  
  42.      hwnd = CreateWindow (szAppName, NULL,
  43.                           WS_OVERLAPPEDWINDOW,
  44.                           CW_USEDEFAULT, CW_USEDEFAULT,
  45.                           CW_USEDEFAULT, CW_USEDEFAULT,
  46.                           NULL, NULL, hInstance, NULL) ;
  47.  
  48.      ShowWindow (hwnd, nCmdShow) ;
  49.      UpdateWindow (hwnd) ;
  50.  
  51.      while (GetMessage (&msg, NULL, 0, 0))
  52.           {
  53.           TranslateMessage (&msg) ;
  54.           DispatchMessage (&msg) ;
  55.           }
  56.      return msg.wParam ;
  57.      }
  58.  
  59. void DoEscSupport (HDC hdc, HDC hdcInfo, short cxChar, short cyChar)
  60.      {
  61.      static struct
  62.           {
  63.           char  *szEscCode ;
  64.           short nEscCode ;
  65.           } 
  66.           esc [] =
  67.           {
  68.           "NEWFRAME",          NEWFRAME,
  69.           "ABORTDOC",          ABORTDOC,
  70.           "NEXTBAND",          NEXTBAND,
  71.           "SETCOLORTABLE",     SETCOLORTABLE,
  72.           "GETCOLORTABLE",     GETCOLORTABLE,
  73.           "FLUSHOUTPUT",       FLUSHOUTPUT,
  74.           "DRAFTMODE",         DRAFTMODE,
  75.           "QUERYESCSUPPORT",   QUERYESCSUPPORT,
  76.           "SETABORTPROC",      SETABORTPROC,
  77.           "STARTDOC",          STARTDOC,
  78.           "ENDDOC",            ENDDOC,
  79.           "GETPHYSPAGESIZE",   GETPHYSPAGESIZE,
  80.           "GETPRINTINGOFFSET", GETPRINTINGOFFSET,
  81.           "GETSCALINGFACTOR",  GETSCALINGFACTOR } ;
  82.  
  83.      static char *szYesNo [] = { "Yes", "No" } ;
  84.      char        szBuffer [32] ;
  85.      POINT       pt ;
  86.      short       n, nReturn ;
  87.  
  88.      TextOut (hdc, cxChar, cyChar, "Escape Support", 14) ;
  89.  
  90.      for (n = 0 ; n < sizeof esc / sizeof esc [0] ; n++)
  91.           {
  92.           nReturn = Escape (hdcInfo, QUERYESCSUPPORT, 1,
  93.                                    (LPSTR) & esc[n].nEscCode, NULL) ;
  94.           TextOut (hdc, 6 * cxChar, (n + 3) * cyChar, szBuffer,
  95.                wsprintf (szBuffer, "%-24s %3s", (LPSTR) esc[n].szEscCode,
  96.                          (LPSTR) szYesNo [nReturn > 0 ? 0 : 1])) ;
  97.  
  98.           if (nReturn > 0 && esc[n].nEscCode >= GETPHYSPAGESIZE
  99.                           && esc[n].nEscCode <= GETSCALINGFACTOR)
  100.                {
  101.                Escape (hdcInfo, esc[n].nEscCode, 0, NULL, (LPSTR) &pt) ;
  102.                TextOut (hdc, 36 * cxChar, (n + 3) * cyChar, szBuffer,
  103.                         wsprintf (szBuffer, "(%u,%u)", pt.x, pt.y)) ;
  104.                }
  105.           }
  106.      }
  107.  
  108. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  109.      {
  110.      static char   szAllDevices [4096], szDevice [32], szDriver [16],
  111.                    szDriverFile [16], szWindowText [64] ;
  112.      static HANDLE hLibrary ;
  113.      static short  n, cxChar, cyChar, nCurrentDevice = IDM_SCREEN,
  114.                                       nCurrentInfo   = IDM_BASIC ;
  115.      char          *szOutput, *szPtr ;
  116.      DEVMODEPROC   lpfnDM ;
  117.      HDC           hdc, hdcInfo ;
  118.      HMENU         hMenu ;
  119.      PAINTSTRUCT   ps ;
  120.      TEXTMETRIC    tm ;
  121.  
  122.      switch (message)
  123.           {
  124.           case WM_CREATE:
  125.                hdc = GetDC (hwnd) ;
  126.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  127.                GetTextMetrics (hdc, &tm) ;
  128.                cxChar = tm.tmAveCharWidth ;
  129.                cyChar = tm.tmHeight + tm.tmExternalLeading ;
  130.                ReleaseDC (hwnd, hdc) ;
  131.  
  132.                lParam = NULL ;
  133.                                                   // fall through
  134.           case WM_WININICHANGE:
  135.                if (lParam != NULL && lstrcmp ((LPSTR) lParam, "devices") != 0)
  136.                     return 0 ;
  137.  
  138.                hMenu = GetSubMenu (GetMenu (hwnd), 0) ;
  139.  
  140.                while (GetMenuItemCount (hMenu) > 1)
  141.                     DeleteMenu (hMenu, 1, MF_BYPOSITION) ;
  142.  
  143.                GetProfileString ("devices", NULL, "", szAllDevices,
  144.                          sizeof szAllDevices) ;
  145.  
  146.                n = IDM_SCREEN + 1 ;
  147.                szPtr = szAllDevices ;
  148.                while (*szPtr)
  149.                     {
  150.                     AppendMenu (hMenu, n % 16 ? 0 : MF_MENUBARBREAK, n, szPtr);
  151.                     n++ ;
  152.                     szPtr += strlen (szPtr) + 1 ;
  153.                     }
  154.                AppendMenu (hMenu, MF_SEPARATOR, 0, NULL) ;
  155.                AppendMenu (hMenu, 0, IDM_DEVMODE, "Device Mode") ;
  156.  
  157.                wParam = IDM_SCREEN ;
  158.                                                   // fall through
  159.           case WM_COMMAND:
  160.                hMenu = GetMenu (hwnd) ;
  161.  
  162.                if (wParam < IDM_DEVMODE)          // IDM_SCREEN & Printers
  163.                     {
  164.                     CheckMenuItem (hMenu, nCurrentDevice, MF_UNCHECKED) ;
  165.                     nCurrentDevice = wParam ;
  166.                     CheckMenuItem (hMenu, nCurrentDevice, MF_CHECKED) ;
  167.                     }
  168.                else if (wParam == IDM_DEVMODE)
  169.                     {
  170.                     GetMenuString (hMenu, nCurrentDevice, szDevice,
  171.                                    sizeof szDevice, MF_BYCOMMAND) ;
  172.  
  173.                     GetProfileString ("devices", szDevice, "",
  174.                                       szDriver, sizeof szDriver) ;
  175.  
  176.                     szOutput = strtok (szDriver, ", ") ;
  177.                     strcat (strcpy (szDriverFile, szDriver), ".DRV") ;
  178.  
  179.                     if (hLibrary >= 32)
  180.                          FreeLibrary (hLibrary) ;
  181.  
  182.                     hLibrary = LoadLibrary (szDriverFile) ;
  183.                     if (hLibrary >= 32)
  184.                          {
  185.                          lpfnDM = GetProcAddress (hLibrary, "DEVICEMODE") ;
  186.                          (*lpfnDM) (hwnd, hLibrary, (LPSTR) szDevice,
  187.                                                     (LPSTR) szOutput) ;
  188.                          }
  189.                     }
  190.                else                               // info menu items
  191.                     {
  192.                     CheckMenuItem (hMenu, nCurrentInfo, MF_UNCHECKED) ;
  193.                     nCurrentInfo = wParam ;
  194.                     CheckMenuItem (hMenu, nCurrentInfo, MF_CHECKED) ;
  195.                     }
  196.                InvalidateRect (hwnd, NULL, TRUE) ;
  197.                return 0 ;
  198.  
  199.           case WM_INITMENUPOPUP:
  200.                if (lParam == 0)
  201.                     EnableMenuItem (GetMenu (hwnd), IDM_DEVMODE,
  202.                          nCurrentDevice == IDM_SCREEN ?
  203.                               MF_GRAYED : MF_ENABLED) ;
  204.                return 0 ;
  205.  
  206.           case WM_PAINT:
  207.                strcpy (szWindowText, "Device Capabilities: ") ;
  208.           
  209.                if (nCurrentDevice == IDM_SCREEN)
  210.                     {
  211.                     strcpy (szDriver, "DISPLAY") ;
  212.                     strcat (szWindowText, szDriver) ;
  213.                     hdcInfo = CreateIC (szDriver, NULL, NULL, NULL) ;
  214.                     }
  215.                else
  216.                     {
  217.                     hMenu = GetMenu (hwnd) ;
  218.  
  219.                     GetMenuString (hMenu, nCurrentDevice, szDevice,
  220.                                    sizeof szDevice, MF_BYCOMMAND) ;
  221.  
  222.                     GetProfileString ("devices", szDevice, "", szDriver, 10) ;
  223.                     szOutput = strtok (szDriver, ", ") ;
  224.                     strcat (szWindowText, szDevice) ;
  225.                     
  226.                     hdcInfo = CreateIC (szDriver, szDevice, szOutput, NULL) ;
  227.                     }
  228.                SetWindowText (hwnd, szWindowText) ;
  229.  
  230.                hdc = BeginPaint (hwnd, &ps) ;
  231.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  232.  
  233.                if (hdcInfo)
  234.                     {
  235.                     switch (nCurrentInfo)
  236.                          {
  237.                          case IDM_BASIC:
  238.                               DoBasicInfo (hdc, hdcInfo, cxChar, cyChar) ;
  239.                               break ;
  240.  
  241.                          case IDM_OTHER:
  242.                               DoOtherInfo (hdc, hdcInfo, cxChar, cyChar) ;
  243.                               break ;
  244.  
  245.                          case IDM_CURVE:
  246.                          case IDM_LINE:
  247.                          case IDM_POLY:
  248.                          case IDM_TEXT:
  249.                               DoBitCodedCaps (hdc, hdcInfo, cxChar, cyChar,
  250.                                    nCurrentInfo - IDM_CURVE) ;
  251.                               break ;
  252.  
  253.                          case IDM_ESC:
  254.                               DoEscSupport (hdc, hdcInfo, cxChar, cyChar) ;
  255.                               break ;
  256.                          }
  257.                     DeleteDC (hdcInfo) ;
  258.                     }
  259.  
  260.                EndPaint (hwnd, &ps) ;
  261.                return 0 ;
  262.  
  263.           case WM_DESTROY:
  264.                if (hLibrary >= 32)
  265.                     FreeLibrary (hLibrary) ;
  266.  
  267.                PostQuitMessage (0) ;
  268.                return 0 ;
  269.           }
  270.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  271.      }
  272.