Click here to Skip to main content
15,878,748 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
How can I use graphics class to draw images over a bitmap? Which then can be restored to the screen so whatever i draw does not get erased when I call invalidate().please provide me the solution.
Updated 2-Dec-10 4:02am
Emilio Garavaglia 2-Dec-10 8:23am    
By saying "as soon as possible", you'll make no-one responding.
anonymous1310 2-Dec-10 10:02am    
Is it OK now.i hope i will get some response to my question now

"Provide me the solution" seems implying there must be one and only. But the question is strange, since it mixed up two distinct aspects.

Drawing on a bitmap is something that happens in memory, and has no relation with the action of invalidate(), whose purpose is to force the window manager to ask your program to redraw the window contents.

If you don't want the window contents to "vanish", you have to override OnPaint, repainting the window.
You can do that by copying an image in it (Create a CPaintDC, initialise a gdp::Graphics with it, ad use it to draw your already existent and drawn bitmap into it).

To draw your bitmap, you have first to create a gdp::Bitmap somewhere, initiaklize a gdp::Graphics on it, ad use it to draw.

But ... are these two distinct step necessary? Can't you just draw the into the window when required?
Share this answer
The window does not have a persistent canvas/bitmap associated with its surface. The system can overwrite a protion of your window on the screen any time, and it can call you any time with a WM_PAINT to ask you to redraw the 'destroyed' part of the window image on the screen.
The drawing consists of 2 steps:

  1. The system sends WM_ERASEBKGND to your window/control.
  2. WM_PAINT, where you have to draw your control

If you want to keep the image of your window/control then you have to draw the actual appearance of the control to an in-memory bitmap, and in the WM_PAINT message you just draw the bitmap to the control. This is called double buffering. If the control is not too complex then it does not worth keeping an in memory bitmap for this purpose. Doublebuffering is usually used only if the drawing of the control is complex and performance critical, or if you can avoid a lot of annoying flickering. Another two flicker avoidance methods:

  1. Returning 1 from WM_ERASEBKGND without clearing the beckground before painting.
  2. Moving your drawing to WM_ERASEBKGND entirely from your WM_PAINT, and doing nothing in WM_PAINT but validating the clipping region (for example by just calling BeginPaint() and EndPaint() without actual drawing).
Share this answer
anonymous1310 5-Dec-10 10:56am    
Thanks,but I have to draw some line,or rectangle,or ellipse according to the selection of the can i use double buffering on that case I don't have any experience on that topic ,can you provide me any example of that or any other way of retaining shapes using GDI+.
pasztorpisti 5-Dec-10 13:06pm    
Then you have to derive your own button class from the class you are currently using, and somehow override its OnPaint() method (or intercept the OnPaint() handler in the message map, I don't know what to tell because you haven't shown any reference code to bugfix.). And you should use your own button class instead of the current one. In the OnPaint() event handler you have to draw the entire button according to the current state (background, border, label, and maybe a focus rectangle).
When you call invalidate(), a WM_PAINT message is sent.
In your OnPaint[^]function, you can then do whatever is needed to redraw the picture (using the graphics class if you wish).
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900