⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 2dpushgraph.cpp

📁 完成MOTOBUS通信
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* ===================================================================

C2DPushGraph Control (2DPushGraph.h and 2DPushGraph.cpp)

Author:  Stuart Konen
Contact: skonen@gmail.com (Job information welcome)

Description: A push graph control similiar to the graph control located
in Microsoft's Task Manager.

====================================================================*/


#include "stdafx.h"
#include "2DPushGraph.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define PUSHGRAPH_MAX(num, max, type) \
((num > max) ? (type)(max) : (type)(num))

#define PUSHGRAPH_MIN(num, min, type) \
((num < min) ? (type)(min) : (type)(num))


/* Self-Register */
BOOL C2DPushGraph::m_bRegistered = C2DPushGraph::RegisterClass(); 


/////////////////////////////////////////////////////////////////////////////
// C2DPushGraph

C2DPushGraph::C2DPushGraph()
{
	m_nMaxCoords       = -1;
	m_nMoveOffset      = 0;
	m_nPeekOffset      = 0;
	m_bStylesModified  = false;
	m_usLineInterval   = 2;

	SetPeekRange(0, 100);
	
	SetLabelForMax("100%");
	SetLabelForMin("0%");

	SetGridSize(15);	
	

	SetBGColor  ( RGB(0, 0, 0)     );
	SetGridColor( RGB(0, 150, 0)   );
	SetTextColor( RGB(255, 255, 0) );	
}


// ===================================================================

C2DPushGraph::~C2DPushGraph()
{
	while (m_aLines.GetSize())
	{
		delete m_aLines[0];
		m_aLines.RemoveAt(0);
	}
}


// ===================================================================

BOOL C2DPushGraph::RegisterClass()
{
	/* Static function to automatically register this class */

	WNDCLASS wc;
	
	ZeroMemory(&wc, sizeof(WNDCLASS));
                                 
    wc.lpfnWndProc   = ::DefWindowProc;
    wc.hInstance     = (HINSTANCE)::GetModuleHandle(NULL);        
    wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);                
    wc.lpszClassName = _T("C2DPushGraph");	

    return AfxRegisterClass(&wc);
}


BEGIN_MESSAGE_MAP(C2DPushGraph, CWnd)
	//{{AFX_MSG_MAP(C2DPushGraph)
	ON_WM_ERASEBKGND()
	ON_WM_PAINT()
	ON_WM_SIZING()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


// ===================================================================

bool C2DPushGraph::CreateFromStatic( UINT nStaticID, CWnd* pParent )
{
	CStatic  wndStatic;		

	if (pParent == NULL || !wndStatic.SubclassDlgItem(nStaticID, pParent))
	{
		return false;
	}
	

	/* Get the static windows rectangle and transform it into
	parent client coordinates, instead of screen coordinates */

	CRect    rectStatic;

	wndStatic.GetWindowRect(&rectStatic);
	pParent->ScreenToClient(&rectStatic);

		
	if ( !CreateEx( wndStatic.GetExStyle(), 
		            NULL, NULL, WS_CHILD | WS_VISIBLE | wndStatic.GetStyle(),
		            rectStatic, pParent, nStaticID, NULL))
	{		
		wndStatic.DestroyWindow();
		return false;
	}
	
	
	wndStatic.DestroyWindow();
	return true;
}


// ==========================================================================
// Functions for Setting and Retrieving attributes
// ==========================================================================


COLORREF C2DPushGraph::GetGridColor() const
{
	return m_crGridColor;
}


// ===================================================================

void C2DPushGraph::SetGridColor( COLORREF crColor )
{
	m_crGridColor = crColor;
}


// ===================================================================

COLORREF C2DPushGraph::GetTextColor() const
{
	return m_crTextColor;
}


// ===================================================================

void  C2DPushGraph::SetTextColor( COLORREF crColor )
{
	m_crTextColor = crColor;
}


// ===================================================================

COLORREF C2DPushGraph::GetBGColor() const
{
	return m_crBGColor;
}


// ===================================================================

void C2DPushGraph::SetBGColor( COLORREF crColor )
{
	m_crBGColor = crColor;
}


// ===================================================================

int C2DPushGraph::GetGridSize() const
{
	return m_nGridSize;
}


// ===================================================================

void C2DPushGraph::SetGridSize( unsigned short usWidthAndHeight )
{
	m_nGridSize = PUSHGRAPH_MIN(usWidthAndHeight, 3, int);
}


// ===================================================================

