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