我在 msdn 上找到了一个示例,它显示了如何在纯 win32 中绘制内容。
您不应该在 WM_PAINT 中调用 Invalidate 或 Updatewindow,因为 UpdateWindow 会发送一个新的 WM_PAINT 事件,并且在下一个wm_paint 事件之前会累积无效。
您应该将您的代码分为两个功能,一个用于执行移动,另一个用于在当前位置绘制您的圆圈。
您的 Mover 函数可以从任何地方调用(也许在计时器处理函数中?)并且应该以
InvalidateRect (hWnd, NULL, TRUE);
UpdateWindow(hWnd);
为了标记您的客户区进行重绘并通知您的窗口重绘自身。
你的 Draw() 函数应该读取你的移动函数设置的位置,然后在这个位置周围画一个圆圈。
(旁注:如果您想最小化闪烁并获得流畅的动画,请在启动并运行基本动画后查看双缓冲)
更新
您在更新函数中缺少 UpdateWindow 命令您的 OnPaint-Function 仅在您的应用程序收到 WM_PAINT 消息时调用,因此您需要发送这些消息。
UpdateWindow用于此目的
VOID update(HDC hdc,HWND hWnd)
{
sf++;
FillRect(hdc,rect,(HBRUSH)(COLOR_WINDOW+1));
InvalidateRect (hWnd, NULL, TRUE);
UpdateWindow(hWND);//<- This Line sends a wm_paint-message to your window in order to make it redraw itself
}
//i didn't do any changes to the onPaint functon but here is the code for it
VOID onPaint(HDC hdc)
{
Graphics graphics(hdc);
Pen pen(Color(255, 0, 0, 255));
graphics.DrawEllipse(&pen, sf , 0, 10, 10);
}
//here is the while loop
while(sd==1)
{ onPaint(hdc);
update(hdc,hWnd);
}