#include
#define ID_LIST 1
#define ID_TEXT 2
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCEhPrevInstance,
PSTR szCmdLine, intiCmdShow)
{
staticTCHAR szAppName[] = TEXT ("Environ") ;
HWND hwnd ;
MSG msg ;
WNDCLASSwndclass ;
wndclass.style =CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL,IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if(!RegisterClass (&wndclass))
{
MessageBox( NULL, TEXT ("This program requires WindowsNT!"),
szAppName,MB_ICONERROR) ;
return 0;
}
hwnd =CreateWindow (szAppName, TEXT ("Environment List Box"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while(GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
returnmsg.wParam ;
}
void FillListBox (HWND hwndList)
{
int iLength ;
TCHAR *pVarBlock, * pVarBeg, * pVarEnd, * pVarName ;
pVarBlock =GetEnvironmentStrings () ; // Get pointer to environment block
while(*pVarBlock)
{
if(*pVarBlock != '=') // Skip variable namesbeginning with '='
{
pVarBeg = pVarBlock ; // Beginning of variable name
while (*pVarBlock++ != '=') ; // Scan until'='
pVarEnd = pVarBlock - 1 ; // Points to '=' sign
iLength = pVarEnd - pVarBeg ; // Length ofvariable name
//Allocate memory for the variable name and terminating
// zero.Copy the variable name and append a zero.
pVarName =(TCHAR*)calloc (iLength + 1, sizeof (TCHAR)) ;
CopyMemory(pVarName, pVarBeg, iLength * sizeof (TCHAR)) ;
pVarName[iLength] = '\0' ;
// Put thevariable name in the list box and free memory.
SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) pVarName);
free(pVarName) ;
}
while(*pVarBlock++ != '\0') ; // Scan until terminating zero
}
FreeEnvironmentStrings (pVarBlock) ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAMwParam,LPARAM lParam)
{
static HWND hwndList, hwndText ;
int iIndex, iLength, cxChar,cyChar;
TCHAR *pVarName, *pVarValue ;
switch(message)
{
case WM_CREATE :
cxChar =LOWORD (GetDialogBaseUnits ()) ;
cyChar =HIWORD (GetDialogBaseUnits ()) ;
// Createlistbox and static text windows.
hwndList =CreateWindow (TEXT ("listbox"), NULL,
WS_CHILD | WS_VISIBLE | LBS_STANDARD,
cxChar,cyChar * 3,
cxChar * 16 + GetSystemMetrics(SM_CXVSCROLL),
cyChar * 5,
hwnd, (HMENU) ID_LIST,
(HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),NULL) ;
//hwnd =CreateWindow(TEXT("static"),NULL:这个TEXT的内容不是随便加的。
hwndText = CreateWindow (TEXT ("edit"),NULL,
WS_CHILD | WS_VISIBLE | ES_LEFT,
cxChar, cyChar,
GetSystemMetrics (SM_CXSCREEN), cyChar,
hwnd, (HMENU) ID_TEXT,
(HINSTANCE) GetWindowLong (hwnd,GWL_HINSTANCE),
NULL) ;
FillListBox (hwndList) ;
return 0;
case WM_SETFOCUS :
SetFocus(hwndList);
return 0;
case WM_COMMAND :
//确定下拉清单,并确定选中消息发生变化
if (LOWORD(wParam) == ID_LIST && HIWORD (wParam) ==LBN_SELCHANGE)
{
// Get currentselection.
//返回清单中当前选中项的索引
iIndex = SendMessage (hwndList,LB_GETCURSEL, 0, 0) ;
//返回清单中索引号为Index的字符串长度
iLength = SendMessage (hwndList, LB_GETTEXTLEN,iIndex, 0) + 1 ;
pVarName =(TCHAR*)calloc (iLength, sizeof (TCHAR)) ;
//将清单中索引号为Index的字符串赋值到pVarName
SendMessage (hwndList, LB_GETTEXT, iIndex,(LPARAM) pVarName) ;
// Getenvironment string.
//获取环境变量的长度,第2个参数表示输出的环境变量值,第3个参数表示缓冲字符串的长度
iLength = GetEnvironmentVariable (pVarName,NULL, 0) ;
pVarValue = (TCHAR*)calloc (iLength, sizeof(TCHAR)) ;
GetEnvironmentVariable (pVarName, pVarValue,iLength) ;
// Show it in window.
SetWindowText (hwndText, pVarValue) ;
free (pVarName) ;
free (pVarValue) ;
}
return 0;
case WM_DESTROY :
PostQuitMessage (0) ;
return 0;
}
returnDefWindowProc (hwnd, message, wParam, lParam) ;
}