Solved

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

Posted on 2002-03-18
5
1,074 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:jhanson040697
  • 3
5 Comments
 
LVL 2

Expert Comment

by:shilpa_Jb
ID: 6879277
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
0
 
LVL 1

Expert Comment

by:kirthir
ID: 6879409
Why you want to use GetDC?

U can declare a CClientDC object instead

try this
CClientDC dc(this);
0
 
LVL 1

Expert Comment

by:kirthir
ID: 6879412
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
0
 
LVL 1

Accepted Solution

by:
kirthir earned 100 total points
ID: 6879413
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
0
 

Author Comment

by:jhanson040697
ID: 6881719
Thanks for the help. I replaced all the GetDCs with ClientDCs an it fixed my problem. I must have missed a ReleaseDC somewhere.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question