Solved

How to keep CDC* pDC around for life of application?

Posted on 1998-09-24
4
344 Views
Last Modified: 2013-11-19
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
0
Comment
Question by:tpnguyen
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 100 total points
ID: 1322579
Add these members variables to your class

class whatever // class derived from CView or CWnd or one of their derived classes (e.g. CFormView)
{
 // etc
private:
  CDC * m_pDC ;
} ;


In class' constructor
{
  m_pDC = NULL ;
}

In class'  destructor
{
  ASSERT( m_pDC == NULL ) ;
}


Once the window is up (e.g. OnInitialUpdate) you can initialize m_pDC (do this only once)
{
HDC hDC = ::GetDC( m_hWnd ) ;
m_pDC = new CDC ;
m_pDC->Attach( hDC ) ;
}

When window closes (e.g. OnDestroy) release the DC (do this only once)
{
ASSERT( m_pDC != NULL ) ;
HDC hDC = m_pDC->m_hDC ;
m_pDC->Detach() ;
delete m_pDC ;
m_pDC = NULL ;
::ReleaseDC( m_hWnd, hDC ) ;
}

This is a little unfriendly as you are grabbing control of a limited resource for an extended period (e.g. you can run out of available DCs especially under Win95 if lots of copies of your app or lots of apps that do like you)
0
 
LVL 3

Expert Comment

by:plaroche
ID: 1322580
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.
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1322581
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)
0
 

Author Comment

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

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now