www.pudn.com > 3DEDITOR.rar > GLMEMORYDC.CPP


// GLMemoryDC.cpp: implementation of the GLMemoryDC class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "3DEditor.h" 
#include "GLMemoryDC.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CGLMemoryDC::CGLMemoryDC() 
{ 
    m_hBitmap = NULL;       
	memset(&m_DIBInfo, 0, sizeof(BITMAPINFO));   
    m_hImage = NULL;  
} 
 
CGLMemoryDC::~CGLMemoryDC() 
{ 
	ClearMemory(); 
} 
 
/********************************************************************/ 
/* 清除所有占用的内存区域   											    */ 
/********************************************************************/ 
void CGLMemoryDC::ClearMemory(void) 
{ 
    if(m_hBitmap) 
		DeleteObject(m_hBitmap); 
	m_hBitmap = NULL;       
	memset(&m_DIBInfo, 0, sizeof(BITMAPINFO));   
    m_hImage = NULL;  
} 
 
 
/********************************************************************/ 
/* 设置内存区域大小             								    */ 
/********************************************************************/ 
BOOL CGLMemoryDC::SetMemorySize(int width, int height) 
{ 
	//清除内存 
	ClearMemory(); 
 
	//设置 DIB 图像头信息 
	m_DIBInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 
	m_DIBInfo.bmiHeader.biCompression = BI_RGB; 
    m_DIBInfo.bmiHeader.biPlanes = 1; 
    m_DIBInfo.bmiHeader.biBitCount = 32; 
    m_DIBInfo.bmiHeader.biWidth = width; 
    m_DIBInfo.bmiHeader.biHeight = height; 
    m_DIBInfo.bmiHeader.biSizeImage = width*height*4; 
 
	//创建 BITMAP 颜色数据内存区域 
    m_hBitmap = CreateDIBSection( 
		        NULL, 
				&m_DIBInfo, 
				DIB_RGB_COLORS, 
				(void **)&m_hImage, 
                NULL, 
				NULL); 
     
	//如果成功 
	if(m_hBitmap) 
		return TRUE; 
	else 
        return FALSE;    
} 
 
 
/********************************************************************/ 
/* 获得设备描述表内存           								    */ 
/********************************************************************/ 
void CGLMemoryDC::GetMemorySize(int* width, int* height) 
{ 
    *width = m_DIBInfo.bmiHeader.biWidth; 
    *height = m_DIBInfo.bmiHeader.biHeight; 
} 
 
 
/********************************************************************/ 
/* 从设备描述表中拷贝数据   										*/ 
/********************************************************************/ 
void CGLMemoryDC::CopyDataFromDC(CDC* pDC, CRect& rect) 
{ 
	CDC      dcBuffer;       //兼容 DC  
	CBitmap  bmBitmap;		 
	CBitmap* pbmBitmapOld; 
 
	//如果最初的位图没有设置 
	if(!m_hBitmap) 
        return; 
 
	//创建兼容的DC,拷贝数据 
	dcBuffer.CreateCompatibleDC(pDC); 
 
	//创建内存位图  
    bmBitmap.CreateCompatibleBitmap(pDC, 
                  m_DIBInfo.bmiHeader.biWidth, 
                  m_DIBInfo.bmiHeader.biHeight); 
     
	//set memory bitmap to memory DC 
	pbmBitmapOld = dcBuffer.SelectObject(&bmBitmap); 
     
	//拷贝源DC图像到内存位图中 
	dcBuffer.StretchBlt(0, 0,   
             m_DIBInfo.bmiHeader.biWidth, 
	         m_DIBInfo.bmiHeader.biHeight, 
			 pDC,  
             rect.left, 
			 rect.top, 
			 rect.Width(), 
			 rect.Height(), 
			 SRCCOPY); 
 
	//在内存DC中恢复最初的对象 
    dcBuffer.SelectObject(pbmBitmapOld); 
 
	//从内存位图中拷贝图像数据 
	GetDIBits(pDC->m_hDC, 
		(HBITMAP)bmBitmap,  
		0,  
        m_DIBInfo.bmiHeader.biHeight, 
        m_hImage, 
		&m_DIBInfo, 
		DIB_RGB_COLORS); 
} 
 
 
/********************************************************************/ 
/* 拷贝数据到目标DC                                                 */ 
/********************************************************************/ 
void CGLMemoryDC::CopyDataToDC(CDC* pDC, CRect& rect) 
{ 
    ::StretchDIBits(pDC->m_hDC,  
		            rect.left,  
					rect.top,  
		            rect.Width(),  
					rect.Height(), 
                    0, 0,  
					m_DIBInfo.bmiHeader.biWidth,  
					m_DIBInfo.bmiHeader.biHeight, 
                    m_hImage,   
					&m_DIBInfo,  
					DIB_RGB_COLORS,  
					SRCCOPY); 
}