《Windows程序设计》之列表框控件

这部分代码是书中HEAD的部分,里面的东西值得细细去理会

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
	static BOOL bValidFile;
	static BYTE buffer[MAXREAD];
	static HWND hwndList,hwndText;
	static RECT rect;
	static TCHAR szFile[MAX_PATH+1];
	HANDLE hFile;
	HDC hdc;
	int i,cxChar,cyChar;
	PAINTSTRUCT ps;
	TCHAR szBuffer[MAX_PATH+1];
	switch(message)
	{
	case WM_CREATE:
		//系统字体中字符的宽高
		cxChar=LOWORD(GetDialogBaseUnits());
		cyChar=HIWORD(GetDialogBaseUnits());
		//矩形左上角坐标
		rect.left=20*cxChar;
		rect.top=3*cyChar;
		//创建列表框控件,并设置位置,大小
		hwndList=CreateWindow(TEXT("listbox"),NULL,WS_CHILDWINDOW|WS_VISIBLE|LBS_STANDARD,cxChar,cyChar*3,
						cxChar*13+GetSystemMetrics(SM_CXVSCROLL),cyChar*10,hwnd,(HMENU)ID_LIST,(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL);
		//获取当前路径
		GetCurrentDirectory(MAX_PATH+1,szBuffer);
		//创建文本框控件,并设置位置,大小
		hwndText=CreateWindow(TEXT("static"),szBuffer,WS_CHILDWINDOW|WS_VISIBLE|SS_LEFT,cxChar,cyChar,cxChar*MAX_PATH,cyChar,hwnd,(HMENU)ID_TEXT,
						(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL);
		//回调
		OldList=(WNDPROC)SetWindowLong(hwndList,GWL_WNDPROC,(LPARAM)ListProc);
		//List Box最强大的一条消息:LB_DIR
		//DIRATTR是文件属性,"*.*"是文件匹配名
		SendMessage(hwndList,LB_DIR,DIRATTR,(LPARAM)TEXT("*.*"));
		return 0;
	case WM_SIZE:
		rect.right=LOWORD(lParam);
		rect.bottom=HIWORD(lParam);
		return 0;
	case WM_SETFOCUS:
		SetFocus(hwndList);
		return 0;
	case WM_COMMAND:
		if(LOWORD(wParam)==ID_LIST&&HIWORD(wParam)==LBN_DBLCLK)
		{
			//获取双击的是哪个
			if(LB_ERR==(i=SendMessage(hwndList,LB_GETCURSEL,0,0)))
				break;
			//获取文本
			SendMessage(hwndList,LB_GETTEXT,i,(LPARAM)szBuffer);
			if(INVALID_HANDLE_VALUE!=(hFile=CreateFile(szBuffer,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL)))
			{
				//为文件
				CloseHandle(hFile);
				bValidFile=TRUE;
				lstrcpy(szFile,szBuffer);
				GetCurrentDirectory(MAX_PATH+1,szBuffer);
				if(szBuffer[lstrlen(szBuffer)-1]!='\\')
					lstrcat(szBuffer,TEXT("\\"));
				SetWindowText(hwndText,lstrcat(szBuffer,szFile));
			}
			else
			{
				//为文件夹
				bValidFile=FALSE;
				szBuffer[lstrlen(szBuffer)-1]='\0';
				if(!SetCurrentDirectory(szBuffer+1))
				{
					szBuffer[3]=':';
					szBuffer[4]='\0';
					SetCurrentDirectory(szBuffer+2);
				}
				GetCurrentDirectory(MAX_PATH+1,szBuffer);
				SetWindowText(hwndText,szBuffer);
				SendMessage(hwndList,LB_RESETCONTENT,0,0);
				SendMessage(hwndList,LB_DIR,DIRATTR,(LPARAM)TEXT("*.*"));
			}
			//触发WM_PAINT
			InvalidateRect(hwnd,NULL,TRUE);
		}
		return 0;
	case WM_PAINT:
		if(!bValidFile)
			break;
		if(INVALID_HANDLE_VALUE==(hFile=CreateFile(szFile,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL)))
		{
			bValidFile=FALSE;
			break;
		}
		//读取文件内容
		ReadFile(hFile,buffer,MAXREAD,(LPDWORD)&i,NULL);
		CloseHandle(hFile);
		hdc=BeginPaint(hwnd,&ps);
		SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT));
		SetTextColor(hdc,GetSysColor(COLOR_BTNTEXT));
		SetBkColor(hdc,GetSysColor(COLOR_BTNFACE));
		//打印到窗口
		DrawTextA(hdc,(LPCSTR)buffer,i,&rect,DTFLAGS);
		EndPaint(hwnd,&ps);
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hwnd,message,wParam,lParam);
}



你可能感兴趣的:(《Windows程序设计》之列表框控件)