引爆ywt 引爆ywt
菜鸟
关注数: 1 粉丝数: 3 发帖数: 1,594 关注贴吧数: 4
求助,Windows程序设计第五版里的一个题目,运行不正确 是第九章的倒数第二个程序,ENVIRON,代码如下: #include<windows.h>#define ID_LIST 1#define ID_TEXT 2 LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, PSTR szCmdLine,int iCmdShow){static TCHAR szAppName[]=TEXT("Environ");HWND hwnd;MSG msg;WNDCLASS wndclass; 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 Windows NT!"), szAppName,MB_ICONERROR); return 0;} hwnd=CreateWindow(szAppName,TEXT("Environmrnt 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);}return msg.wParam;} void FillListBox(HWND hwndList){int iLength;TCHAR*pVarBlock,*pVarBeg,*pVarEnd,*pVarName; pVarBlock=GetEnvironmentStrings(); while(*pVarBlock){if(*pVarBlock!='='){pVarBeg=pVarBlock;while(*pVarBlock++!='=');pVarEnd=pVarBlock-1;iLength=pVarEnd-pVarBeg; pVarName =(TCHAR*) calloc(iLength+1,sizeof(TCHAR));CopyMemory(pVarName,pVarBeg,iLength*sizeof(TCHAR));pVarName[iLength]='\0'; SendMessage(hwndList,LB_ADDSTRING,0,(LPARAM)pVarName);free(pVarName);}while(*pVarBlock++!='\0');}FreeEnvironmentStrings(pVarBlock);} LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,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()); 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); hwndText=CreateWindow(TEXT("static"),NULL,WS_CHILD|WS_VISIBLE|SS_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){iIndex=SendMessage(hwndList,LB_GETCURSEL,0,0);iLength=SendMessage(hwndList,LB_GETTEXTLEN,iIndex,0)+1;pVarName=(TCHAR*)calloc(iLength,sizeof(TCHAR));SendMessage(hwndList,LB_GETTEXT,iIndex,(LPARAM)pVarName); iLength=GetEnvironmentVariable(pVarName,NULL,0);pVarValue=(TCHAR*)calloc(iLength,sizeof(TCHAR));GetEnvironmentVariable(pVarName,pVarValue,iLength); SetWindowText(hwndText,pVarValue);free(pVarName);free(pVarValue);}return 0; case WM_DESTROY:PostQuitMessage(0);return 0;}return DefWindowProc(hwnd,message,wParam,lParam);} (我去,我也不知道为啥就这样了) 其中做了点修改,要不然就报错,可是还是没有结果。麻烦哪位大神运行一下,这是为什么啊?谢谢阿!
Windows程序设计第五版中的程序Checker3,好像出了点问题…… 源代码如下! #include<windows.h> # define DIVISIONS 5 LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); LRESULT CALLBACK ChildWndProc(HWND,UINT,WPARAM,LPARAM); TCHAR szChildClass[]=TEXT("Checker3_Child"); int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, PSTR szCmdLine,int iCmdShow) { static TCHAR szAppName[]=TEXT("Checker3"); HWND hwnd; MSG msg; WNDCLASS wndclass; 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)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName=NULL; wndclass.lpszClassName=szAppName; if(!RegisterClass(&wndclass)) { MessageBox(NULL,TEXT("This program requires Windows NT!"), szAppName,MB_ICONERROR); return 0; } wndclass.lpfnWndProc=ChildWndProc; wndclass.cbWndExtra=sizeof(long); wndclass.hIcon=NULL; wndclass.lpszClassName=szChildClass; RegisterClass(&wndclass); hwnd=CreateWindow(szAppName,TEXT("Checker3 Mouse Hit-Test Demo"), 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); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) { static HWND hwndChild[DIVISIONS][DIVISIONS]; int cxBlock,cyBlock,x,y; switch(message) { case WM_CREATE: for(x=0;x<DIVISIONS;x++) for(y=0;y<DIVISIONS;y++) hwndChild[x][y]=CreateWindow(szChildClass,NULL,WS_CHILDWINDOW|WS_VISIBLE,0,0,0,0,hwnd,(HMENU)(y<<8|x),(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL);return 0; case WM_SIZE: cxBlock=LOWORD(lParam)/DIVISIONS; cyBlock=HIWORD(lParam)/DIVISIONS; for(x=0;x<DIVISIONS;x++) for(y=0;y<DIVISIONS;y++) MoveWindow(hwndChild[x][y], x*cxBlock,y*cyBlock,cxBlock,cyBlock,TRUE); return 0; case WM_LBUTTONDOWN: MessageBeep(0); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd,message,wParam,lParam); } LRESULT CALLBACK ChildWndProc(HWND hwnd,UINT message, WPARAM wParam,LPARAM,lParam) { HDC hdc; PAINTSTRUCT ps; RECT rect; switch(message) { case WM_CREATE: SetWindowLong(hwnd,0,0); return 0; case WM_LBUTTONDOWN: SetWindowLong(hwnd,0,1^GetWindowLong(hwnd,0)); InvalidateRect(hwnd,NULL,FALSE); return 0; case WM_PAINT: hdc=BeginPaint(hwnd,&ps); GetClientRect(hwnd,&rect); Rectangle(hdc,0,0,rect.right,rect.bottom); if(GetWindowLong(hwnd,0)) { MoveToEx(hdc,0,0,NULL); LineTo(hdc,rect.right,rect.bottom); MoveToEx(hdc,0,rect.bottom,NULL); LineTo(hdc,rect.right,0);} EndPaint(hwnd,&ps); return 0; } return DefWindowProc(hwnd,message,wParam,lParam); }
1 下一页