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

  1. /*----------------------------------------
  2.    ENVIRON.C -- Environment List Box
  3.                 (c) Charles Petzold, 1996
  4.   ----------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #define  MAXENV  4096
  11.  
  12. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  13.  
  14. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  15.                     PSTR szCmdLine, int iCmdShow)
  16.      {
  17.      static char szAppName[] = "Environ" ;
  18.      HWND        hwnd ;
  19.      MSG         msg ;
  20.      WNDCLASSEX  wndclass ;
  21.  
  22.      wndclass.cbSize        = sizeof (wndclass) ;
  23.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  24.      wndclass.lpfnWndProc   = WndProc ;
  25.      wndclass.cbClsExtra    = 0 ;
  26.      wndclass.cbWndExtra    = 0 ;
  27.      wndclass.hInstance     = hInstance ;
  28.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  29.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  30.      wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1) ;
  31.      wndclass.lpszMenuName  = NULL ;
  32.      wndclass.lpszClassName = szAppName ;
  33.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  34.  
  35.      RegisterClassEx (&wndclass) ;
  36.  
  37.      hwnd = CreateWindow (szAppName, "Environment List Box",
  38.                           WS_OVERLAPPEDWINDOW,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           NULL, NULL, hInstance, NULL) ;
  42.  
  43.      ShowWindow (hwnd, iCmdShow) ;
  44.      UpdateWindow (hwnd) ;
  45.  
  46.      while (GetMessage (&msg, NULL, 0, 0))
  47.           {
  48.           TranslateMessage (&msg) ;
  49.           DispatchMessage (&msg) ;
  50.           }
  51.      return msg.wParam ;
  52.      }
  53.  
  54. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  55.      {
  56.      static char szBuffer[MAXENV + 1] ;
  57.      static HWND hwndList, hwndText ;
  58.      HDC         hdc ;
  59.      int         i ;
  60.      TEXTMETRIC  tm ;
  61.  
  62.      switch (iMsg)
  63.           {
  64.           case WM_CREATE :
  65.                hdc = GetDC (hwnd) ;
  66.                GetTextMetrics (hdc, &tm) ;
  67.                ReleaseDC (hwnd, hdc) ;
  68.  
  69.                hwndList = CreateWindow ("listbox", NULL,
  70.                               WS_CHILD | WS_VISIBLE | LBS_STANDARD,
  71.                               tm.tmAveCharWidth, tm.tmHeight * 3,
  72.                               tm.tmAveCharWidth * 16 +
  73.                                    GetSystemMetrics (SM_CXVSCROLL),
  74.                               tm.tmHeight * 5,
  75.                               hwnd, (HMENU) 1,
  76.                               (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
  77.                               NULL) ;
  78.  
  79.                hwndText = CreateWindow ("static", NULL,
  80.                               WS_CHILD | WS_VISIBLE | SS_LEFT,
  81.                               tm.tmAveCharWidth,          tm.tmHeight,
  82.                               tm.tmAveCharWidth * MAXENV, tm.tmHeight,
  83.                               hwnd, (HMENU) 2,
  84.                               (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
  85.                               NULL) ;
  86.  
  87.                for (i = 0 ; environ[i] ; i++)
  88.                     {
  89.                     if (strlen (environ [i]) > MAXENV)
  90.                          continue ;
  91.                     *strchr (strcpy (szBuffer, environ [i]), '=') = '\0' ;
  92.                     SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) szBuffer) ;
  93.                     }
  94.                return 0 ;
  95.  
  96.           case WM_SETFOCUS :
  97.                SetFocus (hwndList) ;
  98.                return 0 ;
  99.  
  100.           case WM_COMMAND :
  101.                if (LOWORD (wParam) == 1 && HIWORD (wParam) == LBN_SELCHANGE)
  102.                     {
  103.                     i = SendMessage (hwndList, LB_GETCURSEL, 0, 0) ;
  104.                     i = SendMessage (hwndList, LB_GETTEXT, i,
  105.                                                (LPARAM) szBuffer) ;
  106.  
  107.                     strcpy (szBuffer + i + 1, getenv (szBuffer)) ;
  108.                     *(szBuffer + i) = '=' ;
  109.  
  110.                     SetWindowText (hwndText, szBuffer) ;
  111.                     }
  112.                return 0 ;
  113.  
  114.           case WM_DESTROY :
  115.                PostQuitMessage (0) ;
  116.                return 0 ;
  117.           }
  118.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  119.      }
  120.