1, the first memdc. H
# # ifndef _MEMDC_H_
The class CMemDC: public CDC
{
Public:
//the constructor sets up the memory DC
CMemDC (pDC) CDC * : CDC ()
{
ASSERT (pDC!=NULL);
M_pDC=pDC;
M_pOldBitmap=NULL;
M_bMemDC=! PDC - & gt; IsPrinting ();
If (m_bMemDC)//Create a Memory DC
{
PDC - & gt; GetClipBox (& amp; M_rect);
CreateCompatibleDC (pDC);
M_bitmap. CreateCompatibleBitmap (pDC, m_rect Width (), m_rect. Height ());
M_pOldBitmap=SelectObject (& amp; M_bitmap);
SetWindowOrg (m_rect left, m_rect. Top);
}
The else//Make a copy of the relevent parts of the current DC for printing
{
M_bPrinting=pDC - & gt; M_bPrinting;
M_hDC=pDC - & gt; M_hDC;
M_hAttribDC=pDC - & gt; M_hAttribDC;
}
}
//Destructor copies the contents of the mem DC to the the original DC
~ CMemDC ()
{
If (m_bMemDC)
{
//Copy the offscreen bitmap onto the screen.
M_pDC - & gt; BitBlt (m_rect m_rect. Left, top, m_rect. The Width (), m_rect. Height (),
This, m_rect left, m_rect. Top SRCCOPY);
//Swap back the the original bitmap.
SelectObject (m_pOldBitmap);
} else {
//All we need to do is replace the DC with an illegal value,
//this keeps us from accidently deleting the handles associated with
//the CDC that was passed to the constructor.
M_hDC=m_hAttribDC=NULL;
}
}
//Allow the usage as a pointer
CMemDC * operator - & gt; () {return this; }
//Allow the usage as a pointer
Operator CMemDC * () {return this; }
Private:
CBitmap m_bitmap;//Offscreen bitmap
CBitmap * m_pOldBitmap;//bitmap originally found in CMemDC
The CDC * m_pDC;//Saves the CDC passed in the constructor
CRect m_rect;//a Rectangle of drawing area.
BOOL m_bMemDC;//TRUE if the CDC really is a Memory DC.
};
# endif
2, the second class is memdc. CPP and memdc h two files
Memdc. H
The class CMemDC: public CDC
{
//Construction
Public:
CMemDC (UINT heigh, UINT width);
//the Attributes
Public:
//Implementation
Public:
HBITMAP CloseMemDC ();
Virtual ~ CMemDC ();
Private:
CBitmap m_bitmap;
CBitmap * m_pOldBitmap;
UINT m_bmpHeigh;
UINT m_bmpWidth;
};
Memdc. CPP
# include "stdafx. H"
# include "MemDC. H"
# ifdef _DEBUG
# define new DEBUG_NEW
# undef THIS_FILE
The static char THIS_FILE []=__FILE__;
# endif
CMemDC: : CMemDC (UINT width, UINT heigh)
{
The CDC * pDC=CDC: : FromHandle (: : GetDC (GetDesktopWindow ()));
ASSERT (pDC!=NULL);
CreateCompatibleDC (pDC);
M_bmpWidth=width;
M_bmpHeigh=heigh;
M_bitmap. CreateCompatibleBitmap (pDC, m_bmpWidth m_bmpHeigh);
M_pOldBitmap=SelectObject (& amp; M_bitmap);
: : ReleaseDC (GetDesktopWindow (), pDC - & gt; M_hDC);
}
CMemDC: : ~ CMemDC ()
{
SelectObject (m_pOldBitmap);
M_bitmap. DeleteObject ();
DeleteDC ();
}
HBITMAP CMemDC: : CloseMemDC ()
{
SelectObject (m_pOldBitmap);
Return (HBITMAP) (m_bitmap Detach ());
}
Now want to put the back of the MemDC class integrated into the first MemDC. H, how to deal with? Thank you very much!

CodePudding user response:
class CMemDC: public CDC
{
Public:
CMemDC (UINT heigh, UINT width)
{
The CDC * pDC=CDC: : FromHandle (: : GetDC (GetDesktopWindow ()));
ASSERT (pDC!=NULL);
CreateCompatibleDC (pDC);
M_bmpWidth=width;
M_bmpHeigh=heigh;
M_bitmap. CreateCompatibleBitmap (pDC, m_bmpWidth m_bmpHeigh);
M_pOldBitmap=SelectObject (& amp; M_bitmap);
: : ReleaseDC (GetDesktopWindow (), pDC - & gt; M_hDC);
M_nIndex=1;
}
//the constructor sets up the memory DC
CMemDC (pDC) CDC * : CDC ()
{
ASSERT (pDC!=NULL);
M_pDC=pDC;
M_pOldBitmap=NULL;
M_bMemDC=! PDC - & gt; IsPrinting ();
If (m_bMemDC)//Create a Memory DC
{
PDC - & gt; GetClipBox (& amp; M_rect);
CreateCompatibleDC (pDC);
M_bitmap. CreateCompatibleBitmap (pDC, m_rect Width (), m_rect. Height ());
M_pOldBitmap=SelectObject (& amp; M_bitmap);
SetWindowOrg (m_rect left, m_rect. Top);
}
The else//Make a copy of the relevent parts of the current DC for printing
{
M_bPrinting=pDC - & gt; M_bPrinting;
M_hDC=pDC - & gt; M_hDC;
M_hAttribDC=pDC - & gt; M_hAttribDC;
}
M_nIndex=0;
}
//Destructor copies the contents of the mem DC to the the original DC
~ CMemDC ()
{
If (1==m_nIndex)
{
SelectObject (m_pOldBitmap);
M_bitmap. DeleteObject ();
DeleteDC ();
}
The else {
If (m_bMemDC)
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull