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

  1. /*--------------------------------------------------------
  2.    STRPROG.C -- Program using STRLIB dynamic link library
  3.                 (c) Charles Petzold, 1990
  4.   --------------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <string.h>
  8. #include "strprog.h"
  9.  
  10. #define MAXLEN 32
  11. #define WM_DATACHANGE WM_USER
  12.  
  13. typedef struct
  14.      {
  15.      HDC   hdc ;
  16.      short xText ;
  17.      short yText ;
  18.      short xStart ;
  19.      short yStart ;
  20.      short xIncr ;
  21.      short yIncr ;
  22.      short xMax ;
  23.      short yMax ;
  24.      }
  25.      CBPARM ;
  26.  
  27. BOOL  FAR PASCAL AddString    (LPSTR) ;      // functions in STRLIB
  28. BOOL  FAR PASCAL DeleteString (LPSTR) ;
  29. short FAR PASCAL GetStrings   (FARPROC, CBPARM FAR *) ;
  30.  
  31. long  FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;
  32.  
  33. char  szAppName [] = "StrProg" ;
  34. char  szString  [MAXLEN] ;
  35.  
  36. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  37.                     LPSTR lpszCmdLine, int nCmdShow)
  38.      {
  39.      HWND     hwnd ;
  40.      MSG      msg ;
  41.      WNDCLASS wndclass ;
  42.  
  43.      if (!hPrevInstance) 
  44.           {
  45.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  46.           wndclass.lpfnWndProc   = WndProc ;
  47.           wndclass.cbClsExtra    = 0 ;
  48.           wndclass.cbWndExtra    = 0 ;
  49.           wndclass.hInstance     = hInstance ;
  50.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  51.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  52.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  53.           wndclass.lpszMenuName  = szAppName ;
  54.           wndclass.lpszClassName = szAppName ;
  55.  
  56.           RegisterClass (&wndclass) ;
  57.           }
  58.  
  59.      hwnd = CreateWindow (szAppName, "DLL Demonstration Program",
  60.                           WS_OVERLAPPEDWINDOW,
  61.                           CW_USEDEFAULT, CW_USEDEFAULT,
  62.                           CW_USEDEFAULT, CW_USEDEFAULT,
  63.                           NULL, NULL, hInstance, NULL) ;
  64.  
  65.      ShowWindow (hwnd, nCmdShow) ;
  66.      UpdateWindow (hwnd) ;
  67.  
  68.      while (GetMessage (&msg, NULL, 0, 0))
  69.           {
  70.           TranslateMessage (&msg) ;
  71.           DispatchMessage (&msg) ;
  72.           }
  73.      return msg.wParam ;
  74.      }
  75.  
  76. BOOL FAR PASCAL DlgProc (HWND hDlg, WORD message, WORD wParam, LONG lParam)
  77.      {
  78.      switch (message)
  79.           {
  80.           case WM_INITDIALOG:
  81.                SendDlgItemMessage (hDlg, IDD_STRING, EM_LIMITTEXT,
  82.                                    MAXLEN - 1, 0L);
  83.                return TRUE ;
  84.  
  85.           case WM_COMMAND:
  86.                switch (wParam)
  87.                     {
  88.                     case IDOK:
  89.                          GetDlgItemText (hDlg, IDD_STRING, szString, MAXLEN) ;
  90.                          EndDialog (hDlg, TRUE) ;
  91.                          return TRUE ;
  92.  
  93.                     case IDCANCEL:
  94.                          EndDialog (hDlg, FALSE) ;
  95.                          return TRUE ;
  96.                     }
  97.           }
  98.      return FALSE ;
  99.      }
  100.  
  101. BOOL FAR PASCAL EnumCallBack (HWND hwnd, LONG lParam)
  102.      {
  103.      char szClassName [16] ;
  104.  
  105.      GetClassName (hwnd, szClassName, sizeof szClassName) ;
  106.  
  107.      if (0 == strcmp (szClassName, szAppName))
  108.           SendMessage (hwnd, WM_DATACHANGE, 0, 0L) ;
  109.  
  110.      return TRUE ;
  111.      }
  112.  
  113. BOOL FAR PASCAL GetStrCallBack (LPSTR lpString, CBPARM FAR *lpcbp)
  114.      {
  115.      TextOut (lpcbp->hdc, lpcbp->xText, lpcbp->yText,
  116.               lpString, lstrlen (lpString)) ;
  117.  
  118.      if ((lpcbp->yText += lpcbp->yIncr) > lpcbp->yMax)
  119.           {
  120.           lpcbp->yText = lpcbp->yStart ;
  121.           if ((lpcbp->xText += lpcbp->xIncr) > lpcbp->xMax)
  122.                return FALSE ;
  123.           }
  124.      return TRUE ;
  125.      }
  126.  
  127. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  128.      {
  129.      static FARPROC lpfnDlgProc, lpfnGetStrCallBack, lpfnEnumCallBack ;
  130.      static HANDLE  hInst ;
  131.      static short   cxChar, cyChar, cxClient, cyClient ;
  132.      CBPARM         cbparam ;
  133.      HDC            hdc ;
  134.      PAINTSTRUCT    ps ;
  135.      TEXTMETRIC     tm ;
  136.  
  137.      switch (message)
  138.           {
  139.           case WM_CREATE:
  140.                hInst = ((LPCREATESTRUCT) lParam)->hInstance ;
  141.  
  142.                lpfnDlgProc        = MakeProcInstance (DlgProc, hInst) ;
  143.                lpfnGetStrCallBack = MakeProcInstance (GetStrCallBack, hInst) ;
  144.                lpfnEnumCallBack   = MakeProcInstance (EnumCallBack, hInst) ;
  145.  
  146.                hdc = GetDC (hwnd) ;
  147.                GetTextMetrics (hdc, &tm) ;
  148.                cxChar = tm.tmAveCharWidth ;
  149.                cyChar = tm.tmHeight + tm.tmExternalLeading ;
  150.                ReleaseDC (hwnd, hdc) ;
  151.  
  152.                return 0 ;
  153.  
  154.           case WM_COMMAND:
  155.                switch (wParam)
  156.                     {
  157.                     case IDM_ENTER:
  158.                          if (DialogBox (hInst, "EnterDlg", hwnd, lpfnDlgProc))
  159.                               {
  160.                               if (AddString (szString))
  161.                                    EnumWindows (lpfnEnumCallBack, 0L) ;
  162.                               else
  163.                                    MessageBeep (0) ;
  164.                               }
  165.                          break ;
  166.  
  167.                     case IDM_DELETE:
  168.                          if (DialogBox (hInst, "DeleteDlg", hwnd, lpfnDlgProc))
  169.                               {
  170.                               if (DeleteString (szString))
  171.                                    EnumWindows (lpfnEnumCallBack, 0L) ;
  172.                               else
  173.                                    MessageBeep (0) ;
  174.                               }
  175.                          break ;
  176.                     }
  177.                return 0 ;
  178.  
  179.           case WM_SIZE:
  180.                cxClient = LOWORD (lParam) ;
  181.                cyClient = HIWORD (lParam) ;
  182.                return 0 ;
  183.  
  184.           case WM_DATACHANGE:
  185.                InvalidateRect (hwnd, NULL, TRUE) ;
  186.                return 0 ;
  187.  
  188.           case WM_PAINT:
  189.                hdc = BeginPaint (hwnd, &ps) ;
  190.  
  191.                cbparam.hdc   = hdc ;
  192.                cbparam.xText = cbparam.xStart = cxChar ;
  193.                cbparam.yText = cbparam.yStart = cyChar ;
  194.                cbparam.xIncr = cxChar * MAXLEN ;
  195.                cbparam.yIncr = cyChar ;
  196.                cbparam.xMax  = cbparam.xIncr * (1 + cxClient / cbparam.xIncr) ;
  197.                cbparam.yMax  = cyChar * (cyClient / cyChar - 1) ;
  198.  
  199.                GetStrings (lpfnGetStrCallBack, &cbparam) ;
  200.  
  201.                EndPaint (hwnd, &ps) ;
  202.                return 0 ;
  203.  
  204.           case WM_DESTROY:
  205.                PostQuitMessage (0) ;
  206.                return 0 ;
  207.           }
  208.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  209.      }
  210.