我不明的是为何在case语句中是用msg.Lparam的值来判断?<br>具体用什么来判断要看具体是什么消息,<br>给你个例子既用到了wparam,又用到了lparam<br>建议看一看<<Windows程序设计>>这本书,我是初学者,我也正在看,上面写得很清楚.<br>还要看MSDN,或者Delphi自带的SDK帮助.<br>给你个例子既用到了wparam,又用到了lparam<br><br>program SysMets4;<br><br>uses<br> Windows, Messages, Math, SysMets;<br><br> // 窗体消息处理回调函数<br>function WndProc(hWnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;<br>const<br>{$J+}<br> cxChar: Integer = 0; // 小写字母宽度<br> cxCaps: Integer = 0; // 大写字母宽度<br> cyChar: Integer = 0; // 小写字母高度<br> cxClient: Integer = 0; // 窗体客户区宽度<br> cyClient: Integer = 0; // 窗体客户区高度<br> iMaxWidth: Integer = 0; // 文字输出行宽度<br>{$J-} <br>var<br> X, Y, J, L, iVertPos, iHorzPos, iPaintBeg, iPaintEnd: Integer;<br> Buffer: array [0..5] of Char; // 输出缓冲区 <br> hWndDc: HDC;<br> PS: TPaintStruct;<br> TM: TTextMetric;<br> SI: TScrollInfo;<br>begin<br> Result := 0;<br> case Msg of<br> WM_CREATE : begin<br> hWndDc := GetDC(hWnd);<br><br> GetTextMetrics(hWndDc, TM); // 取得字体度量信息<br> cxChar := TM.tmAveCharWidth;<br> if (TM.tmPitchAndFamily and $1) = 0 // 宽度固定?<br> then cxCaps := cxChar<br> else cxCaps := cxChar * 3 div 2;<br> cyChar := TM.tmHeight + TM.tmExternalLeading;<br> iMaxWidth := 40 * cxChar + 22 * cxCaps;<br><br> ReleaseDC(hWnd, hWndDc);<br> end;<br><br> WM_SIZE : begin<br> cxClient := LOWORD(lParam);<br> cyClient := HIWORD(lParam);<br> // 设置竖直卷动条范围和单页大小<br> SI.cbSize := SizeOf(SI);<br> SI.fMask := SIF_RANGE or SIF_PAGE;<br> SI.nMin := 0;<br> SI.nMax := NUMLINES - 1;<br> SI.nPage := cyClient div cyChar;<br> SetScrollInfo(hWnd, SB_VERT, SI, TRUE);<br> // 设置横向卷动条范围和单页大小<br> SI.cbSize := SizeOf(SI);<br> SI.fMask := SIF_RANGE or SIF_PAGE;<br> SI.nMin := 0;<br> SI.nMax := 2 + iMaxWidth div cxChar;<br> SI.nPage := cxClient div cxChar;<br> SetScrollInfo(hWnd, SB_HORZ, SI, TRUE);<br> end;<br> <br> WM_VSCROLL : begin<br> // 取得竖直卷动条全部信息<br> SI.cbSize := SizeOf(SI);<br> SI.fMask := SIF_ALL;<br> GetScrollInfo(hWnd, SB_VERT, SI);<br> // 暂存竖直卷动条位置信息<br> iVertPos := SI.nPos;<br> // 区分不同的卷动类型<br> case LOWORD(wParam) of<br> SB_TOP : SI.nPos := SI.nMin;<br> SB_BOTTOM : SI.nPos := SI.nMax;<br> SB_LINEUP : SI.nPos := SI.nPos-1;<br> SB_LINEDOWN : SI.nPos := SI.nPos+1;<br> SB_PAGEUP : SI.nPos := SI.nPos-SI.nPage;<br> SB_PAGEDOWN : SI.nPos := SI.nPos+SI.nPage;<br> SB_THUMBTRACK: SI.nPos := SI.nTrackPos;<br> end;<br> SI.fMask := SIF_POS;<br> SetScrollInfo(hWnd, SB_VERT, SI, TRUE);<br> GetScrollInfo(hWnd, SB_VERT, SI);<br> // 与以前不同则上下滚动(产生无效区域)<br> if (SI.nPos <> iVertPos) then<br> begin<br> ScrollWindow(hWnd, 0, cyChar * (iVertPos - SI.nPos), nil, nil);<br> UpdateWindow(hWnd);<br> end;<br> end;<br><br> WM_HSCROLL : begin<br> // 取得横向卷动条全部信息<br> SI.cbSize := SizeOf(SI);<br> SI.fMask := SIF_ALL;<br> GetScrollInfo(hWnd, SB_HORZ, SI);<br> // 暂存横向卷动条位置信息<br> iHorzPos := SI.nPos;<br> // 区分不同的卷动类型<br> case LOWORD(wParam) of<br> SB_LINELEFT : SI.nPos := SI.nPos-1;<br> SB_LINERIGHT : SI.nPos := SI.nPos+1;<br> SB_PAGELEFT : SI.nPos := SI.nPos-SI.nPage;<br> SB_PAGERIGHT : SI.nPos := SI.nPos+SI.nPage;<br> SB_THUMBPOSITION: SI.nPos := SI.nTrackPos;<br> end;<br> SI.fMask := SIF_POS;<br> SetScrollInfo(hWnd, SB_HORZ, SI, TRUE);<br> GetScrollInfo(hWnd, SB_HORZ, SI);<br> // 与以前不同则左右滚动(产生无效区域)<br> if (SI.nPos <> iHorzPos) then<br> begin<br> ScrollWindow(hWnd, cxChar * (iHorzPos - SI.nPos), 0, nil, nil);<br> UpdateWindow(hWnd);<br> end;<br> end;<br><br> WM_KEYDOWN : begin // 转发滚动消息<br> case wParam of<br> VK_HOME : SendMessage(hWnd, WM_VSCROLL, SB_TOP, 0);<br> VK_END : SendMessage(hWnd, WM_VSCROLL, SB_BOTTOM, 0);<br> VK_PRIOR : SendMessage(hWnd, WM_VSCROLL, SB_PAGEUP, 0);<br> VK_NEXT : SendMessage(hWnd, WM_VSCROLL, SB_PAGEDOWN, 0);<br> VK_UP : SendMessage(hWnd, WM_VSCROLL, SB_LINEUP, 0);<br> VK_DOWN : SendMessage(hWnd, WM_VSCROLL, SB_LINEDOWN, 0);<br> VK_LEFT : SendMessage(hWnd, WM_HSCROLL, SB_PAGEUP, 0);<br> VK_RIGHT : SendMessage(hWnd, WM_HSCROLL, SB_PAGEDOWN, 0);<br> end;<br> end;<br><br> WM_PAINT : begin<br> hWndDc := BeginPaint(hWnd, PS);<br> <br> SI.cbSize := SizeOf(SI) ;<br> SI.fMask := SIF_POS;<br> // 取得竖直卷动条位置<br> GetScrollInfo(hWnd, SB_VERT, SI);<br> iVertPos := SI.nPos ;<br> // 取得横向卷动条位置<br> GetScrollInfo(hWnd, SB_HORZ, SI);<br> iHorzPos := SI.nPos;<br> // 计算需要重绘的范围(行)<br> iPaintBeg := Max(0, iVertPos + PS.rcPaint.Top div cyChar);<br> iPaintEnd := Min(NUMLINES - 1, iVertPos + PS.rcPaint.Bottom div cyChar);<br> // 依次绘制各行<br> for J := iPaintBeg to iPaintEnd do<br> begin<br> X := cxChar * (1 - iHorzPos); // 本行横坐标(左上角)<br> Y := cyChar * (J - iVertPos); // 本行纵坐标(左上角)<br> TextOut(hWndDc, X, Y, SysMetrics[J].szLabel, lstrlen(SysMetrics[J].szLabel));<br> TextOut(hWndDc, X + 22 * cxCaps, Y, SysMetrics[J].szDesc, lstrlen(SysMetrics[J].szDesc));<br> SetTextAlign(hWndDc, TA_RIGHT or TA_TOP); // 右上对齐<br> L := GetSystemMetrics(SysMetrics[J].Index);<br> TextOut(hWndDc, X + 22 * cxCaps + 40 * cxChar , Y, Buffer, wvsprintf(Buffer, '%5d', @L));<br> SetTextAlign(hWndDc, TA_LEFT or TA_TOP); // 左上对齐<br> end;<br><br> EndPaint(hWnd, PS);<br> end;<br> <br> WM_DESTROY : PostQuitMessage(0); // 退出循环<br><br> else Result := DefWindowProc(hWnd, Msg, wParam, lParam); // 默认处理<br> end;<br>end;<br><br><br>const<br> AppName = 'SysMets4';<br> <br>var<br> hWnd: LongWord;<br> Msg: TMsg;<br> WndClass: TWndClass;<br><br>begin<br> // 填充结构体<br> WndClass.style := CS_HREDRAW or CS_VREDRAW;<br> WndClass.lpfnWndProc := @WndProc;<br> WndClass.cbClsExtra := 0;<br> WndClass.cbWndExtra := 0;<br> WndClass.hInstance := hInstance;<br> WndClass.hIcon := LoadIcon(0, IDI_APPLICATION);<br> WndClass.hCursor := LoadCursor(0, IDC_ARROW);<br> WndClass.hbrBackground := GetStockObject(WHITE_BRUSH);<br> WndClass.lpszMenuName := nil;<br> WndClass.lpszClassName := AppName;<br><br> // 注册窗体类<br> if (RegisterClass(WndClass) = 0) then<br> begin<br> MessageBox(0,'Program requires Windows NT!', AppName, MB_ICONERROR);<br> Exit;<br> end;<br><br> // 建立窗体<br> hWnd := CreateWindow(AppName, 'Get System Metrics No. 4',<br> WS_OVERLAPPEDWINDOW or WS_VSCROLL or WS_HSCROLL,<br> CW_USEDEFAULT, CW_USEDEFAULT,<br> CW_USEDEFAULT, CW_USEDEFAULT,<br> 0, 0, hInstance, nil);<br><br> // 显示窗体<br> ShowWindow(hWnd, CmdShow);<br> UpdateWindow(hWnd);<br> <br> // 消息循环<br> while GetMessage(Msg, 0, 0, 0) do<br> begin<br> TranslateMessage(Msg);<br> DispatchMessage(Msg);<br> end;<br>end.