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

  1. /*------------------------------------------------------
  2.    DEVCAPS1.C -- Displays Device Capability Information
  3.                  (c) Charles Petzold, 1990
  4.   ------------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <string.h>
  8. #include "devcaps1.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. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;
  15.  
  16. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  17.                     LPSTR lpszCmdLine, int nCmdShow)
  18.      {
  19.      static char szAppName[] = "DevCaps" ;
  20.      HWND        hwnd ;
  21.      MSG         msg ;
  22.      WNDCLASS    wndclass ;
  23.  
  24.      if (!hPrevInstance) 
  25.           {
  26.           wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  27.           wndclass.lpfnWndProc   = WndProc ;
  28.           wndclass.cbClsExtra    = 0 ;
  29.           wndclass.cbWndExtra    = 0 ;
  30.           wndclass.hInstance     = hInstance ;
  31.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  32.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  33.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  34.           wndclass.lpszMenuName  = szAppName ;
  35.           wndclass.lpszClassName = szAppName ;
  36.  
  37.           !RegisterClass (&wndclass) ;
  38.           }
  39.  
  40.      hwnd = CreateWindow (szAppName, "Device Capabilities",
  41.                           WS_OVERLAPPEDWINDOW,
  42.                           CW_USEDEFAULT, CW_USEDEFAULT,
  43.                           CW_USEDEFAULT, CW_USEDEFAULT,
  44.                           NULL, NULL, hInstance, NULL) ;
  45.  
  46.      ShowWindow (hwnd, nCmdShow) ;
  47.      UpdateWindow (hwnd) ;
  48.  
  49.      while (GetMessage (&msg, NULL, 0, 0))
  50.           {
  51.           TranslateMessage (&msg) ;
  52.           DispatchMessage (&msg) ;
  53.           }
  54.      return msg.wParam ;
  55.      }
  56.  
  57. HDC GetPrinterIC ()
  58.      {
  59.      char szPrinter [64] ;
  60.      char *szDevice, *szDriver, *szOutput ;
  61.  
  62.      GetProfileString ("windows", "device", "", szPrinter, 64) ;
  63.  
  64.      if ((szDevice = strtok (szPrinter, "," )) &&
  65.          (szDriver = strtok (NULL,      ", ")) && 
  66.          (szOutput = strtok (NULL,      ", ")))
  67.           
  68.                return CreateIC (szDriver, szDevice, szOutput, NULL) ;
  69.  
  70.      return NULL ;
  71.      }
  72.  
  73. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  74.      {
  75.      static short cxChar, cyChar, nCurrentDevice = IDM_SCREEN,
  76.                                   nCurrentInfo   = IDM_BASIC ;
  77.      HDC          hdc, hdcInfo ;
  78.      HMENU        hMenu ;
  79.      PAINTSTRUCT  ps ;
  80.      TEXTMETRIC   tm ;
  81.  
  82.      switch (message)
  83.           {
  84.           case WM_CREATE:
  85.                hdc = GetDC (hwnd) ;
  86.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  87.  
  88.                GetTextMetrics (hdc, &tm) ;
  89.                cxChar = tm.tmAveCharWidth ;
  90.                cyChar = tm.tmHeight + tm.tmExternalLeading ;
  91.  
  92.                ReleaseDC (hwnd, hdc) ;
  93.                return 0 ;
  94.  
  95.           case WM_COMMAND:
  96.                hMenu = GetMenu (hwnd) ;
  97.  
  98.                switch (wParam)
  99.                     {
  100.                     case IDM_SCREEN:
  101.                     case IDM_PRINTER:
  102.                          CheckMenuItem (hMenu, nCurrentDevice, MF_UNCHECKED) ;
  103.                          nCurrentDevice = wParam ;
  104.                          CheckMenuItem (hMenu, nCurrentDevice, MF_CHECKED) ;
  105.                          InvalidateRect (hwnd, NULL, TRUE) ;
  106.                          return 0 ;
  107.  
  108.                     case IDM_BASIC:
  109.                     case IDM_OTHER:
  110.                     case IDM_CURVE:
  111.                     case IDM_LINE:
  112.                     case IDM_POLY:
  113.                     case IDM_TEXT:
  114.                          CheckMenuItem (hMenu, nCurrentInfo, MF_UNCHECKED) ;
  115.                          nCurrentInfo = wParam ;
  116.                          CheckMenuItem (hMenu, nCurrentInfo, MF_CHECKED) ;
  117.                          InvalidateRect (hwnd, NULL, TRUE) ;
  118.                          return 0 ;
  119.                     }
  120.                break ;
  121.  
  122.           case WM_DEVMODECHANGE:
  123.                InvalidateRect (hwnd, NULL, TRUE) ;
  124.                return 0 ;
  125.  
  126.           case WM_PAINT:
  127.                hdc = BeginPaint (hwnd, &ps) ;
  128.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  129.           
  130.                if (nCurrentDevice == IDM_SCREEN)
  131.                     hdcInfo = CreateIC ("DISPLAY", NULL, NULL, NULL) ;
  132.                else
  133.                     hdcInfo = GetPrinterIC () ;
  134.  
  135.                if (hdcInfo)
  136.                     {
  137.                     switch (nCurrentInfo)
  138.                          {
  139.                          case IDM_BASIC:
  140.                               DoBasicInfo (hdc, hdcInfo, cxChar, cyChar) ;
  141.                               break ;
  142.  
  143.                          case IDM_OTHER:
  144.                               DoOtherInfo (hdc, hdcInfo, cxChar, cyChar) ;
  145.                               break ;
  146.  
  147.                          case IDM_CURVE:
  148.                          case IDM_LINE:
  149.                          case IDM_POLY:
  150.                          case IDM_TEXT:
  151.                               DoBitCodedCaps (hdc, hdcInfo, cxChar, cyChar,
  152.                                               nCurrentInfo - IDM_CURVE) ;
  153.                               break ;
  154.                          }
  155.                     DeleteDC (hdcInfo) ;
  156.                     }
  157.  
  158.                EndPaint (hwnd, &ps) ;
  159.                return 0 ;
  160.  
  161.           case WM_DESTROY:
  162.                PostQuitMessage (0) ;
  163.                return 0 ;
  164.           }
  165.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  166.      }
  167.