Link to home
Start Free TrialLog in
Avatar of jhanson040697
jhanson040697

asked on

Unable to Get a Device Context (using CWnd::GetDC())

I'm writing an application that allows users to place bitmaps on the screen using the mouse. I need to draw bitmaps when the user pushes the mouse button or holds the button and drags. Therefore I can't just use the device context from the OnDraw function, I need to use GetDC() in the OnButtonDown and OnMouseMove functions. This works fine for the first few hundred bitmaps but eventually I get back NULL from the GetDC(). I have checked, and I can't find anyplace that I'm not calling ReleaseDC. Is there any other reason that calling GetDC could fail? It seems like I am using up some resource. Is it possible to get this problem by using up some resource other than device contexts? I have also noticed that once I start getting NULL back from GetDC, none of the other windows in my app will display correctly. By the way, I am using VC++ 5 and running Windows XP.
Avatar of shilpa_Jb
shilpa_Jb

Hi,
Just check for the return value of the RealseDC() i suspect that its not releasing the resource and hence GetDC
is returning NULL.
-Shilpa
Why you want to use GetDC?

U can declare a CClientDC object instead

try this
CClientDC dc(this);
The constructor of CClientDC takes care of calling GetDC() and destructor takes of ReleaseDC(),
If you want to do it manually, u need to be more careful in coding
ASKER CERTIFIED SOLUTION
Avatar of kirthir
kirthir

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 jhanson040697

ASKER

Thanks for the help. I replaced all the GetDCs with ClientDCs an it fixed my problem. I must have missed a ReleaseDC somewhere.