Solved

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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
function returning CString in a regular win32 DLL 8 190
bigDiff challenge 17 126
isEverywhere  challenge 19 84
Path of Workbook 3 86
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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 use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

821 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