Question about GetDC()

In functions such as ::OnMouseMove(), I need to draw to the view area. To do this, I need a DC for the view. I can make one, or use GetDC(). My question is in two parts:
1. What exactly does GetDC() do? Is it safe to call this function many times over (releasing it each time, of course) as will happen in functions like OnMouseMove()? In the help it makes some mention of "if DC is available" - this doesn't make me feel too good.
2. Is there a View DC (the one that is passed by MFC into functions like OnDraw()) that is declared as a member of CView, one that will always exist, or does this have to be recreated every time one draws to a view?
[664 byte] By [Slimfinger] at [2007-11-16 5:41:51]
# 1 Re: Question about GetDC()
I've never tried this, so I'm not sure if it's a good idea or not, but perhaps you can make a member of your class a pointer to the DC, get the DC ONE time at the start of the code, then use the pointer on each subsequent instance that the DC is required. Does anyone know if the DC ever actually changes from call to call, or could this work in speeding up code which would require many updates in rapid succession like this?

--Kelly
Kelly at 2007-11-10 4:42:18 >
# 2 Re: Question about GetDC()
Storing a DC across calls should present no problems. A DC is just a block of information that Windows keeps to track the current foreground/background colors, current X/Y position for text output, brushes etc.

There are also window style bits that allow Windows to create and keep the same DC for a window instead of creating a new one each time ; there is also a bit that creates a single DC for a window class i.e. it gets shared by all windows of that class.
taka at 2007-11-10 4:43:19 >
# 3 Re: Question about GetDC()
A better way to do it is to call Invalidate() or InvalidateRect() in your OnMouseMove(). It will automatically invoke OnDraw() and you can do your drawing in there. If you want to re-draw differenltly, you can use some class members as parameters: set them in OnMouseMove and then pick them up in OnDraw().
As for keeping a pointer to the DC as a class member, it should not cause any harm. Though, it is not nice.
at 2007-11-10 4:44:19 >
# 4 Re: Question about GetDC()
In the old Windows days, there are only five (common) DCs. That is why the phrase 'if DC is available'. In Win32 program, you should not worriy about that, unless your program has big resource leak problem.

If the common DCs are used up, new DC will be created.

GetDC retrieves an available common DC handle, resets it to its default values. It's safe to keep the DC and reuse them. But you should always call BeginPaint and EndPaint in WM_PAINT message handling, unless you know exactly what's BeginPaint and EndPaint is doing. I guess BeginPaint calls GetDCEx to get the DC and validate the update region, among other stuff like handling background painting.
Feng Yuan at 2007-11-10 4:45:14 >
# 5 Re: Question about GetDC()
Hi,

It's safe as far as u release it.U r actually getting the dc from the view.
If u want another one u can create it by CreateDC or Create CompatibleDC.But make sure to release it.Windows has a limited no. of resources(DC).NT...is much better in that.

Happy programming
sreejithev at 2007-11-10 4:46:14 >