如何把两个CMemDC类代码整合在一起?

yschat2012 2017-01-19 08:58:08
在整合两个不同的代码时,两个不同的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中,怎么处理?谢谢!
...全文
252 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eleven 2017-01-22
  • 打赏
  • 举报
回复
class CMemDC : public CDC
{
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;
};

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