Solved

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

Posted on 1998-09-24
4
350 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

806 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