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

📄 grids.cpp

📁 一个二位式伪随机序列发生演示程序
💻 CPP
字号:
// Grids.cpp: implementation of the CGrids class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "serial.h"
#include "Grids.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CGrids::CGrids()
{
	m_LTTOrgPt.x = 0;
	m_LTTOrgPt.y = 0;
	m_MRGOrgPt.x = 0;
	m_MRGOrgPt.y = 0;

	m_nLine = 0;
	m_nCount = 0;
	m_uMargin = 0;
	m_uSpace = 0;
	m_uHWidth = 0;
	m_uVWidth = 0;
	m_uGRDNUM = 0;

	m_bIsSetValue = FALSE;
	m_bEnableClick = FALSE;
	for(int i = 0; i < MAX_GRL_NUM; i++)
		for(int j = 0; j < MAX_GRL_NUM; j++)
		{
			m_bIsGRDSelected[i][j] = FALSE;
		}
}

CGrids::~CGrids()
{

}

/* // parameter indication
*  // [in] UINT uMargin width of the lattice margin(place on the top and left)
*  // [in] UINT uSpace width or height(must be equal) of each lattice
*  // [in] UINT uWidth total width of the whole lattice
*/
BOOL CGrids::DrawGrid(CDC &dc, CPoint MrOrgPt, UINT uMargin, UINT uSpace,
					  UINT uLine, UINT uCount)
{
	CPen pen;
	CPen *pOldpen = NULL;

	//if(!m_bIsSetValue)
	//{
		m_bIsSetValue = TRUE;
		// save the margin value

		m_nLine = uLine;

		m_nCount = uCount;

		m_uMargin = uMargin;
		// save space of each lattice
		m_uSpace = uSpace;
		// save the total length of horizon
		m_uHWidth = uSpace * uCount;
		// save the total length of vertical
		m_uVWidth = uLine * uSpace;
		// save the grid's number(current max)
		m_uGRDNUM = uCount;
		// save the original margin point
		m_MRGOrgPt = MrOrgPt;
	//}

	// set the lattice(except margin) original point
	m_LTTOrgPt.x = m_MRGOrgPt.x + uMargin;
	m_LTTOrgPt.y = m_MRGOrgPt.y + uMargin;

	// draw the margin
	dc.MoveTo(m_MRGOrgPt.x,m_MRGOrgPt.y);
	dc.LineTo(m_MRGOrgPt.x,m_MRGOrgPt.y + uMargin + m_uVWidth); // draw vertical
	dc.MoveTo(m_MRGOrgPt.x,m_MRGOrgPt.y);
	dc.LineTo(m_MRGOrgPt.x + uMargin + m_uHWidth,m_MRGOrgPt.y); // draw horizon

	for(UINT x = 0; x <= m_uHWidth; x += uSpace) // draw vertical
	{
		dc.MoveTo(m_LTTOrgPt.x + x,m_LTTOrgPt.y - uMargin);
		dc.LineTo(m_LTTOrgPt.x + x,m_LTTOrgPt.y + m_uVWidth);
	}

	for(UINT y = 0; y <= m_uVWidth; y += uSpace)
	{
		dc.MoveTo(m_LTTOrgPt.x - uMargin,m_LTTOrgPt.y +y);
		dc.LineTo(m_LTTOrgPt.x + m_uHWidth,m_LTTOrgPt.y + y);
	}
	return TRUE;
}

/*	// parameter
*	// [in] UINT x left_coordinate indicate the original X_point of the margin
*	// [in] UINT y top_coordinate indicate the original Y_point of the margin
*   // [in] UINT uSpace width or height(must be equal) of each lattice
*   // [in] UINT uCount number of the lattice(vertical or horizon but both of them must be equal)
*/
BOOL CGrids::SetMarginText(CDC &dc)
{

	BOOL brResult = TRUE;
	if(m_uMargin <=0 || m_MRGOrgPt.x < 0 || m_MRGOrgPt.y < 0 \
		|| m_uSpace <= 0 || m_nCount <= 0)
		return FALSE;

	CFont fnt,*pOldfnt = NULL;

	brResult = fnt.CreatePointFont(m_uMargin*6, "Arial", &dc);
	pOldfnt = dc.SelectObject(&fnt);

	CString strText;
	CSize cs;

	dc.SetBkMode(TRANSPARENT);

	//for(UINT x = 1; x <= m_uCount; x++)
	for(UINT x = m_nCount; x >= 1; x--)
	{
		strText.Format("%X",m_nCount - x);

		if(x%2 == 0)
			dc.SetTextColor(RGB(255,0,0));
		else
			dc.SetTextColor(RGB(0,0,255));

		cs = dc.GetTextExtent(strText);
		
		dc.TextOut(m_MRGOrgPt.x + m_uMargin + (x-1)*m_uSpace + m_uSpace/2 - cs.cx/2,\
			m_MRGOrgPt.y + m_uMargin - cs.cy,strText);
	}

	for(int y = 1; y <= m_nLine; y++)
	//for(int y = m_uLine; y >= 1; y--)
	{
		strText.Format("%X",y-1);

		if(y%2 == 0)
			dc.SetTextColor(RGB(255,0,0));
		else
			dc.SetTextColor(RGB(0,0,255));
		cs = dc.GetTextExtent(strText);

		dc.TextOut(m_MRGOrgPt.x + m_uMargin/2 - cs.cx/2,\
			m_MRGOrgPt.y + m_uMargin + (y-1)*m_uSpace + m_uSpace/2 - cs.cy/2,strText);
	}

	dc.SelectObject(pOldfnt);
	return brResult;
}

