Hello everyone.

I've recently written a program which draws some stuff to a DC; however, on a screen invalidation, the things drawn on the DC, dissapear (get erased.) Does anyone know how one would be able to draw to this dc, and have whatever was drawn to the dc remain there AFTER the screen has been invalidated?

If I move my program off screen (which invalidates the screen) everything I drew to the screen, gets erased!

For example, lets say I created a DC as follows:

WM_CREATE:
HDC screenDC;
screenDC = GetDC(hWnd);
break;

WM_MOUSEMOVE:
//Draw to the created DC
SetPixel(hDC, LOWORD(lParam), HIWORD(lParam), (0, 0, 0, 0));
break;

Once I have drawn to my created DC, via the SetPixel function in the above example, if the screen becomes invalidated, anything drawn on the DC gets erased.

How can I draw to a DC, and have its contents remain on it, once the screne is invalidated? Thank you.

Recommended Answers

All 5 Replies

Its been a little while since I've messed with Win32 but I believe it is a flag you set when you setup your wndclass structure. One of the members to the wndclass structure is a window style and you have to specify the flags 'CS_VREDRAW' and 'CS_HREDRAW' in order to have your screen update its invalid areas.

Source: http://msdn.microsoft.com/en-us/library/aa925944.aspx

If this is all the code you have for your DC and drawing then the problem is that you aren't drawing to the correct DC that you might think you are.

In WM_CREATE: you are creating an HDC object and then setting it to the current DC. After this case is finished none of that information is kept. The way I think you have it, but I cannot tell because I do not see all your code, is that you have hDC declared globally but not initialized to anything so the line that says HDC screen DC; and replace screenDC with hDC in WM_CREATE:

Sorry, screenDC is supposed to be written where hDC is.. Total mistake on my part. My actual program has everything written correctly in it, and the above problem was reached. Sorry.

Can you post your code then? Because I don't know if you are drawing a background in WM_PAINT: or other problems that might be happening.

I could post the code, but it'll just confuse you. It's rather a lot going on. Basically, nothing is painted in WM_PAINT. The only time anything is being painted to any DC, is on WM_MOUSEMOVE, when I have SetPixel setting a pixel every time the mouse is moved (which results in a line being drawn wherever the mouse is moved). After this line is drawn to the DC on WM_MOUSEMOVE, if the screen is invalidated, everything that was drawn to the DC on WM_MOUSEMOVE gets erased. If the above is not enough information, then I can surely post the code, but as I said.. it is quite a lot of code (very confusing.. lots going on)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.