SideEffect
SideEffect

Reputation: 401

WinAPI - avoid redraw of window

I'm drawing into a WinAPI-Window by using the SetPixel()-Function.

If I scale the window or lose focus (another window is on top) I lose the whole content that I draw into the window.

I just used

RECT rc;
GetClientRect(hwnd, &rc);
RedrawWindow(hwnd, &rc, NULL, RDW_NOERASE | RDW_NOFRAME | RDW_VALIDATE);

which helped to avoid redrawing the content when I move the window but scaling and losing focus still removes the content. Does anyone have an idea what I missed?

Upvotes: 0

Views: 1756

Answers (5)

RobH
RobH

Reputation: 3338

When a window needs to be repainted it will be sent a WM_PAINT message. At this point you must redraw all of the window, or at least all parts of it which are contained within a clipping region. Windows does some buffering and automatic painting, specifically it will repaint parts of a window which are covered by other windows then uncovered. Once the window has been resized though, or (presumably) invalidated, you're on your own.

As @daniel suggested, if painting is an intensive process and you don't want to do it every time a repaint is required, render your content into a bitmap (which in this case will be an off-screen buffer) and BitBlt (copy) it into the window as necessary.

Grab yourself a copy of Charles Petzold's book "Programming Windows" for information about how you should go about painting. If you are writing a WinAPI app but have used SetPixel I'd recommend reading the entirety of the first few chapters to get an idea of how an old-school Windows programme should be structured.

Upvotes: 3

Alex F
Alex F

Reputation: 43311

SetPixel is very slow, you cannot improve your program significantly. Create in-memory bitmap and draw it on the window. For example, you can do this using StretchDIBits function, which draws the whole memory area as bitmap to the window, instead of SetPixel.

The most important StretchDIBits parameters are:

CONST VOID *lpBits - memory array (pixels). You need to fill it in memory instead of SetPixel calls.

CONST BITMAPINFO *lpBitsInfo - BITMAPINFO structure which must describe bitmap structure. For example, if lpBits has BGRW structure (4 bytes per pixel), BITMAPINFO must describe true color bitmap.

Upvotes: 2

Mike
Mike

Reputation: 1727

You need to draw the content into memory and then draw it to the window when you got WM_PAINT message. There is no way to avoid using memory buffer because window device context does not save what you draw.

Upvotes: 1

macbirdie
macbirdie

Reputation: 16193

Create a DIB surface and draw into it instead. Then redraw the bitmap when redrawing a window.

You're trying to draw with a pre-Windows way in Windows. ;)

Upvotes: 0

Daniel A. White
Daniel A. White

Reputation: 190907

Draw it to a buffer/bitmap and then draw that to your window.

Upvotes: 6

Related Questions