/*
*	// [in] nH count of a grid in x coordinate
*	// [in] nV count of a grid in y coordinate
*	// [out] lpRect return rectangle
*/
BOOL CGrids::GetSelectRect(int nH, int nV, LPRECT lpRect)
{
	if(nH < 0 || nV < 0 || !m_nLine || !m_nCount)
		return FALSE;

	lpRect->left = m_LTTOrgPt.x + (m_nCount - 1 - nV) * m_uSpace + 1;
	lpRect->top = m_LTTOrgPt.y + nH * m_uSpace + 1;
	lpRect->right = lpRect->left + m_uSpace - 1;
	lpRect->bottom = lpRect->top + m_uSpace - 1;

	return TRUE;
}

BOOL CGrids::SetRectDisplayBkColor(CDC &dc,LPCRECT lpRect, COLORREF cf)
{
	CBrush bsh(cf);
	CBrush *pOldbsh = NULL;

	pOldbsh = dc.SelectObject(&bsh);
	dc.Rectangle(lpRect);
	dc.SelectObject(pOldbsh);

	return TRUE;
}

/* // parameter indication
*  // [in] CPoint MsPt point of mouse 
*  // [out] int *nH horizon order contained which the mouse click
*  // [out] int *nV vertical order contained which the mouse click
*	//  note: 
*/
BOOL CGrids::GetClickPosition(CPoint MsPt, UINT uHWidth, UINT uVWidth, int *nH, int *nV)
{
	int temp = 0;

	if(!m_bEnableClick)
		return FALSE;
	
	if((MsPt.x > m_LTTOrgPt.x) && (MsPt.x < m_LTTOrgPt.x + (int)m_uHWidth)
		&& (MsPt.y >m_LTTOrgPt.y) && (MsPt.y < m_LTTOrgPt.y + (int)m_uVWidth))
	{
		// set nH (horizon)
		//temp = (MsPt.x - m_LTTOrgPt.x) / m_uSpace + 1;
		temp = (m_LTTOrgPt.x + uHWidth - MsPt.x) / m_uSpace + 1;
		if(0 != temp)
		{
			*nH = temp - 1;
		}
		else
			*nH = -1; // out of the draw area
		
		// set nV (vertical)
		//temp = (MsPt.y - m_LTTOrgPt.y) / m_uSpace + 1;
		temp = (m_LTTOrgPt.y + uVWidth - MsPt.y) / m_uSpace + 1;
		if(0 != temp)
		{
			*nV = temp - 1;
		}
		else
			*nV = -1; // out of draw area
		
		if((*nH != -1) && (*nV != -1))
		{
			// if *nH and *nV in the draw area
			if(m_bIsGRDSelected[*nV][*nH])
				m_bIsGRDSelected[*nV][*nH] = FALSE;
			else
				m_bIsGRDSelected[*nV][*nH] = TRUE;
		}
	}
	else
	{
		*nH = -1;
		*nV = -1;
		return FALSE; // out of the draw rectangle
	}
	return TRUE;
}

BOOL CGrids::SetGridsText(CDC &dc,int uLine,int uCount,COLORREF cfSelected,COLORREF cfBkColor)
{
	BOOL brResult = TRUE;
	if(uLine <= 0 || uCount <= 0)
		return FALSE;

	CFont fnt,*pOldfnt = NULL;

	brResult = fnt.CreatePointFont(m_uMargin*6, "Arial", &dc);
	pOldfnt = dc.SelectObject(&fnt);

	CString strText;
	CSize cs;

	dc.SetBkMode(TRANSPARENT);
	//for(int y = 1; y <= uLine; y++)
	//	for(int x = 1; x <= uCount; x++)
	for(int y = 1; y <= uLine; y++) // set text int each box according to the status
		for(int x = uCount; x >= 1; x--)
		{
			//if(m_bIsGRDSelected[y-1][x-1])
			if(m_bIsGRDSelected[y-1][uCount - x])
			{
				strText = _T("1");
				dc.SetTextColor(cfSelected);
			}
			else
			{
				strText = _T("0");
				dc.SetTextColor(cfBkColor);
			}
			cs = dc.GetTextExtent(strText);
			dc.TextOut(m_LTTOrgPt.x + (x-1)*m_uSpace + m_uSpace/2 - cs.cx/2,
				m_LTTOrgPt.y + (y-1)*m_uSpace + m_uSpace/2 - cs.cy/2,strText);
		}
	dc.SelectObject(pOldfnt);
	return TRUE;
}

