c++ GDI 动画不工作
我正在尝试使用 gdi 创建球动画,但我无法让它工作。 我用这支钢笔创建了一个球
Graphics graphics(hdc);
(Color(255, 0, 0, 255)); Graphics.DrawEllipse(&pen, sf , 0, 10, 10); 我有 while 循环,它循环并向 sf 值加 1 ,基本上像这样 sf++ ; 比我尝试重新绘制窗口(它不起作用)所以最终会得到多个圆圈;/ 这是循环(循环是 int WM_PAINT)
while(sd==1)//sd equals 1
{
sf++;
onPaint(hdc);
InvalidateRect (hWnd, NULL, TRUE);// this should repaint the window but it doesn't
UpdateWindow(hWnd);
}
提前致谢 拉米
im trying to create ball animation using gdi but i can't get it working.
i created a ball using this
Graphics graphics(hdc);
Pen pen(Color(255, 0, 0, 255));
graphics.DrawEllipse(&pen, sf , 0, 10, 10);
i have while loop that loops and adds 1 to sf value basicly like this sf++;
than i try to repaint the window(it doesn't work) so ill end up with more than one circle ;/
here is the loop( the loop is int WM_PAINT)
while(sd==1)//sd equals 1
{
sf++;
onPaint(hdc);
InvalidateRect (hWnd, NULL, TRUE);// this should repaint the window but it doesn't
UpdateWindow(hWnd);
}
thanks in advance
Rami
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了实现动画,我建议你使用计时器。例如:
现在窗口每秒(1000ms)就会收到WM_TIMER消息。你应该处理它们:
然后你需要处理 WM_PAINT 消息来进行绘图
In order to achieve animation I would suggest you use a timer. For example:
now window will receive WM_TIMER messages every second (1000ms). You should handle them:
then you need to handle WM_PAINT message to do the drawing
您确实意识到您正在循环中以 (sd == 1) 为条件递增 sf,对吧?这当然会无限循环或永远不会被输入,因为 sd 的值不会以任何方式改变。您在这里使用过调试器吗?无论如何,为什么你需要这样一个循环?您不应该在循环中调用 OnPaint。
如果您想要多个圆,只需在从函数返回之前将它们全部绘制出来即可。维护将用于在 OnPaint 处理程序中绘制圆圈的数据集合。
You do realize that you are incrementing sf in a loop with a conditional of (sd == 1), right? That will of course just loop infinitely or never be entered because the value of sd is not being changed in any way. Have you used the debugger at all here? Why would you need such a loop anyway? You should not be calling OnPaint in a loop.
If you want more than one circle, just draw them all before returning from the function. Maintain a collection of data that will be used to draw the circles in the OnPaint handler.
InvalidateRect 发送 WM_ERASEBKGND 消息,如果您在创建窗口时没有定义 WNDCLASS 结构的 hbrBackground(用于重绘背景的画笔)成员,则除非您自己处理 WM_ERASEBKGND 消息,否则它不会重绘背景。
如果这不是问题,那么可能是因为您直接调用 UpdateWindow 而不是轮询和处理消息,所以 WM_ERASEBKGND 消息永远不会被处理。在绘制新圆之前,尝试用背景颜色覆盖前一个圆。
或者使用 WM_ERASEBKGRND 作为消息调用 SendMessage。
InvalidateRect sends a WM_ERASEBKGND message, and if you don't have a hbrBackground (brush to repaint the background) member of the WNDCLASS structure defined when you create the window it won't redraw the background unless you handle the WM_ERASEBKGND message yourself.
If that isn't the problem, then maybe because you are calling UpdateWindow directly instead of polling and handling messages, the WM_ERASEBKGND message never gets handled. Try overwriting the previous circle with the background colour before drawing the new one.
Or call SendMessage with WM_ERASEBKGRND as the message.
我在msdn上找到了一个示例,它展示了如何绘制内容 在纯 win32 中。
您不应在 WM_PAINT 中调用 Invalidate 或 Updatewindow,因为 UpdateWindow 会发送新的 WM_PAINT 事件,并且无效会累积到下一个 wm_paint 事件。
您应该将代码分为两个功能,一个用于执行移动,另一个用于在当前位置绘制圆圈。
您的移动器函数可以从任何地方调用(也许在计时器处理函数中?)并且应该以
In order to mark your client Area for redrawal and notification your window to redraw myself 结尾。
您的 Draw() 函数应该读取使用移动器函数设置的位置,然后围绕该位置绘制一个圆圈。
(旁注:如果您想最大程度地减少闪烁并获得流畅的动画,请在启动并运行基本动画后查看双缓冲)
更新
您错过了 UpdateWindow更新功能中的命令
仅当应用程序收到 WM_PAINT 消息时才会调用 OnPaint 函数,因此您需要发送这些消息。
UpdateWindow 就是用于此目的
I found an example on msdn which shows how to draw stuff in pure win32.
You should not call Invalidate or Updatewindow in WM_PAINT, as UpdateWindow sends a new WM_PAINT-event, and invalidates get accumulated until the next wm_paint event.
You should divide your Code into two functions, one to perform the movement and the other to draw your circle at the current location.
Your Mover-function can be called from anywhere (perhaps in a timer handler function?) and should end with
In order to mark your client area for redrawal and notify your window to redraw itself.
Your Draw()-function should read the position set with your mover function, and just a draw a circle around this location.
(Sidenote: If you want to minimize flicker and get smooth animation, have a look at double buffering once you get your basic animation up and running)
UPDATE
You were missing the UpdateWindow command in your Update-function
Your OnPaint-Function is only called when a WM_PAINT-message is received by your application, so you need to send those.
UpdateWindow serves this purpose