page-flipping in managed C++

P

Peter Oliphant

The flicker is because you background is painted first to clear the
background and then your graphics shapes.
Flicker could be avoided by using a memory device context. where you draw
the complete image in a memory buffer and then swap it to the dialogbox
window, without painting the background first.

Sounds like page flipping. I like page flipping!

Ok. I currently draw to a Form (actually, my own class derived from a Form,
but therefore it is a Form) via a Graphics object I get from the Form's
Paint event (i.e., using DrawRectange, DrawEllipse, etc.). How do I easily
create and draw to an off-screen 'memory device context'
instead of the Form, and how do I then do the swap of the results to the
Form as you mentioned?

It sounds like a great idea, but I need details! : )

[==P==]
 
P

Peter Oliphant

Let me make this concrete. The following code snippet will draw a red
300x400 rectangle at position (100,200) on 'form' :

__gc class HandlerClass
{
public:
static void m_Paint_Event_Handler( Object*, PaintEventArgs* e )
{
Pen* pen = new Pen( Color::Red, 1 ) ;
e->Graphics->DrawRectangle( pen, 100, 200, 300, 400 ) ;
}
} ;

Form* form = new Form( ) ;
form->Paint += new PaintEventHandler( 0, HandlerClass::paint_Event_Handler )
;

How would you modify this code so that it draws the red triangle on an
off-screen 'memory-device context' and then swaps (or copies) this
off-screen buffer to the on-screen form?

Thanks in advance! : )

[==P==]


Peter Oliphant said:
The flicker is because you background is painted first to clear the
background and then your graphics shapes.
Flicker could be avoided by using a memory device context. where you draw
the complete image in a memory buffer and then swap it to the dialogbox
window, without painting the background first.

Sounds like page flipping. I like page flipping!

Ok. I currently draw to a Form (actually, my own class derived from a
Form,
but therefore it is a Form) via a Graphics object I get from the Form's
Paint event (i.e., using DrawRectange, DrawEllipse, etc.). How do I easily
create and draw to an off-screen 'memory device context'
instead of the Form, and how do I then do the swap of the results to the
Form as you mentioned?

It sounds like a great idea, but I need details! : )

[==P==]
 
T

Tamas Demjen

Peter said:
How would you modify this code so that it draws the red triangle on an
off-screen 'memory-device context' and then swaps (or copies) this
off-screen buffer to the on-screen form?

First you create a bitmap:

Bitmap* bmp = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);

then you get a DC to the bitmap:

Graphics* gr = Graphics::FromImage(bmp);

Now you paint your image onto gr.

And finally paint the bitmap on the screen:
e->Graphics->DrawImageUnscaled(bmp, 0, 0);

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top