Re: Device Context Confusion
- From: eepcat <eepcat@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 4 Dec 2007 05:53:04 -0800
Thank you. That makes sense. So, do you see anything wrong with this
OnPaint( ) method...
void CMyWhackyClass::OnPaint()
{
CPaintDC dc(this); // device context for painting
CDC MemDC;
CBitmap MemBitmap, *pOldBitmap;
CPen *pOldPen=NULL;
CBrush *pOldBrush=NULL;
CFont *pOldFont=NULL;
MemDC.CreateCompatibleDC(&dc);
MemBitmap.CreateCompatibleBitmap(&dc, 100, 100);
pOldBitmap = MemDC.SelectObject(&MemBitmap);
pOldPen = MemDC.GetCurrentPen();
pOldBrush = MemDC.GetCurrentBrush();
pOldFont = MemDC.GetCurrentFont();
// these drawing resources are members of the dialog class - created at
// OnInitDlg( ) and deleted OnDestroy( )...
MemDC.SelectObject(&SomePen);
MemDC.SelectObject(&SomeBrush);
MemDC.SetTextColor(WHITE);
MemDC.SetBkColor(BLACK);
MemDC.SelectObject(&SomeFont);
// do drawing here...
// update display...
dc.BitBlt(100, 100, 100, 100, &MemDC, 0, 0, SRCCOPY);
if (pOldBitmap != NULL)
{
MemDC.SelectObject(pOldBitmap);
}
if (pOldPen != NULL)
{
MemDC.SelectObject(pOldPen);
}
if (pOldBrush != NULL)
{
MemDC.SelectObject(pOldBrush);
}
if (pOldFont != NULL)
{
MemDC.SelectObject(pOldFont);
}
this->ReleaseDC(&dc);
bReturn = ::DeleteDC(MemDC);
bReturn = ::DeleteObject(MemBitmap);
}
"voidcoder" wrote:
.
You are only deleting DCs created manually (e.g. using CreateDC())
and releasing DCs returned by GetDC(). The only exception is
WM_PAINT, where you are obtaining DC with BeginPaint() and releasing
with EndPaint(). Note that in some contexts DC is provided by
GDI and there is no need to release it (e.g. WM_ERASEBKGND etc).
In all cases, the right ways is to select the old objects first
and after that it doesn't matter whether you are freeing DC
first or releasing the objects.
HPEN hPen = CreatePen(...);
HBRUSH hBrush = CreateSolidBrush(...);
HFONT hFont = CreateFont(...);
HDC hDC = GetDC(hWnd);
HGDIOBJ hOldPen = SelectObject(hDC, hPen);
HGDIOBJ hOldBrush = SelectObject(hDC, hBrush);
HGDIOBJ hOldFont = SelectObject(hDC, hFont);
<some drawing here>
SelectObject(hDC, hOldPen);
SelectObject(hDC, hOldBrush);
SelectObject(hDC, hOldFont);
ReleaseDC(hDC);
--
Oleg
eepcat wrote:
Hello,
In the course of debugging some memory leaks, I noticed that my graphics
routines were rather inconsistent. Sometimes I ReleaseDC( ), then
DeleteObject( ) on the drawing resources; sometimes vice versa. The examples
I see throughout forums and MSDN proper also show inconsistent ordering.
So, suppose I create:
1 Pen, 1 Brush, 1 Font, 1 Compatible Bitmap within a function. At the end
of this function, what is the proper cleanup?...
Delete the drawing resources first, then ReleaseDC()? Vice versa?
Sometimes I've seen DeleteDC() rather than ReleaseDC(). I want to structure
the cleanup properly so that I can eliminate this situation as a cause of
resource leaks.
Thanks to all,
- eep!
- Follow-Ups:
- Re: Device Context Confusion
- From: Hans-Peter Diettrich
- Re: Device Context Confusion
- References:
- Device Context Confusion
- From: eepcat
- Re: Device Context Confusion
- From: voidcoder
- Device Context Confusion
- Prev by Date: Re: about startup problem
- Next by Date: Re: Device Context Confusion
- Previous by thread: Re: Device Context Confusion
- Next by thread: Re: Device Context Confusion
- Index(es):
Relevant Pages
|
Loading