www.pudn.com > Éù²¨ÆµÂÊÏÔʾ .zip > OscilloGraph.cpp


/********************************************************************/ 
/*																	*/ 
/*  OscilloGraph.cpp												*/ 
/*																	*/ 
/*  Implementation of the COscilloGraph.							*/ 
/*																	*/ 
/*  Programmed by Pablo van der Meer								*/ 
/*  Copyright Pablo Software Solutions 2003							*/ 
/*	http://www.pablovandermeer.nl									*/ 
/*																	*/ 
/*  Last updated: 11 January 2003									*/ 
/*																	*/ 
/********************************************************************/ 
 
 
#include "stdafx.h" 
#include "OscilloGraph.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
 
COscilloGraph::COscilloGraph() 
{ 
	m_pOldBitmap = NULL; 
	m_pBuffer = NULL; 
	m_dwBufferLength = 0; 
	m_clrPixel = RGB(0,255,0); 
	m_clrBackground = RGB(0,0,0); 
} 
 
COscilloGraph::~COscilloGraph() 
{ 
	if (m_pOldBitmap != NULL) 
		m_MainDC.SelectObject(m_pOldBitmap);   
 
	if (m_pBuffer) 
		delete m_pBuffer; 
} 
 
 
BEGIN_MESSAGE_MAP(COscilloGraph, CStatic) 
	//{{AFX_MSG_MAP(COscilloGraph) 
	ON_WM_PAINT() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
 
 
/********************************************************************/ 
/*																	*/ 
/* Function name : OnPaint											*/ 
/* Description   : Called when the application makes a request to	*/ 
/*				   repaint a portion of the window.					*/ 
/*																	*/ 
/********************************************************************/ 
void COscilloGraph::OnPaint()  
{ 
	CPaintDC dc(this); // device context for painting 
	 
	CRect rect; 
	GetClientRect(&rect); 
 
	CDC memDC; 
	CBitmap memBitmap; 
	CBitmap* oldBitmap; 
 
	// to avoid flicker, establish a memory dc, draw to it and then BitBlt it to the client 
	memDC.CreateCompatibleDC(&dc); 
	memBitmap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height()); 
	oldBitmap = (CBitmap *)memDC.SelectObject(&memBitmap); 
 
	if (memDC.GetSafeHdc() != NULL) 
	{ 
		// first drop the bitmap on the memory dc 
		memDC.BitBlt(0, 0, rect.Width(), rect.Height(), &m_MainDC, 0, 0, SRCCOPY); 
		// finally send the result to the display 
		dc.BitBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY); 
	} 
	memDC.SelectObject(oldBitmap); 
} 
 
 
/********************************************************************/ 
/*																	*/ 
/* Function name : OnSize											*/ 
/* Description   : The framework calls this member function after	*/ 
/*				   the window’s size has changed.					*/ 
/*																	*/ 
/********************************************************************/ 
void COscilloGraph::OnSize(UINT nType, int cx, int cy)  
{ 
	// OnSize automatically gets called during the setup of the control 
	CStatic::OnSize(nType, cx, cy); 
	 
	CRect rect; 
	GetClientRect(&rect); 
 
	// destroy and recreate the main bitmap 
	CClientDC dc(this); 
 
	if (m_pOldBitmap && m_MainBitmap.GetSafeHandle() && m_MainDC.GetSafeHdc()) 
	{ 
		m_MainDC.SelectObject(m_pOldBitmap); 
		m_MainBitmap.DeleteObject(); 
		m_MainBitmap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height()); 
		m_pOldBitmap = m_MainDC.SelectObject(&m_MainBitmap); 
	}	 
} 
 
 
/********************************************************************/ 
/*																	*/ 
/* Function name : DrawCtrl											*/ 
/* Description   : Render the vumeter to a bitmap.					*/ 
/*																	*/ 
/********************************************************************/ 
void COscilloGraph::DrawCtrl() 
{ 
	CRect rect; 
	GetClientRect(&rect); 
 
	// in case we haven't established the memory dc's 
	CClientDC dc(this); 
 
	// if we don't have one yet, set up a memory dc for the control  
	if (m_MainDC.GetSafeHdc() == NULL) 
	{ 
		m_MainDC.CreateCompatibleDC(&dc); 
		m_MainBitmap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height()); 
		m_pOldBitmap = m_MainDC.SelectObject(&m_MainBitmap); 
	} 
 
	m_MainDC.Draw3dRect(rect, GetSysColor(COLOR_3DSHADOW), GetSysColor(COLOR_3DHILIGHT)); 
    rect.InflateRect(-2, -2); 
 
	// stay between the lines... 
	CRgn rgn; 
	rgn.CreateRectRgn(rect.left, rect.top, rect.right, rect.bottom); 
	m_MainDC.SelectClipRgn(&rgn); 
 
	m_MainDC.FillSolidRect(rect, m_clrBackground); 
 
	if (m_dwBufferLength) 
	{ 
		int nStep = rect.Width()/m_dwBufferLength; 
 
		if (nStep == 0) 
			nStep = 1; 
 
		for(DWORD i=0; i