tpnguyen
asked on
How to keep CDC* pDC around for life of application?
Currently, I use MFC (SDI application), and I need to refer to CDC* pDC for drawing at many places, however, these are not called from within OnDraw() or OnPaint() (basically I can not pass in CDC), every time I need the DC, I use GetDC() to get a pointer to it. Unfortunally, these calls happen quite a lot, I end up call GetDC() too many time, and this is not efficient, and everytime I call GetDC() it seems to erase what I draw before. My question is:
Is there a way to get DC once and refer to it when needed? It look like the pointer returns from GetDC() is just a temporary pointer, it becomes NULL as soon as I get out of function scope. Thanks,
-Thang
Is there a way to get DC once and refer to it when needed? It look like the pointer returns from GetDC() is just a temporary pointer, it becomes NULL as soon as I get out of function scope. Thanks,
-Thang
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can grab a DC for extending period, it's just a bit "naughty".
Guilty Admission: I did infact do this in one my apps (SDK rather MFC style, but to the same effect)
Guilty Admission: I did infact do this in one my apps (SDK rather MFC style, but to the same effect)
ASKER
Thank you both Answers2000 & plaroche. It worked ! I will take into consideration of the resouces. Thanks again.
I think it'd be preferable to create a compatible DC and bitmap as member variables, do the drawing to that dc and in OnDraw or OnPaint call a BitBlt.
This may be a design issue but keeping a dc for such a long time does not seem apropriate.