滚动条例题

wuchengwei 2006-03-16 12:50:47
#include <windows.h>
#include <stdio.h>
#include <string.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow){
static TCHAR szAppName[] = TEXT("SimpleDisplay");
static TCHAR szClassName[] = TEXT("SimpleDisplayClass");
static TCHAR szWinName[] = TEXT("Source Text Simple Display Program");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;

//typedef the class of the window
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 = szClassName;

//regist window
if(!RegisterClass(&wndclass)){
MessageBox(NULL,TEXT("This program requires Windows NT!"),
szAppName,MB_ICONERROR);
return 0;
}
//RegisterClass(&wndclass);
//create window
hwnd = CreateWindow(
szClassName,
szWinName,
WS_OVERLAPPEDWINDOW | WS_HSCROLL |WS_VSCROLL,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL );

//show window
ShowWindow(hwnd, nCmdShow);

//update window
UpdateWindow(hwnd);

//message circle
while(GetMessage(&msg, NULL, 0, 0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}

...全文
58 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
teli_eurydice 2006-03-18
  • 打赏
  • 举报
回复
的确很长,刷新太多了吧
yoogle 2006-03-18
  • 打赏
  • 举报
回复
太长了,没仔细看。
我猜是进入到一个死循环里了。你可以设置跟进去看看。
wuchengwei 2006-03-16
  • 打赏
  • 举报
回复
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
static int cxChar, cyChar; //record the width and height of char
static int cxClient, cyClient; //record the width and height of client area
static int nHScrollPos,nVScrollPos; //record the position of horizontal and vertical scroolbar
static int cMaxCharNumber; //record the most count of the line's char
static int cLineNumber; //record the lines' count of whole
int nCurrentLineNumber; //record current line
int nCurrentCharNumber; //record the count of current line' char
int nFirstLine,nLastLine; //record the first and last line
TCHAR szBuff[256]; //store the input char
HDC hdc; //handle of displayer
PAINTSTRUCT ps; //struct of paint
TEXTMETRIC tm; //struct of text
SCROLLINFO si; //struct of scrollbar
FILE *fp; //file's point


//dispose the message queue
switch(message){
case WM_CREATE:
//get the device' handle
hdc = GetDC(hwnd);

//get the information of char
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight + tm.tmExternalLeading;

//free the handle of device
ReleaseDC(hwnd, hdc);

//get cLineNumber and cMaxCharNumber
nCurrentCharNumber = 0;
cMaxCharNumber = 0;
cLineNumber = 0;
if(fp = fopen("my2.cpp", "r")){
while(!feof(fp)){
char ch;
while(ch = fgetc(fp) != '\n' && ch != EOF)
nCurrentCharNumber++;
if(nCurrentCharNumber > cMaxCharNumber)
cMaxCharNumber = nCurrentCharNumber;
cLineNumber++;
nCurrentCharNumber = 0;
}
}
fclose(fp);
return 0;

case WM_SIZE:
//get the information of client area
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);

//set scrollbar
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_PAGE | SIF_RANGE;
si.nMax = cLineNumber -1;
si.nMin = 0;
si.nPage = cyClient/cyChar;
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);

si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_PAGE | SIF_RANGE;
si.nMax = cMaxCharNumber -1;
si.nMin = 0;
si.nPage = cxClient/cxChar;
SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
return 0;

case WM_HSCROLL:
//get the information of horizontal scrollbar and record the current position of the scrollbar
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_ALL;
GetScrollInfo(hwnd, SB_HORZ, &si);
nHScrollPos = 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;
case SB_THUMBPOSITION:
si.nPos -= si.nTrackPos;
break;
case SB_LEFT:
si.nPos -= si.nMin;
break;
case SB_RIGHT:
si.nPos -= si.nMax;
break;
default:
break;
}

//set the new information of horizontal scrollbar
si.fMask = SIF_POS;
SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
//read the information of horizontal scrollbar
GetScrollInfo(hwnd, SB_HORZ, &si);

//if the scrollbar is moved , scroll the window accordingly
if(si.nPos != nHScrollPos)
ScrollWindow(hwnd, cxChar*(nHScrollPos-si.nPos), 0, NULL, NULL);
return 0;

case WM_VSCROLL:
//get the information of horizontal scrollbar and record the current position of the scrollbar
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_ALL;
GetScrollInfo(hwnd, SB_VERT, &si);
nVScrollPos = si.nPos;

switch(LOWORD(wParam)){
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;
case SB_TOP:
si.nPos -= si.nMin;
break;
case SB_BOTTOM:
si.nPos -= si.nMax;
break;
default:
break;
}
//set the new information of horizontal scrollbar
si.fMask = SIF_POS;
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
//read the information of horizontal scrollbar
GetScrollInfo(hwnd, SB_VERT, &si);

//if the scrollbar is moved , scroll the window accordingly
if(si.nPos != nVScrollPos)
ScrollWindow(hwnd, 0, cyChar*(nVScrollPos-si.nPos), NULL, NULL);
return 0;

case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
SetTextColor(hdc, RGB(255, 0, 0));
SetBkColor(hdc, RGB(0, 255, 0));
SetBkMode(hdc, OPAQUE);

TextOut(hdc, 0, 0, "Loading......", strlen("Loading......"));

//read the position of scrollbar
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_POS;
GetScrollInfo(hwnd, SB_VERT, &si); //horizontal scrollbar
nVScrollPos = si.nPos;

si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_POS;
GetScrollInfo(hwnd, SB_HORZ, &si); //vertical scrollbar
nVScrollPos = si.nPos;

//set the extern of text will be displayed
nFirstLine = max(0, nVScrollPos + ps.rcPaint.top/cyChar);
nLastLine = min(cLineNumber -1,nVScrollPos + ps.rcPaint.bottom/cyChar);

//output text
//open the source file "SimpleDisplay.c" only readable
if(fp = fopen("my2.cpp", "r")){

//if still arrive the end of file ,continue reading
for(nCurrentLineNumber = 0; nCurrentLineNumber <= nLastLine; nCurrentLineNumber++){
int i = 0;
char ch;

if(nCurrentLineNumber < nFirstLine)
while((ch = fgetc(fp) ) != '\n' && ch != EOF);
else
while((ch = fgetc(fp) ) != '\n' && ch != EOF)
if(ch != '\t')
szBuff[i++] = ch;
//TextOut(hdc, cxChar, cyChar*cLineNumber, szBuff, i);
TextOut(hdc, 0, cyChar*(nCurrentLineNumber-nVScrollPos), szBuff, i);
//TextOut(hdc, cxChar*(-nHScrollPos), cyChar*(nCurrentLineNumber-nVScrollPos), szBuff, i);
}
fclose(fp);
}

EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}

编译通过,运行时CPU100%,且不显示结果,是怎么回事啊???
程序长了点,不过但是简单的基础语句,各位大峡们就帮我看下吧
在线等啊

1,649

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 非技术类
社区管理员
  • 非技术类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