Solved

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

Posted on 1998-09-24
4
354 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Title # Comments Views Activity
List out all word 7 359
while loop over for loop 7 119
only14 challenge 19 89
EvenOdd challenge 10 160
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…
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.
Suggested Courses

752 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