Win32 HDC

Alright, so what is this? Best I've found was "A handle to the display DC used for painting."

I seem to remember this when I dabbled with OpenGL, but can't remember what it means. What is a "display DC?"

Thank you
Isn't this the background colour for a window or something?

wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);

Or maybe
wc.hbrBackground = CreateSolidBrush(RGB(255,150,150));

Or maybe
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);

Is what you are looking for
DC stands for "device context" which is about as descriptive as it can be. It basically holds state information for GDI drawing tools like bitmaps, brushes, pens, etc.

Think of it like an art easel. Each easel is assigned a bunch of pens (HPEN), some paint cans (HBRUSH), a canvas (HBITMAP), and some other junk. Drawing operations that use a DC may use one or more of these assigned drawing tools. But the drawing tools themselves can be moved to and from other DCs.


The "display DC" is the DC used by the current display. IE whatever is visible on that DC is what is visible on the window.

This is constrast to an "offscreen DC" which retrains drawing and state information but isn't visible on the window.
Last edited on
So if I want to do any painting in my app, I call HDC to do it?
If you are using WinGDI for drawing, yes.

The screen DC is typically gotten one of two ways:

1) With BeginPaint / EndPaint (for use when you draw inside your WM_PAINT handler)

2) With GetDC / ReleaseDC (if you need to do drawing outside of the WM_PAINT handler)

Note that BeginPaint/EndPaint does other things besides simply giving you the DC -- so it should be preferred in your WM_PAINT handler.
I'm quite new to this Win32 game, let's say I have this in my WndProc

case WM_PAINT:
      hdc = BeginPaint(hWnd, &ps);

Would this have any issues? If I'm doing any major drawing, I don't really want it all in that one spot. I'd prefer to have it in a function elsewhere.

When exactly is WndProc called?
It is perfectly OK to have a separate function for the actual drawing code, and it is actually the best practice. This is because it is expected that your window also handles WM_PRINT (and WM_PRINTCLIENT). These messages carry their own HDC in the wParam parameter that you must use.

Note that you don't need to pass the HDC by reference or by pointer because it is simply a number. A copy of the HDC is really nothing of concern.
Ah I wasn't sure if it would be better better to pass it by reference or value. I guess thinking about it almost seems better to pass it by value actually
Quick question, will I need to use PAINTSTRUCT also to paint something? I feel like I need more than just HDC to paint
The PAINTSTRUCT can tell you the invalidated region, which can help you draw faster. Drawing fast is paramount for a good user experience. It also tells you if the background has been erased or not.
Hmm so passing in ps would be good as well? That way I can at least use it if need be.

What are the functions used for drawing? I'm having a hard time finding them

Well it looks like PAINTSTRUCT actually has HDC defined in it. Why would I have both declared?
Last edited on
WM_PRINT doesn't provide a PAINTSTRUCT structure. If you are passing something to the function, pass the HDC, the update rectangle/region, and a boolean for the background erasing and make the last two optional.
Topic archived. No new replies allowed.