Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 1998-09-24
4
Medium Priority
?
358 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 400 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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 quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

876 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