Re: a new drawing not erase previous drawing?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



There are several answers here...
On Mon, 6 Jun 2005 22:50:48 +0300, "Andra" <andraatlatnetdotlv> wrote:

>visual studio c++ 6.0, mfc, graphics
>
>
>
>in my program a new drawing erases previous drawing. How to manage that all
>that is drawn stays alive on the window? And is not erased when the window
>is covered by another window, as well. And that the image does not
>flicker...
****
Frequently, when you invalidate a window, the default is "TRUE", meaning to erase the
background. The OnEraseBkgnd handler is called, and the default, if you don't override it,
is to simply draw a blank area using the default background color for the window class
(usually white).

To avoid this, you can override OnEraseBkgnd and do nothing, and not call the superclass,
BUT...and here's the catch...you are now responsible in the OnPaint handler to paint any
part of the window that you do not want to remain in whatever bogus state it was left in.
The problem with OnPaint is that you have no idea why it is called; you could have had a
dialog box, including a dialog box from another app, have popped up on top of it, thus
trashing its contents. Or some other window might have partially covered it. Who knows?
You certainly don't. Therefore, you must redraw ALL of the invalidated area, without
exception, every single pixel, or you will end up with trash on the screen.

Flicker avoidance is a more serious problem. For example, the simplest way to avoid it is
to draw the entire image in an offscreen bitmap and BitBlt it onto your drawing surface.
The effect to the user is that if nothing changes, no visible change is seen. If only part
of the drawing had been obscured, the drawing area is clipped so only that part is redrawn
anyway. There can be performance implications with this technique, but unless you know
performance is a problem, you can safely ignore them (and if it IS a problem, you might
end up spending a lot of effort to reduce the problem! The simplest method, by the way, is
to create a clipping region for your bitmap which is identical to the clipping region of
your window).
****
>
>
>
>Those last two have from time to time appeared in my attempts to solve the
>first problem. And that I didn't manage at all - make the first drawing,
>then the second, see them both at the same time on the window.
****
Be careful about this idea. There is no "first" or "second" drawing. There is only the
window image, and you have to be prepared to draw any or all of it, at any time, without
warning. While logically there is a "first" and "second" drawing, where the "second" is
what appears on top of the "first", you should expect to have to draw both, in that order,
at any time. You cannot assume that any pixel, once placed in the window, will remain
intact. On the contrary, you MUST assume that any pixel can be trashed at an time for any
reason, and you have no control of this. Your responsibility is to see that the pixels can
be re-created on demand.
****
>
>
>
>I have tried the following and maybe more: Invalidate(false),
>InvalidateRect(false), override OnEraseBkgnd, SendMessage(WM_PAINT, 0, 0),
>RedrawWindow(NULL, NULL,RDW_NOERASE), DefWindowProc(WM_PAINT,
>(WPARAM)dc.m_hDC, 0).
****
Invalidate(FALSE) and InvalidateRect(FALSE) would be one solution, but remember, the
system is free at any time to do Invalidate(TRUE) or InvalidateRect(..., TRUE), and you
can't control that. Assume it will happen.

Sending WM_PAINT is not only useless, it is harmful. Under no circumstances, ever, is it
possible to issue such a SendMessage or call DefWindowProc as you show. WM_PAINT messages
are special and can ONLY be generated by the operating system (for example, prior to
sending the message, it has prepared a clipping region for BeginPaint to work on. You
cannot do this yourself; only the system can do this). RedrawWindow will have no effect. I
think you have missed the key Windows paradigm here: any pixel can be erased at any time
for any reason by means you cannot control, and you, and you alone, have complete
responsibility to see that such pixels are correctly redrawn. There is no way to put
pixels on a window and expect they will stay there. They will not. Windows *guarantees*
they will not.
*****
>
>
>
>please help!
>

Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: openGL to emulate QuickDraw (i.e. 2D projection)
    ... Dont mind Funghi, he's a bit well, weird. ... 2d drawing; it's quite a bit different than quickdraw ... just to set up a 'working canvas' ... is the actual window you're drawing to, ...
    (comp.graphics.api.opengl)
  • Re: UI subsystem interface design
    ... The application constructs its UI by composing the primitive drawing objects and then giving those objects as data to the subsystem, which will be responsible for doing the drawing based upon the data it is given. ... Each window is a rectangle and contains coordinates for its position represented as floating-point numbers between 0 and 1. ... There has to be a mapping of the message data packets in the subsystem interface and you will need some way to ensure that mapping, but I don't see that being dedicated identity objects. ...
    (comp.object)
  • Re: UI subsystem interface design
    ... I will call the user-interface subsystem which I am designing 'the ... UI by composing the primitive drawing objects and then giving those ... The most primitive class of drawing objects is the Window. ... Each window contains a mapping for Colors ...
    (comp.object)
  • Re: PolyLine and autoscrolling canvas
    ... There is something deeply suspicious here about getting a window DC. ... you are drawing the entire window, including borders, captions, etc. ... CRect rect; ... The screen resolution could change at any time, ...
    (microsoft.public.vc.mfc)
  • Code Addendum 01 - gforth: SDL/OpenGL Graphics Part V
    ... \ Load the SDL C Library Interface ... \ set to 1) when the mouse cursor is inside the display window. ... \ Pixel plot function. ... \ Plots the pixel value to the *dst surface at the given coordinates. ...
    (comp.lang.forth)