下面的程序我找了半天也找不出错误在哪儿,哪位高手能找出吗?
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
TCHAR szClassName[]=TEXT("myClass");
//准备添装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.lpszClassName=szClassName;
wndclass.lpszMenuName=NULL;
//注册wndclass类
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("Unicode can not be supported by OS"),TEXT("ERROR"),MB_OK | MB_ICONERROR);
return 0;
}
//创建窗口
hwnd=CreateWindow(szClassName,TEXT("Window With Scrollbar"),WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
//更新窗口
UpdateWindow(hwnd);
//显示窗口
ShowWindow(hwnd,SW_NORMAL);
//消息循环
while(GetMessage(&msg,hwnd,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//程序结束
ExitProcess(0);
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
SCROLLINFO si;
TEXTMETRIC tm;
FILE *fp;
static int cxChar,cyChar; //字符的高度和宽度
static int cMaxCharNumber; //文件中单行文本的最多字符数
static int cLineNumber; //文件中文本显示的总行数
static int cxClient,cyClient;//窗口用户区的长度和高度
static int nHScrollPos;
static int nVScrollPos; //滚动块的位置
int nFirstLine;
int nLastLine;
int nCurrentLine; //在响应wm_scroll消息显示文本时使用
int cCurrentCharNumber; //在获取单行字符数时使用
char szBuffer[256]; //用于存储每一行的字符
char ch;
switch(message)
{
case WM_CREATE:
hdc=GetDC(hwnd);
//获取字符信息
GetTextMetrics(hdc,&tm);
cxChar=tm.tmAveCharWidth;
cyChar=tm.tmHeight+tm.tmExternalLeading;
ReleaseDC(hwnd,hdc);
//获取文件的总行数和单行最多字符数
if(!(fp=fopen("E:\\1.txt","r")))
{
MessageBox(hwnd,TEXT("can not open the file"),TEXT("ERROR"),MB_OK | MB_ICONERROR);
return 0;
}
while(!feof(fp))
{
cCurrentCharNumber=0;
while((ch=fgetc(fp))!='\n'&&ch!=EOF)
cCurrentCharNumber++;
if(cMaxCharNumber<cCurrentCharNumber)
cMaxCharNumber=cCurrentCharNumber;
cLineNumber++;
}
fclose(fp);
return 0;
case WM_SIZE:
//获取窗口用户区的长度和高度
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
//准备对垂直滚动块进行设置
si.cbSize=sizeof(SCROLLINFO);
si.fMask=SIF_RANGE | SIF_PAGE;
si.nMin=0;
si.nMax=cLineNumber-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=cMaxCharNumber-1;
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);
nVScrollPos=si.nPos;
//处理滚动块的位置
switch(LOWORD(wParam))
{
case SB_LINEUP:
si.nPos--;
break;
case SB_LINEDOWN:
si.nPos++;
break;
case SB_PAGEUP:
si.nPos-=si.nPage;
break;
case SB_PAGEDOWN:
si.nPos+=si.nPage;
break;
case SB_THUMBPOSITION:
si.nPos=si.nTrackPos;
break;
case SB_TOP:
si.nPos=0;
break;
case SB_BOTTOM:
si.nPos=si.nMax;
break;
default:
break;
}
//对滚动块进行设置
si.fMask=SIF_POS;
SetScrollInfo(hwnd,SB_VERT,&si,TRUE);
GetScrollInfo(hwnd,SB_VERT,&si);
if(si.nPos!=nVScrollPos)
ScrollWindow(hwnd,0,(nVScrollPos-si.nPos)*cyChar,NULL,NULL);
return 0;
case WM_HSCROLL: //对滚动块的位置进行设置
//主要获取滚动块的位置信息
si.cbSize=sizeof(SCROLLINFO);
si.fMask=SIF_ALL;
GetScrollInfo(hwnd,SB_HORZ,&si);
nHScrollPos=si.nPos;
//处理滚动块的位置
switch(LOWORD(wParam))
{
case SB_LINELEFT:
si.nPos--;
break;
case SB_LINERIGHT:
si.nPos++;
break;
case SB_PAGELEFT:
si.nPos-=si.nPage;
break;
case SB_PAGERIGHT:
si.nPos+=si.nPage;
break;
case SB_THUMBPOSITION:
si.nPos=si.nTrackPos;
break;
case SB_LEFT:
si.nPos=0;
break;
case SB_RIGHT:
si.nPos=si.nMax;
break;
default:
break;
}
//对滚动块进行设置
si.fMask=SIF_POS;
SetScrollInfo(hwnd,SB_HORZ,&si,TRUE);
GetScrollInfo(hwnd,SB_HORZ,&si);
if(si.nPos!=nVScrollPos)
ScrollWindow(hwnd,(nHScrollPos-si.nPos)*cyChar,0,NULL,NULL);
return 0;
case WM_PAINT:
//获取重绘区设区设备
hdc=BeginPaint(hwnd,&ps);
//读取滚动块的位置信息,以确定要显示的文本内容的范围
si.cbSize=sizeof(SCROLLINFO);
si.fMask=SIF_POS;
GetScrollInfo(hwnd,SB_VERT,&si);
nVScrollPos=si.nPos;
GetScrollInfo(hwnd,SB_HORZ,&si);
nHScrollPos=si.nPos;
//确定要显示文本内容的范围
nFirstLine=max(0,nVScrollPos+ps.rcPaint.top/cyChar);
nLastLine=min(cLineNumber-1,nVScrollPos+ps.rcPaint.bottom/cyChar);
//显示文本
if(fp=fopen("E:\1.TXT","r+"))
{
for(nCurrentLine=0;nCurrentLine<=nLastLine;nCurrentLine++)
{
int i=0;
while((ch=fgetc(fp))!='\n' && ch!=EOF)
{
if(nCurrentLine>=nFirstLine)
{
szBuffer[i]=ch;
}
i++;
}
TextOut(hdc,-nHScrollPos*cxChar,(nCurrentLine-nVScrollPos)*cyChar,szBuffer,i);
nCurrentLine++;
}
}
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
DestroyWindow(hwnd);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}