Cisplatin
Cisplatin

Reputation: 2998

How to move objects with Window's C++ Graphics (GDI+) library?

So far, I've only copy/pasted the example that Microsoft has here (but I removed #include ).

I'm trying to figure out how OnPaint is continually called (as to have moving objects), but it doesn't seem to be called more than once.

How do I use the standard Windows C++ graphics library (i.e. GDI+, or another standard Windows API) to create a moving object? Must I call OnPaint myself? Or is there an easy fix to make it continually be called? Or is it just not possible?

Upvotes: 1

Views: 1876

Answers (1)

Hans Passant
Hans Passant

Reputation: 942508

The OnPaint() method will only run when Windows thinks that your window needs to be repainted. Which, typically, happens just once when your window is first created. Or when you minimize and restore the window.

To force it to run more than once and animate something, you'll have to tell it that a repaint is required. Best way to do so is by using a timer, it will give you an animation clock. Set the interval to a number that's a bit less than a multiple of 15.625 millseconds. 45 msec is a decent value, it gets you 21 updates per second. Assuming that you can paint fast enough. Call InvalidateRect() in the WM_TIMER message handler. Or Invalidate() if you use Winforms.

Upvotes: 1

Related Questions