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

  1. /*-----------------------------------------
  2.    WHATSIZE.C -- What Size is the Window?
  3.                  (c) Charles Petzold, 1990
  4.   -----------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <stdio.h>
  8.  
  9. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;
  10.  
  11. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  12.                     LPSTR lpszCmdLine, int nCmdShow)
  13.      {
  14.      static char szAppName[] = "WhatSize" ;
  15.      HWND        hwnd ;
  16.      MSG         msg ;
  17.      WNDCLASS    wndclass ;
  18.  
  19.      if (!hPrevInstance) 
  20.           {
  21.           wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  22.           wndclass.lpfnWndProc   = WndProc ;
  23.           wndclass.cbClsExtra    = 0 ;
  24.           wndclass.cbWndExtra    = 0 ;
  25.           wndclass.hInstance     = hInstance ;
  26.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  27.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  28.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  29.           wndclass.lpszMenuName  = NULL ;
  30.           wndclass.lpszClassName = szAppName ;
  31.  
  32.           RegisterClass (&wndclass) ;
  33.           }
  34.  
  35.      hwnd = CreateWindow (szAppName, "What Size is the Window?",
  36.                           WS_OVERLAPPEDWINDOW,
  37.                           CW_USEDEFAULT, CW_USEDEFAULT,
  38.                           CW_USEDEFAULT, CW_USEDEFAULT,
  39.                           NULL, NULL, hInstance, NULL) ;
  40.  
  41.      ShowWindow (hwnd, nCmdShow) ;
  42.      UpdateWindow (hwnd) ;
  43.  
  44.      while (GetMessage (&msg, NULL, 0, 0))
  45.           {
  46.           TranslateMessage (&msg) ;
  47.           DispatchMessage (&msg) ;
  48.           }
  49.      return msg.wParam ;
  50.      }
  51.  
  52. void Show (HWND hwnd, HDC hdc, short xText, short yText, short nMapMode,
  53.            char *szMapMode)
  54.      {
  55.      char szBuffer [60] ;
  56.      RECT rect ;
  57.  
  58.      SaveDC (hdc) ;
  59.  
  60.      SetMapMode (hdc, nMapMode) ;
  61.      GetClientRect (hwnd, &rect) ;
  62.      DPtoLP (hdc, (LPPOINT) &rect, 2) ;
  63.  
  64.      RestoreDC (hdc, -1) ;
  65.  
  66.      TextOut (hdc, xText, yText, szBuffer,
  67.                sprintf (szBuffer, "%-20s %7d %7d %7d %7d", szMapMode,
  68.                     rect.left, rect.right, rect.top, rect.bottom)) ;
  69.      }  
  70.  
  71. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  72.      {
  73.      static char  szHeading [] =
  74.                     "Mapping Mode            Left   Right     Top  Bottom" ;
  75.      static char  szUndLine [] = 
  76.                     "------------            ----   -----     ---  ------" ;
  77.      static short cxChar, cyChar ;
  78.      HDC          hdc ;
  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_PAINT:
  96.                hdc = BeginPaint (hwnd, &ps) ;
  97.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  98.  
  99.                SetMapMode (hdc, MM_ANISOTROPIC) ;
  100.                SetWindowExt (hdc, 1, 1) ;
  101.                SetViewportExt (hdc, cxChar, cyChar) ;
  102.  
  103.                TextOut (hdc, 1, 1, szHeading, sizeof szHeading - 1) ;
  104.                TextOut (hdc, 1, 2, szUndLine, sizeof szUndLine - 1) ;
  105.  
  106.                Show (hwnd, hdc, 1, 3, MM_TEXT,      "TEXT (pixels)") ;
  107.                Show (hwnd, hdc, 1, 4, MM_LOMETRIC,  "LOMETRIC (.1 mm)") ;
  108.                Show (hwnd, hdc, 1, 5, MM_HIMETRIC,  "HIMETRIC (.01 mm)") ;
  109.                Show (hwnd, hdc, 1, 6, MM_LOENGLISH, "LOENGLISH (.01 in)") ;
  110.                Show (hwnd, hdc, 1, 7, MM_HIENGLISH, "HIENGLISH (.001 in)") ;
  111.                Show (hwnd, hdc, 1, 8, MM_TWIPS,     "TWIPS (1/1440 in)") ;
  112.  
  113.                EndPaint (hwnd, &ps) ;
  114.                return 0 ;
  115.  
  116.           case WM_DESTROY:
  117.                PostQuitMessage (0) ;
  118.                return 0 ;
  119.           }
  120.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  121.      }
  122.