//帮忙看看程序错在那里,编译没问题能通过。谢谢
#include<windows.h>#include "textone.h"
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow)
{static TCHAR szAppName[]=TEXT("Demo3");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WndProc;
wndclass.lpszClassName = szAppName;
wndclass.lpszMenuName = NULL;
wndclass.style = CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,"注册失败",szAppName,MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
szAppName,
WS_OVERLAPPEDWINDOW|WS_VSCROLL|WS_HSCROLL,
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 int cxChar,cyChar,cxCaps,cxClient,cyClient,iMaxWidth;
HDC hdc;
int i,x,y,iVertPos,iHorzPos,iPaintBeg,iPaintEnd;
PAINTSTRUCT ps;
SCROLLINFO si;
TEXTMETRIC tm;
switch(message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
GetTextMetrics(hdc,&tm);
cxChar = tm.tmAveCharWidth;
cxCaps = (tm.tmPitchAndFamily&1?3:2)*cxChar/2;
ReleaseDC(hwnd,hdc);
iMaxWidth = 50 * cxChar;
return 0;
case WM_SIZE:
//获取客户区x,y单位
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
//设置垂直滚动条
si.cbSize = sizeof(si);//获取结构大小
si.fMask =SIF_RANGE|SIF_PAGE;//获取页码范围和大小
si.nMin = 0;
si.nMax = NUMLINES - 1;
si.nPage = cyClient / cyChar;
SetScrollInfo(hwnd,SB_VERT,&si,TRUE);
//设置水平滚动条
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_RANGE|SIF_PAGE;
si.nMin = 0;
si.nMax = 2 + iMaxWidth / cxChar;
si.nPage = cxClient /cxChar;
SetScrollInfo(hwnd,SB_HORZ,&si,TRUE);
return 0;
case WM_VSCROLL:
//获取垂直滚动条信息
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_ALL;
GetScrollInfo(hwnd,SB_VERT,&si);
iVertPos = si.nPos; //获取当前位置存储在变量iVertPos中,以对比移动滚动条后的变化
switch(LOWORD(wParam))//获取滚动条消息
{
case SB_TOP:
si.nPos = si.nMin;
break;
case SB_BOTTOM:
si.nPos = si.nMax;
break;
case SB_LINEUP:
si.nPos -= 1;
break;
case SB_LINEDOWN:
si.nPos += 1;
break;
case SB_PAGEUP:
si.nPos -=si.nPage;
break;
case SB_PAGEDOWN:
si.nPos += si.nPage;
break;
case SB_THUMBTRACK:
si.nPos = si.nTrackPos;
break;
default :
break;
}
si.fMask =SIF_POS;
SetScrollInfo(hwnd,SB_VERT,&si,TRUE);
if(si.nPos != iVertPos)//判断位置是否移动,如果移动就使用scrollWindow函数滚动客户区的内容
{//参数1水平滚动像素,参数2垂直滚动像素,参数3.4.滚动区域范围,NULL表示滚动整个客户区
ScrollWindow(hwnd,0,cyChar*(iVertPos-si.nPos),NULL,NULL);
UpdateWindow(hwnd);
}
return 0;
case WM_HSCROLL:
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_ALL;
GetScrollInfo(hwnd,SB_HORZ,&si);
iHorzPos = si.nPos;
switch(LOWORD(wParam))
{
case SB_LINELEFT:
si.nPos -= 1;
break;
case SB_LINERIGHT:
si.nPos +=1;
break;
case SB_PAGELEFT:
si.nPos +=si.nPage;
break;
case SB_PAGERIGHT:
si.nPos -=si.nPage;
break;
default:
break;
}
si.fMask = SIF_POS;
SetScrollInfo(hwnd,SB_HORZ,&si,TRUE);
GetScrollInfo(hwnd,SB_HORZ,&si);
if(si.nPos != iHorzPos)
{
ScrollWindow(hwnd,cxChar*(iHorzPos - si.nPos),0,NULL,NULL);
}
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
//获取垂直滚动条位置存储在变量iVertPot中
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_POS;
GetScrollInfo(hwnd,SB_VERT,&si);
iVertPos = si.nPos;
//水平
GetScrollInfo(hwnd,SB_HORZ,&si);
iHorzPos = si.nPos;
//计算文本数组第一行和最后一行输出位置,也就是数组下标
iPaintBeg =max(0,iVertPos + ps.rcPaint.top/cyChar);
iPaintEnd = min(NUMLINES - 1,iVertPos + ps.rcPaint.bottom/cyChar);
//用循环语句输出
for(i=iPaintBeg;i<=iPaintEnd;i++)
{//计算文本x y坐标具体位置
x=cxChar*(1 - iHorzPos);
y=cyChar*(1 - iVertPos);
TextOut(hdc,x,y,NameInfo[i].Name,lstrlen(NameInfo[i].Name));
TextOut(hdc,x+15*cxChar,y,NameInfo[i].Info,lstrlen(NameInfo[i].Info));
}
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}