Win32 HDC

Mar 16, 2012 at 8:30pm
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
Mar 16, 2012 at 9:01pm
Isn't this the background colour for a window or something?

1
2
WNDCLASSEX wc;
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
Mar 16, 2012 at 9:03pm
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.

EDIT:

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 Mar 16, 2012 at 9:05pm
Mar 16, 2012 at 9:09pm
So if I want to do any painting in my app, I call HDC to do it?
Mar 16, 2012 at 9:22pm
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.
Mar 16, 2012 at 9:28pm
I'm quite new to this Win32 game, let's say I have this in my WndProc

1
2
3
case WM_PAINT:
      hdc = BeginPaint(hWnd, &ps);
      randomPaintFunc(&hdc);


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.

Aside,
When exactly is WndProc called?
Mar 16, 2012 at 9:37pm
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.
Mar 16, 2012 at 9:46pm
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
Mar 16, 2012 at 11:43pm
Quick question, will I need to use PAINTSTRUCT also to paint something? I feel like I need more than just HDC to paint
Mar 16, 2012 at 11:48pm
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.
Mar 16, 2012 at 11:49pm
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

EDIT:
Well it looks like PAINTSTRUCT actually has HDC defined in it. Why would I have both declared?
Last edited on Mar 16, 2012 at 11:51pm
Mar 17, 2012 at 2:56am
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.