home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP04 / WHATSIZE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  4.1 KB  |  121 lines

  1. /*-----------------------------------------
  2.    WHATSIZE.C -- What Size is the Window?
  3.                  (c) Charles Petzold, 1996
  4.   -----------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <stdio.h>
  8.  
  9. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  10.  
  11. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  12.                     PSTR szCmdLine, int iCmdShow)
  13.      {
  14.      static char szAppName[] = "WhatSize" ;
  15.      HWND        hwnd ;
  16.      MSG         msg ;
  17.      WNDCLASSEX  wndclass ;
  18.  
  19.      wndclass.cbSize        = sizeof (wndclass) ;
  20.      wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  21.      wndclass.lpfnWndProc   = WndProc ;
  22.      wndclass.cbClsExtra    = 0 ;
  23.      wndclass.cbWndExtra    = 0 ;
  24.      wndclass.hInstance     = hInstance ;
  25.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  26.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  27.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  28.      wndclass.lpszMenuName  = NULL ;
  29.      wndclass.lpszClassName = szAppName ;
  30.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  31.  
  32.      RegisterClassEx (&wndclass) ;
  33.  
  34.      hwnd = CreateWindow (szAppName, "What Size is the Window?",
  35.                           WS_OVERLAPPEDWINDOW,
  36.                           CW_USEDEFAULT, CW_USEDEFAULT,
  37.                           CW_USEDEFAULT, CW_USEDEFAULT,
  38.                           NULL, NULL, hInstance, NULL) ;
  39.  
  40.      ShowWindow (hwnd, iCmdShow) ;
  41.      UpdateWindow (hwnd) ;
  42.  
  43.      while (GetMessage (&msg, NULL, 0, 0))
  44.           {
  45.           TranslateMessage (&msg) ;
  46.           DispatchMessage (&msg) ;
  47.           }
  48.      return msg.wParam ;
  49.      }
  50.  
  51. void Show (HWND hwnd, HDC hdc, int xText, int yText, int iMapMode,
  52.            char *szMapMode)
  53.      {
  54.      char szBuffer [60] ;
  55.      RECT rect ;
  56.  
  57.      SaveDC (hdc) ;
  58.  
  59.      SetMapMode (hdc, iMapMode) ;
  60.      GetClientRect (hwnd, &rect) ;
  61.      DPtoLP (hdc, (PPOINT) &rect, 2) ;
  62.  
  63.      RestoreDC (hdc, -1) ;
  64.  
  65.      TextOut (hdc, xText, yText, szBuffer,
  66.                sprintf (szBuffer, "%-20s %7d %7d %7d %7d", szMapMode,
  67.                     rect.left, rect.right, rect.top, rect.bottom)) ;
  68.      }  
  69.  
  70. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  71.      {
  72.      static char  szHeading [] =
  73.                     "Mapping Mode            Left   Right     Top  Bottom" ;
  74.      static char  szUndLine [] = 
  75.                     "------------            ----   -----     ---  ------" ;
  76.      static int   cxChar, cyChar ;
  77.      HDC          hdc ;
  78.      PAINTSTRUCT  ps ;
  79.      TEXTMETRIC   tm ;
  80.  
  81.      switch (iMsg)
  82.           {
  83.           case WM_CREATE:
  84.                hdc = GetDC (hwnd) ;
  85.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  86.  
  87.                GetTextMetrics (hdc, &tm) ;
  88.                cxChar = tm.tmAveCharWidth ;
  89.                cyChar = tm.tmHeight + tm.tmExternalLeading ;
  90.  
  91.                ReleaseDC (hwnd, hdc) ;
  92.                return 0 ;
  93.  
  94.           case WM_PAINT:
  95.                hdc = BeginPaint (hwnd, &ps) ;
  96.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  97.  
  98.                SetMapMode (hdc, MM_ANISOTROPIC) ;
  99.                SetWindowExtEx (hdc, 1, 1, NULL) ;
  100.                SetViewportExtEx (hdc, cxChar, cyChar, NULL) ;
  101.  
  102.                TextOut (hdc, 1, 1, szHeading, sizeof szHeading - 1) ;
  103.                TextOut (hdc, 1, 2, szUndLine, sizeof szUndLine - 1) ;
  104.  
  105.                Show (hwnd, hdc, 1, 3, MM_TEXT,      "TEXT (pixels)") ;
  106.                Show (hwnd, hdc, 1, 4, MM_LOMETRIC,  "LOMETRIC (.1 mm)") ;
  107.                Show (hwnd, hdc, 1, 5, MM_HIMETRIC,  "HIMETRIC (.01 mm)") ;
  108.                Show (hwnd, hdc, 1, 6, MM_LOENGLISH, "LOENGLISH (.01 in)") ;
  109.                Show (hwnd, hdc, 1, 7, MM_HIENGLISH, "HIENGLISH (.001 in)") ;
  110.                Show (hwnd, hdc, 1, 8, MM_TWIPS,     "TWIPS (1/1440 in)") ;
  111.  
  112.                EndPaint (hwnd, &ps) ;
  113.                return 0 ;
  114.  
  115.           case WM_DESTROY:
  116.                PostQuitMessage (0) ;
  117.                return 0 ;
  118.           }
  119.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  120.      }
  121.