我的scrollbar程序在运行时
总是不停的刷新屏幕,我不知道是那个动作发送了WM_PAINT,即使我鼠标无任何动作。
代码如下:
//////////////////////
#include <windows.h>LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow) {
static TCHAR szAppName[]=TEXT("ScrollBar");
MSG msg;
HWND hwnd;
WNDCLASS wndclass;
wndclass.style=CS_VREDRAW|CS_HREDRAW;
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=szAppName;
RegisterClass(&wndclass);
hwnd=CreateWindow(szAppName,szAppName,WS_OVERLAPPEDWINDOW|WS_VSCROLL,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 current=0;
static int height=0;
char buffer[40];
int size=0;
HDC hdc;
TEXTMETRIC tm;
switch(message) {
case WM_CREATE:
SetScrollRange(hwnd,SB_VERT,0,100,FALSE);
hdc=GetDC(hwnd);
GetTextMetrics(hdc,&tm);
height=tm.tmHeight;
ReleaseDC(hwnd,hdc);
return 0;
case WM_SIZE:
current=0;
return 0;
case WM_LBUTTONDOWN:
current=0;
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_VSCROLL:
if(LOWORD(wParam)==SB_THUMBTRACK) {
hdc=GetDC(hwnd);
TextOut(hdc,0,current*height,TEXT("Thumb is tracking now."),22);
++current;
ReleaseDC(hwnd,hdc);
}
if(LOWORD(wParam)==SB_THUMBPOSITION) {
hdc=GetDC(hwnd);
size=wsprintf(buffer,TEXT("The position is:  %d now."),HIWORD(wParam));
TextOut(hdc,0,current*height,buffer,size);
++current;
SetScrollPos(hwnd,SB_VERT,HIWORD(wParam),TRUE);
ReleaseDC(hwnd,hdc);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}