由于我怀疑这与您之前的问题有关,因此我发布了以下答案。
如果您想在窗口中创建一些动画并想加快速度,请考虑使用 GDI+ 而不是纯 winapi。使用 GDI+,您只需将绘图方法放入WM_PAINT
消息中,并通过使控件 rect 无效和更新窗口来更新场景。
例如这样的:
InvalidateRect(hwnd, NULL, TRUE);
UpdateWindow(hwnd);
WM_PAINT 消息处理可能如下所示:
case WM_PAINT:
{
/************************************************************************/
/* Initialize */
/************************************************************************/
hdc = BeginPaint(hwnd, &ps);
Graphics graphics(hdc);
Bitmap *pMemBitmap = new Bitmap(rect.right, rect.bottom);
Graphics* pMemGraphics = Graphics::FromImage(pMemBitmap);
SolidBrush back(Color(0, 0, 0));
pMemGraphics->FillRectangle(&back, 0, 0, rect.right, rect.bottom); // Fill your background with some color
pMemGraphics->SetSmoothingMode(SmoothingModeHighQuality); // Set your raster quality
graphics.SetSmoothingMode(SmoothingModeHighQuality);
// Rotate some primitive around your screen
static double x = M_PI + M_PI / 2;
x += 0.03f;
if(x > M_PI * 2)
{
x = 0;
}
double posX = cos(x);
double posY = sin(x);
Pen pen(Color(255, 255, 255, 0), 5);
pMemGraphics->DrawEllipse(&pen, (int)(posX * 50 - 15 + rect.right / 2), (int)(posY * 50 - 15 + rect.bottom / 2), 30, 30);
graphics.DrawImage(pMemBitmap, 0, 0); // Draw the entire bitmap to screen (double buffering)
/************************************************************************/
/* Cleanup */
/************************************************************************/
delete pMemGraphics;
delete pMemBitmap;
EndPaint(hwnd, &ps);
break;
}
为了避免闪烁,你应该告诉 winapi 不要像这样擦除背景:
case WM_ERASEBKGND:
{
return true; // Tell winapi, we already handled that.
}