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

  1. /*----------------------------------------
  2.    ENVIRON.C -- Environment List Box
  3.                 (c) Charles Petzold, 1990
  4.   ----------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #define  MAXENV  4096
  10.  
  11. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;
  12.  
  13. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  14.                     LPSTR lpszCmdLine, int nCmdShow)
  15.      {
  16.      static char szAppName[] = "Environ" ;
  17.      HWND        hwnd ;
  18.      MSG         msg ;
  19.      WNDCLASS    wndclass ;
  20.  
  21.      if (!hPrevInstance) 
  22.           {
  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 = COLOR_WINDOW + 1 ;
  31.           wndclass.lpszMenuName  = NULL ;
  32.           wndclass.lpszClassName = szAppName ;
  33.  
  34.           RegisterClass (&wndclass) ;
  35.           }
  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, nCmdShow) ;
  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. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  55.      {
  56.      static char szBuffer [MAXENV + 1] ;
  57.      static HWND hwndList, hwndText ;
  58.      HDC         hdc ;
  59.      TEXTMETRIC  tm ;
  60.      WORD        n ;
  61.  
  62.      switch (message)
  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, 1,
  76.                               GetWindowWord (hwnd, GWW_HINSTANCE), NULL) ;
  77.  
  78.                hwndText = CreateWindow ("static", NULL,
  79.                               WS_CHILD | WS_VISIBLE | SS_LEFT,
  80.                               tm.tmAveCharWidth,          tm.tmHeight,
  81.                               tm.tmAveCharWidth * MAXENV, tm.tmHeight,
  82.                               hwnd, 2,
  83.                               GetWindowWord (hwnd, GWW_HINSTANCE), NULL) ;
  84.  
  85.                for (n = 0 ; environ[n] ; n++)
  86.                     {
  87.                     if (strlen (environ [n]) > MAXENV)
  88.                          continue ;
  89.                     *strchr (strcpy (szBuffer, environ [n]), '=') = '\0' ;
  90.                     SendMessage (hwndList, LB_ADDSTRING, 0,
  91.                                  (LONG) (LPSTR) szBuffer) ;
  92.                     }
  93.                return 0 ;
  94.  
  95.           case WM_SETFOCUS:
  96.                SetFocus (hwndList) ;
  97.                return 0 ;
  98.  
  99.           case WM_COMMAND:
  100.                if (wParam == 1 && HIWORD (lParam) == LBN_SELCHANGE)
  101.                     {
  102.                     n = (WORD) SendMessage (hwndList, LB_GETCURSEL, 0, 0L) ;
  103.                     n = (WORD) SendMessage (hwndList, LB_GETTEXT, n,
  104.                                             (LONG) (LPSTR) szBuffer) ;
  105.  
  106.                     strcpy (szBuffer + n + 1, getenv (szBuffer)) ;
  107.                     *(szBuffer + n) = '=' ;
  108.  
  109.                     SetWindowText (hwndText, szBuffer) ;
  110.                     }
  111.                return 0 ;
  112.  
  113.           case WM_DESTROY:
  114.                PostQuitMessage (0) ;
  115.                return 0 ;
  116.           }
  117.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  118.      }
  119.