Click here to Skip to main content
15,884,176 members
Articles / Programming Languages / C++

Customized Visual Studio .NET package: your fully integrated document window inside the IDE

Rate me:
Please Sign up or sign in to vote.
4.72/5 (20 votes)
20 Dec 200313 min read 63.7K   1.1K   39  
Create a fully integrated document window inside the Visual Studio IDE.
#include "StdAfx.h"
#include ".\histogram.h"
#include "CommandIDs.h"

CHistogram::CHistogramState::CHistogramState()
{
	ZeroMemory(m_adwData,sizeof(m_adwData));
	m_iSelIndx = 0;
	m_dwCmdState = 0;
}

void CHistogram::CHistogramState::SaveState( CHistogram* pHist )
{
	CopyMemory( m_adwData, pHist->m_adwData, sizeof(m_adwData) );
	m_iSelIndx = pHist->m_iSelIndx;
	m_dwCmdState = pHist->m_dwCmdState;
}

void CHistogram::CHistogramState::Swap( CHistogram* pHist )
{
	DWORD dwTmp;
	for( int i(0); i < CHistogram::m_ciMaxDataSize; i ++ )
	{
		dwTmp = pHist->m_adwData[i];
		pHist->m_adwData[i] = m_adwData[i];
		m_adwData[i] = dwTmp;
	}
	int iTmp;
	iTmp = m_iSelIndx;
	m_iSelIndx = pHist->m_iSelIndx;
	pHist->m_iSelIndx = iTmp;

	dwTmp = pHist->m_dwCmdState;
	pHist->m_dwCmdState = m_dwCmdState;
	m_dwCmdState = dwTmp;
}
/*
void CHistogram::CClipBoard::Load(CHistogram* pHist)
{
	 m_iDataIndx = pHist->m_iSelIndx;
	 m_dwData = pHist->m_adwData[m_iDataIndx];
}

void CHistogram::CClipBoard::Store(CHistogram* pHist)
{
	pHist->m_adwData[m_iDataIndx] = m_dwData;
	pHist->m_iSelIndx = m_iDataIndx;
}
*/

CHistogram::CHistogram(void):CGraphicObject(),CDataObject()
{
	ZeroMemory(m_adwData,sizeof(m_adwData));
	m_hbrBar = ::CreateSolidBrush(RGB(0,128,0));
	m_hbrBk = ::CreateSolidBrush(RGB(128,128,128));
	m_hbrSel = ::CreateSolidBrush(RGB(255,0,0));
	m_iSelIndx = 0;
	m_dwCmdState = CMD_FLAG_COPY|CMD_FLAG_CUT|CMD_FLAG_DELETE;
	m_pLastState = new CHistogram::CHistogramState();
	m_pCB = new CHistogram::CClipBoard();
}

CHistogram::~CHistogram(void)
{
	if ( m_hbrBar )
	{
		::DeleteObject(m_hbrBar);
		m_hbrBar = NULL;
	}
	if ( m_hbrBk )
	{
		::DeleteObject(m_hbrBk);
		m_hbrBk = NULL;
	}
	if ( m_hbrSel )
	{
		::DeleteObject(m_hbrBk);
		m_hbrSel = NULL;
	}
	if ( m_pLastState )
	{
		delete m_pLastState;
	}
	if ( m_pCB )
	{
		delete m_pCB;
	}
}

void CHistogram::Init( int iX, int iY, int iCX, int iCY )
{
	CGraphicObject::Init(iX,iY,iCX,iCY);
	if ( iCX > 0 )
	{
		UpdateDX();
	}
	if ( iCY > 10 )
	{
		UpdateYRatio();
	}
}

void CHistogram::Draw(HDC hDC)
{
	::DrawEdge( hDC, &m_rcBound, EDGE_SUNKEN, BF_RECT );
	DrawBk( hDC );
	DrawBars( hDC );
}

void	CHistogram::DrawBars(HDC hDC)
{
	RECT rc;
	rc.left = m_rcBound.left + m_iDX;
	rc.bottom = m_rcBound.bottom - 1;

	HGDIOBJ hbrOld = ::SelectObject(hDC,m_hbrBar);
	
	for( int i(0); i < m_ciMaxDataSize; i ++ )
	{
		rc.top = rc.bottom - int(m_adwData[i]*m_lfYRatio+0.5);
		rc.right = rc.left+m_iDX;
		if ( rc.top != rc.bottom )
		{
			::Rectangle( hDC, rc.left, rc.top, rc.right, rc.bottom );
		}
		rc.left += m_iDX*2;
	}

	::SelectObject(hDC,hbrOld);
}

void	CHistogram::DrawBk(HDC hDC)
{
	RECT rc;
	rc.left = m_rcBound.left + m_iDX + 3;
	rc.bottom = m_rcBound.bottom - 3;

	for( int i(0); i < m_ciMaxDataSize; i ++ )
	{
		rc.top = rc.bottom - int(m_adwData[i]*m_lfYRatio+0.5)-3;
		rc.right = rc.left + m_iDX;
		if ( rc.top == rc.bottom )
		{
			rc.top = rc.bottom - 3;
		}
		::FillRect( hDC, &rc, (i==m_iSelIndx ? m_hbrSel : m_hbrBk) );
		rc.left += m_iDX*2;
	}
}

void CHistogram::AddData(BYTE *ucValues, int iLength)
{
	for( int i(0); i < iLength; i ++ )
	{
		m_adwData[ucValues[i]/m_ciMaxDataSize]++;
	}
	UpdateYRatio();
}

void CHistogram::UpdateDX()
{
	m_iDX = (m_rcBound.right-m_rcBound.left)/(2*m_ciMaxDataSize+1);
}

void CHistogram::UpdateYRatio()
{
	DWORD dwMax = 0;
	for( int i(0); i < m_ciMaxDataSize; i ++ )
	{
		if ( m_adwData[i] > dwMax )
		{
			dwMax = m_adwData[i];
		}
	}
	m_lfYRatio = double(m_rcBound.bottom-m_rcBound.top-10)/double(dwMax>0?dwMax:1);
}

#pragma warning(disable:4311)
BOOL CHistogram::UserAction(UINT uMsg, LPVOID pParams)
{
	if ( uMsg & 0x80000000 ) // MMI Command
	{
		switch( ~uMsg )
		{
		case COMMAND_ID_COPY:
			m_pCB->Copy( this );
			CmdEnable( m_dwCmdState, CMD_FLAG_PASTE );
		case COMMAND_ID_SAVE:
		case COMMAND_ID_SAVEALL:
		default:
			return FALSE;

		case COMMAND_ID_CUT:
			m_pCB->Copy( this );
			m_pLastState->SaveState( this );
			CmdEnable( m_dwCmdState, CMD_FLAG_PASTE|CMD_FLAG_UNDO );
			m_adwData[m_iSelIndx] = 0;
			break;
		case COMMAND_ID_PASTE:
			m_pLastState->SaveState( this );
			m_pCB->Paste( this );
			CmdEnable( m_dwCmdState, CMD_FLAG_UNDO );
			break;
		case COMMAND_ID_DELETE:
			m_pLastState->SaveState( this );
			m_adwData[m_iSelIndx] = 0;
			CmdEnable( m_dwCmdState, CMD_FLAG_UNDO );
			break;
		case COMMAND_ID_UNDO:
			m_pLastState->Swap( this );
			CmdEnable( m_dwCmdState, CMD_FLAG_REDO );
			CmdDisable( m_dwCmdState, CMD_FLAG_UNDO );
			break;
		case COMMAND_ID_REDO:
			m_pLastState->Swap( this );
			CmdEnable( m_dwCmdState, CMD_FLAG_UNDO );
			CmdDisable(m_dwCmdState, CMD_FLAG_REDO );
			break;
		};
		m_bNotSaved = TRUE;
		return TRUE;
	}
	// windows messages
	if ( uMsg == WM_KEYDOWN )
	{
		if ( (UINT(pParams) == VK_LEFT) && (m_iSelIndx > 0) )
		{
			m_iSelIndx --;
			return TRUE;
		}
		if ( (UINT(pParams) == VK_RIGHT) && (m_iSelIndx < m_ciMaxDataSize-1) )
		{
			m_iSelIndx ++;
			return TRUE;
		}
	}
	return FALSE;
}
#pragma warning(default:4311)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
For the last 7 years I have developed software in real-time/embedded and MS-Windows environments for military and civil markets. I hold B.Sc. degree in computer engineering. Living in Ontario, Canada, I like hiking and traveling in general. Currently, I'm looking for employment opportunity inside the GTA.

Comments and Discussions