我不太理解计时器的使用方法,下面是我的部分代码,我是想让一方块能垂直向下,但是程序只能停留在原地,不动。请各位帮帮我看看,谢谢
void RotatePoint(POINT pt)
{
pt[0].y=100;pt[0].x=-50;
pt[1].y=50;pt[0].x=50;
}
RESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
static int cxClient, cyClient ;
PAINTSTRUCT ps;
static POINT pt[2];
pt[0].x=-50;pt[0].y=900;pt[1].x=50;pt[1].y=800;
switch (message)
{
case WM_CREATE :
SetTimer(hwnd,ID_TIMER,3000,NULL); return 0 ;
case WM_SIZE :
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;
case WM_TIMER:
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
SetMapMode (hdc, MM_ISOTROPIC) ;
SetWindowExtEx (hdc, 1000, 1000, NULL) ;
SetViewportExtEx (hdc, cxClient / 2, -cyClient / 2, NULL) ;
SetViewportOrgEx (hdc, cxClient / 2, cyClient / 2, NULL) ;
//if(pt[0].y!=-500)
RotatePoint(pt);
Rectangle(hdc,pt[0].x,pt[0].y,pt[1].x,pt[1].y);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY :
KillTimer(hwnd,ID_TIMER);
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
void RotatePoint(POINT pt)
{
pt[0].y=100;pt[0].x=-50;
pt[1].y=50;pt[0].x=50;
}
RESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
static int cxClient, cyClient ;
PAINTSTRUCT ps;
static POINT pt[2];
pt[0].x=-50;pt[0].y=900;pt[1].x=50;pt[1].y=800;
switch (message)
{
case WM_CREATE :
SetTimer(hwnd,ID_TIMER,3000,NULL); return 0 ;
case WM_SIZE :
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;
case WM_TIMER:
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
SetMapMode (hdc, MM_ISOTROPIC) ;
SetWindowExtEx (hdc, 1000, 1000, NULL) ;
SetViewportExtEx (hdc, cxClient / 2, -cyClient / 2, NULL) ;
SetViewportOrgEx (hdc, cxClient / 2, cyClient / 2, NULL) ;
//if(pt[0].y!=-500)
RotatePoint(pt);
Rectangle(hdc,pt[0].x,pt[0].y,pt[1].x,pt[1].y);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY :
KillTimer(hwnd,ID_TIMER);
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
InvalidateRect(hwnd,NULL,TRUE);
//这里应该加一个修改矩形位置坐标的函数
return 0;
pt[0].x=-50;pt[0].y=900;pt[1].x=50;pt[1].y=800;
========================
你的坐标是固定死的,而且在WM_TIMER中仅仅是InvalidateRect,而这个功能是发重绘消息。应该在InvalidateRect之前改变pt的值。
{
HDC hdc ;
static int cxClient, cyClient ;
PAINTSTRUCT ps;
static POINT pt[2];
pt[0].x=-50;pt[0].y=900;pt[1].x=50;pt[1].y=800;
switch (message)
{
case WM_CREATE :
SetTimer(hwnd,ID_TIMER,1000,NULL); return 0 ;
case WM_SIZE :
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;
case WM_TIMER:
pt[0].y=pt[0].y-50;
pt[1].y=pt[1].y-50;
pt[0].x=-50;pt[1].x=50;
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
SetMapMode (hdc, MM_ISOTROPIC) ;
SetWindowExtEx (hdc, 1000, 1000, NULL) ;
SetViewportExtEx (hdc, cxClient / 2, -cyClient / 2, NULL) ;
SetViewportOrgEx (hdc, cxClient / 2, cyClient / 2, NULL) ;
Rectangle(hdc,pt[0].x,pt[0].y,pt[1].x,pt[1].y);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY :
KillTimer(hwnd,ID_TIMER);
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
我改了还是不能动
pt[0].x=-50;pt[0].y=900;pt[1].x=50;pt[1].y=800;
========================
这一句的问题,你忘记了一点,WndProc是会被调用很多次的,每当有一个消息来时,就会调用这个函数,当你在定时器将pt[0],pt[1]的值改好了之后,你调用InvalidateRect(hwnd,NULL,TRUE); 它会引发WM_PAINT,而在处理这个消息时,系统还会调用WndProc这个函数,此时,你上面的初始化pt[0],pt[1]的部分又被执行了一次,所以此时pt[0].x=-50;pt[0].y=900;pt[1].x=50;pt[1].y=800; 又还原了。应该将这个pt定义成全局函数,初始化放在主函数中或者是WM_CREATE中。