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

  1. /*-------------------------------------------------
  2.    POEPOEM.C -- Demonstrates User-Defined Resource
  3.                 (c) Charles Petzold, 1996
  4.   -------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "poepoem.h"
  8.  
  9. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  10.  
  11. char      szAppName[10] ;
  12. char      szCaption[35] ;
  13. HINSTANCE hInst ;
  14.  
  15. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  16.                     PSTR szCmdLine, int iCmdShow)
  17.      {
  18.      HWND       hwnd ;
  19.      MSG        msg ;
  20.      WNDCLASSEX wndclass ;
  21.  
  22.      LoadString (hInstance, IDS_APPNAME, szAppName, sizeof (szAppName)) ;
  23.      LoadString (hInstance, IDS_CAPTION, szCaption, sizeof (szCaption)) ;
  24.  
  25.      wndclass.cbSize        = sizeof (wndclass) ;
  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 (hInstance, szAppName) ;
  32.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  33.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  34.      wndclass.lpszMenuName  = NULL ;
  35.      wndclass.lpszClassName = szAppName ;
  36.      wndclass.hIconSm       = LoadIcon (hInstance, szAppName) ;
  37.  
  38.      RegisterClassEx (&wndclass) ;
  39.  
  40.      hInst = hInstance ;
  41.  
  42.      hwnd = CreateWindow (szAppName, szCaption,
  43.                           WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  44.                           CW_USEDEFAULT, CW_USEDEFAULT,
  45.                           CW_USEDEFAULT, CW_USEDEFAULT,
  46.                           NULL, NULL, hInstance, NULL) ;
  47.  
  48.      ShowWindow (hwnd, iCmdShow) ;
  49.      UpdateWindow (hwnd) ;
  50.  
  51.      while (GetMessage (&msg, NULL, 0, 0))
  52.           {
  53.           TranslateMessage (&msg) ;
  54.           DispatchMessage (&msg) ;
  55.           }
  56.      return msg.wParam ;
  57.      }
  58.  
  59. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  60.      {
  61.      static char   *pText ;
  62.      static HGLOBAL hResource ;
  63.      static HWND    hScroll ;
  64.      static int     iPosition, cxChar, cyChar, cyClient, iNumLines, xScroll ;
  65.      char           szPoemRes[15] ;
  66.      HDC            hdc ;
  67.      PAINTSTRUCT    ps ;
  68.      RECT           rect ;
  69.      TEXTMETRIC     tm ;
  70.  
  71.      switch (iMsg)
  72.           {
  73.           case WM_CREATE :
  74.                hdc = GetDC (hwnd) ;
  75.                GetTextMetrics (hdc, &tm) ;
  76.                cxChar = tm.tmAveCharWidth ;
  77.                cyChar = tm.tmHeight + tm.tmExternalLeading ;
  78.                ReleaseDC (hwnd, hdc) ;
  79.  
  80.                xScroll = GetSystemMetrics (SM_CXVSCROLL) ;
  81.  
  82.                hScroll = CreateWindow ("scrollbar", NULL,
  83.                               WS_CHILD | WS_VISIBLE | SBS_VERT,
  84.                               0, 0, 0, 0,
  85.                               hwnd, (HMENU) 1, hInst, NULL) ;
  86.  
  87.                LoadString (hInst, IDS_POEMRES, szPoemRes, sizeof (szPoemRes)) ;
  88.  
  89.                hResource = LoadResource (hInst, 
  90.                            FindResource (hInst, szPoemRes, "TEXT")) ;
  91.  
  92.                pText = (char *) LockResource (hResource) ;
  93.  
  94.                iNumLines = 0 ;
  95.  
  96.                while (*pText != '\\' && *pText != '\0')
  97.                     {
  98.                     if (*pText == '\n')
  99.                          iNumLines ++ ;
  100.                     pText = AnsiNext (pText) ;
  101.                     }
  102.                *pText = '\0' ;
  103.  
  104.                SetScrollRange (hScroll, SB_CTL, 0, iNumLines, FALSE) ;
  105.                SetScrollPos   (hScroll, SB_CTL, 0, FALSE) ;
  106.                return 0 ;
  107.  
  108.           case WM_SIZE :
  109.                MoveWindow (hScroll, LOWORD (lParam) - xScroll, 0,
  110.                     xScroll, cyClient = HIWORD (lParam), TRUE) ;
  111.                SetFocus (hwnd) ;
  112.                return 0 ;
  113.  
  114.           case WM_SETFOCUS :
  115.                SetFocus (hScroll) ;
  116.                return 0 ;
  117.  
  118.           case WM_VSCROLL :
  119.                switch (wParam)
  120.                     {
  121.                     case SB_TOP :
  122.                          iPosition = 0 ;
  123.                          break ;
  124.                     case SB_BOTTOM :
  125.                          iPosition = iNumLines ;
  126.                          break ;
  127.                     case SB_LINEUP :
  128.                          iPosition -= 1 ;
  129.                          break ;
  130.                     case SB_LINEDOWN :
  131.                          iPosition += 1 ;
  132.                          break ;
  133.                     case SB_PAGEUP :
  134.                          iPosition -= cyClient / cyChar ;
  135.                          break ;
  136.                     case SB_PAGEDOWN :
  137.                          iPosition += cyClient / cyChar ;
  138.                          break ;
  139.                     case SB_THUMBPOSITION :
  140.                          iPosition = LOWORD (lParam) ;
  141.                          break ;
  142.                     }
  143.                iPosition = max (0, min (iPosition, iNumLines)) ;
  144.  
  145.                if (iPosition != GetScrollPos (hScroll, SB_CTL))
  146.                     {
  147.                     SetScrollPos (hScroll, SB_CTL, iPosition, TRUE) ;
  148.                     InvalidateRect (hwnd, NULL, TRUE) ;
  149.                     }
  150.                return 0 ;
  151.  
  152.           case WM_PAINT :
  153.                hdc = BeginPaint (hwnd, &ps) ;
  154.  
  155.                pText = (char *) LockResource (hResource) ;
  156.  
  157.                GetClientRect (hwnd, &rect) ;
  158.                rect.left += cxChar ;
  159.                rect.top  += cyChar * (1 - iPosition) ;
  160.                DrawText (hdc, pText, -1, &rect, DT_EXTERNALLEADING) ;
  161.  
  162.                EndPaint (hwnd, &ps) ;
  163.                return 0 ;
  164.  
  165.           case WM_DESTROY :
  166.                FreeResource (hResource) ;
  167.                PostQuitMessage (0) ;
  168.                return 0 ;
  169.           }
  170.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  171.      }
  172.