BOOL CGrids::FillGridColor(CDC &dc,COLORREF cfSelected, COLORREF cfBkcolor,
						   int nH, int nV, LPRECT lpRect)
{
	CBrush bshSelected,bshBkcolor,*pOldbsh;

	bshSelected.CreateSolidBrush(cfSelected); // create a brush
	bshBkcolor.CreateSolidBrush(cfBkcolor);

	CRect rect;

	// if the parameter nH or nV are -1
	if((-1 == nH) || (-1 == nV))
	{
		//for(UINT i = 0; i < m_uLine; i++)
			//for(UINT j = 0; j < m_uCount; j++)
		for(int i = 0; i < m_nLine; i++)
			for(int j = m_nCount - 1; j >= 0; j--)
			{
				// recalculate the rectangle for each grids
				rect.left = m_LTTOrgPt.x + j * m_uSpace + 1;
				rect.top = m_LTTOrgPt.y + i * m_uSpace + 1;
				rect.right = rect.left + m_uSpace - 1;
				rect.bottom = rect.top + m_uSpace - 1;
				//if(m_bIsGRDSelected[i][j])
				if(m_bIsGRDSelected[i][m_nCount - 1 - j])
				{
					pOldbsh = dc.SelectObject(&bshSelected);
					dc.FillRect(&rect,&bshSelected);
					dc.SelectObject(pOldbsh);
				}
				else
				{
					pOldbsh = dc.SelectObject(&bshBkcolor);
					dc.FillRect(&rect,&bshBkcolor);
					dc.SelectObject(pOldbsh);
				}
			}
	}
	else
	{
		if(nH < 0 || nV < 0)
			return FALSE; // invalidation parameters

		if(m_bIsGRDSelected[nH][nV])
		{
			pOldbsh = dc.SelectObject(&bshSelected);
			dc.FillRect(lpRect,&bshSelected);
			dc.SelectObject(pOldbsh);
		}
		else
		{
			pOldbsh = dc.SelectObject(&bshBkcolor);
			dc.FillRect(lpRect,&bshBkcolor);
			dc.SelectObject(pOldbsh);
		}
	}
	return FALSE;
}

void CGrids::EnableClick(BOOL en)
{
	m_bEnableClick = en;
}

BOOL CGrids::SetSelectBox(BOOL b,int nH,int nV)
{
	if(nH < 0 || nH > MAX_GRL_NUM || nV < 0 || nV > MAX_GRL_NUM)
		return FALSE;
	m_bIsGRDSelected[nH][nV] = b;
	return TRUE;
}

void CGrids::GetLTTWidth(UINT *upHWidth, UINT *upVWidth)
{
	*upHWidth = m_uHWidth;
	*upVWidth = m_uVWidth;
}

UINT CGrids::GridsStatusToUInt(int nLine)
{
	UINT temp = 0x00000000;
	for(int i = 0; i < MAX_GRL_NUM; i++)
	{
		if(m_bIsGRDSelected[nLine][i])
			temp = temp | (0x00000001 << i);
	}
	return temp;
}

BOOL CGrids::SetSelectBoxByUInt(int nLine, UINT nValue)
{
	UINT temp = 0;
	if(nLine > m_nLine)
		return FALSE;
	for(int i = 0; i < 8*sizeof(int); i++)
	{
		temp = (nValue >> i) & 0x00000001;
		m_bIsGRDSelected[nLine][i] = (BOOL)temp;
	}
	return TRUE;
}

BOOL CGrids::SetGridText(CDC &dc, int nH, int nV, COLORREF cfSelected, COLORREF cfBkcolor)
{
	if(nH < 0 || nH >= m_nLine || nV < 0 || nV >= m_nCount)
		return FALSE;

	CString strText = _T("");
	//if(m_bIsGRDSelected[y-1][x-1])
	if(m_bIsGRDSelected[nH][nV])
	{
		strText = _T("1");
		dc.SetTextColor(cfSelected);
	}
	else
	{
		strText = _T("0");
		dc.SetTextColor(cfBkcolor);
	}
	CSize cs = dc.GetTextExtent(strText);
	dc.TextOut(m_LTTOrgPt.x + (m_nCount - 1 - nV)*m_uSpace + m_uSpace/2 - cs.cx/2,
		m_LTTOrgPt.y + nH*m_uSpace + m_uSpace/2 - cs.cy/2,strText);

		return TRUE;
}

⌨️ 快捷键说明

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