Link to home
Start Free TrialLog in
Avatar of tpnguyen
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
ASKER CERTIFIED SOLUTION
Avatar of Answers2000
Answers2000

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of plaroche
plaroche

Answers2000 last remark is very good and that solution should not be used.  Programmers should try to stay as friendly as possible with the OS, helping a lot to avoid problems that nobody could foresee.

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.
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)
Avatar of tpnguyen

ASKER

Thank you both Answers2000 & plaroche.   It worked !  I will take into consideration of the resouces.   Thanks again.