int C2DPushGraph::GetMaxPeek() const
{
	return m_nMaxPeek;
}


// ===================================================================

void C2DPushGraph::SetMaxPeek( int nMax )
{
	m_nMaxPeek = nMax;
}


// ===================================================================

LPCTSTR C2DPushGraph::GetLabelForMax() const
{
	 return (LPCTSTR)m_strMaxLabel;
}


// ===================================================================

void C2DPushGraph::SetLabelForMax( LPCTSTR lpszLabel )
{
	 m_strMaxLabel = lpszLabel;
}


// ===================================================================

int C2DPushGraph::GetMinPeek() const
{
	return m_nMinPeek;
}


// ===================================================================

void C2DPushGraph::SetMinPeek( int nMin )
{	
	if (nMin < 0)
	{
		m_nPeekOffset = 0 - nMin;		
	}
	else
	{
		m_nPeekOffset = 0;
	}

	m_nMinPeek = nMin;
}


// ===================================================================

LPCTSTR C2DPushGraph::GetLabelForMin() const
{
	 return (LPCTSTR)m_strMinLabel;
}


// ===================================================================

void C2DPushGraph::SetLabelForMin( LPCTSTR lpszLabel )
{
	 m_strMinLabel = lpszLabel;
}


// ===================================================================

void C2DPushGraph::SetPeekRange( int nMin, int nMax )
{
	ASSERT(nMin < nMax);

	SetMinPeek(nMin);
	SetMaxPeek(nMax);
}


// ===================================================================

unsigned short C2DPushGraph::GetInterval() const
{
	return m_usLineInterval;
}


// ===================================================================

void C2DPushGraph::SetInterval( unsigned short usInterval )
{
	m_usLineInterval = usInterval;
	m_nMaxCoords = -1; // Forces reset
}


// ===================================================================

bool C2DPushGraph::AddLine( UINT uiLineID, COLORREF crColor )
{
	PushGraphLine *pNewLine = new PushGraphLine(uiLineID);

	if (!pNewLine)
	{
		ASSERT(pNewLine && "Out of memory");
		return false;
	}

	pNewLine->crLine = crColor;
	m_aLines.Add(pNewLine);

	return true;
}


// ===================================================================

void C2DPushGraph::RemoveLine( UINT uiLineID )
{
	for (int n = m_aLines.GetSize()-1; n >= 0; --n)
	{
		if (m_aLines[n]->uiID == uiLineID)
		{
			delete m_aLines[n];
			m_aLines.RemoveAt(n);

			--n; // Because we removed a line
		}
	}	
}


// ===================================================================

bool C2DPushGraph::SetLineColor( COLORREF crColor, UINT uiLineID )
{
	PushGraphLine *pLine = internal_LineFromID(uiLineID);
	
	if (pLine == NULL)
	{
		return false; 
	}
	
	pLine->crLine = crColor;
	return true;
}


// ===================================================================

COLORREF C2DPushGraph::GetLineColor( UINT uiLineID )
{
	PushGraphLine *pLine = internal_LineFromID(uiLineID);
	return (pLine) ? pLine->crLine : RGB(0, 0, 0);
}


// ===================================================================

bool C2DPushGraph::Push( double nMagnitude, UINT uiLineID )
{
	PushGraphLine *pLine = internal_LineFromID(uiLineID);
	
	if (pLine == NULL)
	{
		return false; 
	}

	
	/* Now add the magnitude (push point) to the array of push points, but
	first restrict it to the peek bounds */

	if (nMagnitude > m_nMaxPeek) {
		nMagnitude = m_nMaxPeek;
	}
	else if (nMagnitude < m_nMinPeek) {
		nMagnitude = m_nMinPeek;
	}
	
	nMagnitude -= m_nMinPeek;
	nMagnitude += m_nPeekOffset;
	
	pLine->aMagnitudes.Add(nMagnitude);
	return true;
}


// ===================================================================

void C2DPushGraph::ShowAsBar( UINT uiLineID, bool bAsBar )
{
	PushGraphLine *pLine = internal_LineFromID(uiLineID);
	
	if (pLine)
	{
		pLine->bShowAsBar = bAsBar;
	}
}


// ===================================================================

void C2DPushGraph::Update()
{
	int nGreatest = 0;  // Largest push point count
	
	
	for (int n=0; n < m_aLines.GetSize(); ++n)
	{
		if (nGreatest < m_aLines[n]->aMagnitudes.GetSize())
		{
			nGreatest = m_aLines[n]->aMagnitudes.GetSize();
		}
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -