在整合两个不同的代码时,两个不同的CMemDC类产生了冲突。现在想把它们整合在一起。但是一个只有memdc.h文件,另一个有memdc.cpp和memdc.h两个文件,不知怎么整合?
1、第一个memdc.h
#ifndef _MEMDC_H_
class CMemDC : public CDC
{
public: // constructor sets up the memory DC
CMemDC(CDC* pDC) : CDC()
{
ASSERT(pDC != NULL); m_pDC = pDC;
m_pOldBitmap = NULL;
m_bMemDC = !pDC->IsPrinting();
if (m_bMemDC) // Create a Memory DC
{
pDC->GetClipBox(&m_rect);
CreateCompatibleDC(pDC);
m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
m_pOldBitmap = SelectObject(&m_bitmap);
SetWindowOrg(m_rect.left, m_rect.top);
}
else // Make a copy of the relevent parts of the current DC for printing
{
m_bPrinting = pDC->m_bPrinting;
m_hDC = pDC->m_hDC;
m_hAttribDC = pDC->m_hAttribDC;
}
}
// Destructor copies the contents of the mem DC to the original DC
~CMemDC()
{
if (m_bMemDC)
{
// Copy the offscreen bitmap onto the screen.
m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
this, m_rect.left, m_rect.top, SRCCOPY); //Swap back 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 usage as a pointer
CMemDC* operator->() {return this;}
// Allow usage as a pointer
operator CMemDC*() {return this;}private:
CBitmap m_bitmap; // Offscreen bitmap
CBitmap* m_pOldBitmap; // bitmap originally found in CMemDC
CDC* m_pDC; // Saves CDC passed in constructor
CRect m_rect; // Rectangle of drawing area.
BOOL m_bMemDC; // TRUE if CDC really is a Memory DC.
};#endif
2、第二个类是有memdc.cpp和memdc.h两个文件
memdc.h
class CMemDC : public CDC
{
// Construction
public:
CMemDC(UINT heigh,UINT width);// 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
static char THIS_FILE[] = __FILE__;
#endif
CMemDC::CMemDC(UINT width,UINT heigh)
{
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(&m_bitmap);
::ReleaseDC(GetDesktopWindow(), pDC->m_hDC);
}CMemDC::~CMemDC()
{
SelectObject(m_pOldBitmap);
m_bitmap.DeleteObject();
DeleteDC();
}
HBITMAP CMemDC::CloseMemDC()
{
SelectObject(m_pOldBitmap);
return (HBITMAP)(m_bitmap.Detach());
}现在想把后面的MemDC类整合到第一个memdc.h中,怎么处理?谢谢!
1、第一个memdc.h
#ifndef _MEMDC_H_
class CMemDC : public CDC
{
public: // constructor sets up the memory DC
CMemDC(CDC* pDC) : CDC()
{
ASSERT(pDC != NULL); m_pDC = pDC;
m_pOldBitmap = NULL;
m_bMemDC = !pDC->IsPrinting();
if (m_bMemDC) // Create a Memory DC
{
pDC->GetClipBox(&m_rect);
CreateCompatibleDC(pDC);
m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
m_pOldBitmap = SelectObject(&m_bitmap);
SetWindowOrg(m_rect.left, m_rect.top);
}
else // Make a copy of the relevent parts of the current DC for printing
{
m_bPrinting = pDC->m_bPrinting;
m_hDC = pDC->m_hDC;
m_hAttribDC = pDC->m_hAttribDC;
}
}
// Destructor copies the contents of the mem DC to the original DC
~CMemDC()
{
if (m_bMemDC)
{
// Copy the offscreen bitmap onto the screen.
m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
this, m_rect.left, m_rect.top, SRCCOPY); //Swap back 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 usage as a pointer
CMemDC* operator->() {return this;}
// Allow usage as a pointer
operator CMemDC*() {return this;}private:
CBitmap m_bitmap; // Offscreen bitmap
CBitmap* m_pOldBitmap; // bitmap originally found in CMemDC
CDC* m_pDC; // Saves CDC passed in constructor
CRect m_rect; // Rectangle of drawing area.
BOOL m_bMemDC; // TRUE if CDC really is a Memory DC.
};#endif
2、第二个类是有memdc.cpp和memdc.h两个文件
memdc.h
class CMemDC : public CDC
{
// Construction
public:
CMemDC(UINT heigh,UINT width);// 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
static char THIS_FILE[] = __FILE__;
#endif
CMemDC::CMemDC(UINT width,UINT heigh)
{
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(&m_bitmap);
::ReleaseDC(GetDesktopWindow(), pDC->m_hDC);
}CMemDC::~CMemDC()
{
SelectObject(m_pOldBitmap);
m_bitmap.DeleteObject();
DeleteDC();
}
HBITMAP CMemDC::CloseMemDC()
{
SelectObject(m_pOldBitmap);
return (HBITMAP)(m_bitmap.Detach());
}现在想把后面的MemDC类整合到第一个memdc.h中,怎么处理?谢谢!
{
public:
CMemDC(UINT heigh,UINT width)
{
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(&m_bitmap);
::ReleaseDC(GetDesktopWindow(), pDC->m_hDC);
m_nIndex = 1;
}
// constructor sets up the memory DC
CMemDC(CDC* pDC) : CDC()
{
ASSERT(pDC != NULL);
m_pDC = pDC;
m_pOldBitmap = NULL;
m_bMemDC = !pDC->IsPrinting();
if (m_bMemDC) // Create a Memory DC
{
pDC->GetClipBox(&m_rect);
CreateCompatibleDC(pDC);
m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
m_pOldBitmap = SelectObject(&m_bitmap);
SetWindowOrg(m_rect.left, m_rect.top);
}
else // Make a copy of the relevent parts of the current DC for printing
{
m_bPrinting = pDC->m_bPrinting;
m_hDC = pDC->m_hDC;
m_hAttribDC = pDC->m_hAttribDC;
}
m_nIndex = 0;
}
// Destructor copies the contents of the mem DC to the original DC
~CMemDC()
{
if(1==m_nIndex)
{
SelectObject(m_pOldBitmap);
m_bitmap.DeleteObject();
DeleteDC();
}
else{
if (m_bMemDC)
{
// Copy the offscreen bitmap onto the screen.
m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
this, m_rect.left, m_rect.top, SRCCOPY);
//Swap back 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 usage as a pointer
CMemDC* operator->() {return this;}
// Allow usage as a pointer
operator CMemDC*() {return this;}
HBITMAP CMemDC::CloseMemDC()
{
SelectObject(m_pOldBitmap);
return (HBITMAP)(m_bitmap.Detach());
}
private:
CBitmap m_bitmap; // Offscreen bitmap
CBitmap* m_pOldBitmap; // bitmap originally found in CMemDC
CDC* m_pDC; // Saves CDC passed in constructor
CRect m_rect; // Rectangle of drawing area.
BOOL m_bMemDC; // TRUE if CDC really is a Memory DC.
UINT m_bmpHeigh;
UINT m_bmpWidth;
Int m_nIndex;
};