home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 7.ddi / MWHC.007 / X0 < prev    next >
Encoding:
Text File  |  1990-11-11  |  5.1 KB  |  148 lines

  1. /*----------------------------------------
  2.    BTNLOOK.C -- Button Look Program
  3.                 (c) Charles Petzold, 1990
  4.   ----------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <stdio.h>
  8.  
  9. struct
  10.      {
  11.      long style ;
  12.      char *text ;
  13.      }
  14.      button[] =
  15.      {
  16.      BS_PUSHBUTTON,      "PUSHBUTTON",
  17.      BS_DEFPUSHBUTTON,   "DEFPUSHBUTTON",
  18.      BS_CHECKBOX,        "CHECKBOX", 
  19.      BS_AUTOCHECKBOX,    "AUTOCHECKBOX",
  20.      BS_RADIOBUTTON,     "RADIOBUTTON",
  21.      BS_3STATE,          "3STATE",
  22.      BS_AUTO3STATE,      "AUTO3STATE",
  23.      BS_GROUPBOX,        "GROUPBOX",
  24.      BS_USERBUTTON,      "USERBUTTON",
  25.      BS_AUTORADIOBUTTON, "AUTORADIO",
  26.      BS_PUSHBOX,         "PUSHBOX"
  27.      } ;
  28.  
  29. #define NUM (sizeof button / sizeof button [0])
  30.  
  31. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;
  32.  
  33. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  34.                     LPSTR lpszCmdLine, int nCmdShow)
  35.      {
  36.      static char szAppName[] = "BtnLook" ;
  37.      HWND        hwnd ;
  38.      MSG         msg ;
  39.      WNDCLASS    wndclass ;
  40.  
  41.      if (!hPrevInstance) 
  42.           {
  43.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  44.           wndclass.lpfnWndProc   = WndProc ;
  45.           wndclass.cbClsExtra    = 0 ;
  46.           wndclass.cbWndExtra    = 0 ;
  47.           wndclass.hInstance     = hInstance ;
  48.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  49.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  50.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  51.           wndclass.lpszMenuName  = NULL ;
  52.           wndclass.lpszClassName = szAppName ;
  53.  
  54.           RegisterClass (&wndclass) ;
  55.           }
  56.  
  57.      hwnd = CreateWindow (szAppName, "Button Look",
  58.                           WS_OVERLAPPEDWINDOW,
  59.                           CW_USEDEFAULT, CW_USEDEFAULT,
  60.                           CW_USEDEFAULT, CW_USEDEFAULT,
  61.                           NULL, NULL, hInstance, NULL) ;
  62.  
  63.      ShowWindow (hwnd, nCmdShow) ;
  64.      UpdateWindow (hwnd) ;
  65.  
  66.      while (GetMessage (&msg, NULL, 0, 0))
  67.           {
  68.           TranslateMessage (&msg) ;
  69.           DispatchMessage (&msg) ;
  70.           }
  71.      return msg.wParam ;
  72.      }
  73.  
  74. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  75.      {
  76.      static char  szPrm []    = "wParam       LOWORD(lParam)  HIWORD(lParam)",
  77.                   szTop []    = "Control ID   Window Handle   Notification",
  78.                   szUnd []    = "__________   _____________   ____________", 
  79.                   szFormat [] = " %5u           %4X          %5u",
  80.                   szBuffer [50] ;
  81.      static HWND  hwndButton [NUM] ;
  82.      static RECT  rect ;
  83.      static int   cxChar, cyChar ;
  84.      HDC          hdc ;
  85.      PAINTSTRUCT  ps ;
  86.      int          i ;
  87.      TEXTMETRIC   tm ;
  88.  
  89.      switch (message)
  90.           {
  91.           case WM_CREATE:
  92.                hdc = GetDC (hwnd) ;
  93.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  94.                GetTextMetrics (hdc, &tm) ;
  95.                cxChar = tm.tmAveCharWidth ;
  96.                cyChar = tm.tmHeight + tm.tmExternalLeading ;
  97.                ReleaseDC (hwnd, hdc) ;
  98.  
  99.                for (i = 0 ; i < NUM ; i++)
  100.                     hwndButton [i] = CreateWindow ("button", button[i].text,
  101.                               WS_CHILD | WS_VISIBLE | button[i].style,
  102.                               cxChar, cyChar * (1 + 2 * i),
  103.                               20 * cxChar, 7 * cyChar / 4,
  104.                               hwnd, i,
  105.                               ((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;
  106.                return 0 ;
  107.  
  108.           case WM_SIZE:
  109.                rect.left   = 24 * cxChar ;
  110.                rect.top    =  3 * cyChar ;
  111.                rect.right  = LOWORD (lParam) ;
  112.                rect.bottom = HIWORD (lParam) ;
  113.                return 0 ;
  114.  
  115.           case WM_PAINT:
  116.                InvalidateRect (hwnd, &rect, TRUE) ;
  117.  
  118.                hdc = BeginPaint (hwnd, &ps) ;
  119.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  120.                SetBkMode (hdc, TRANSPARENT) ;
  121.  
  122.                TextOut (hdc, 24 * cxChar, 1 * cyChar, szPrm, sizeof szPrm - 1);
  123.                TextOut (hdc, 24 * cxChar, 2 * cyChar, szTop, sizeof szTop - 1);
  124.                TextOut (hdc, 24 * cxChar, 2 * cyChar, szUnd, sizeof szUnd - 1);
  125.  
  126.                EndPaint (hwnd, &ps) ;
  127.                return 0 ;
  128.  
  129.           case WM_COMMAND:
  130.                ScrollWindow (hwnd, 0, -cyChar, &rect, &rect) ;
  131.                hdc = GetDC (hwnd) ;
  132.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  133.  
  134.                TextOut (hdc, 24 * cxChar, cyChar * (rect.bottom / cyChar - 1),
  135.                         szBuffer, sprintf (szBuffer, szFormat, wParam,
  136.                         LOWORD (lParam), HIWORD (lParam))) ;
  137.  
  138.                ReleaseDC (hwnd, hdc) ;
  139.                ValidateRect (hwnd, NULL) ;
  140.                return 0 ;
  141.  
  142.           case WM_DESTROY:
  143.                PostQuitMessage (0) ;
  144.                return 0 ;
  145.           }
  146.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  147.      }
  148.