Win32相关问题
最近在学Win32,代码是看懂的,只是不明白为什么执行的结果和预期的差距那么大,等下附上代码,请大家看看这段代码,为什么对滚动条的操作都是无效的
#define MAXROWS 100
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow)
{
HWND hwnd;
WNDCLASS winclass;
MSG msg;
winclass.style = CS_VREDRAW | CS_HREDRAW;
winclass.lpfnWndProc = WndProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
winclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL,IDC_ARROW);
winclass.hInstance = hInstance;
winclass.lpszClassName = TEXT("EDTB");
winclass.lpszMenuName = NULL;
if(!RegisterClass(&winclass))
{
MessageBox(hwnd,TEXT("Create Window Failed!"),TEXT("Program Error"),MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(TEXT("EDTB"),TEXT("EDTB"),
WS_OVERLAPPEDWINDOW | WS_VSCROLL,CW_USEDEFAULT,CW_USEDEFAULT,720,480,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
TEXTMETRIC tm;
static int cxChar,cyChar,cxClient,cyClient;
int x,y,iVertPos = 0,iLinePerPage = 0;
switch(message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
GetTextMetrics(hdc,&tm);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hwnd,hdc);
SetScrollRange(hwnd,SB_VERT,0,CHATRECORDS-1,FALSE);
SetScrollPos(hwnd,SB_VERT,iVertPos,TRUE);
return 0;
case WM_SIZE:
cyClient = HIWORD(lParam);
iLinePerPage = cyClient / cyChar;
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
for(int i = 0;i< CHATRECORDS;i++)
{
y = cyChar * (i - iVertPos);
TextOut(hdc,10,y,tbChatRecord[i].cChatLabel,lstrlen(tbChatRecord[i].cChatLabel));
TextOut(hdc,10+22*(cxChar*1.5),y,tbChatRecord[i].cChatRecord,lstrlen(tbChatRecord[i].cChatRecord));
}
EndPaint(hwnd,&ps);
return 0;
case WM_VSCROLL:
switch(LOWORD(wParam))
{
case SB_LINEUP:
iVertPos -= 1;
break;
case SB_LINEDOWN:
iVertPos += 1;
break;
case SB_PAGEUP:
iVertPos -= iLinePerPage;
break;
case SB_PAGEDOWN:
iVertPos += iLinePerPage;
break;
case SB_THUMBPOSITION:
iVertPos = HIWORD(wParam);
break;
default:
break;
}
iVertPos = max(0,min(iVertPos,CHATRECORDS-1));
if(iVertPos != GetScrollPos(hwnd,SB_VERT))
{
SetScrollPos(hwnd,SB_VERT,iVertPos,TRUE);
InvalidateRect(hwnd,NULL,TRUE);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
}
[解决办法]
变量iVertPos和iLinePerPage要设置为静态