www.pudn.com > LotterySrc > GraphStatic.cpp, change:2000-12-28,size:8467b


// GraphStatic.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "lottery.h" 
#include "GraphStatic.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CGraphStatic 
 
CGraphStatic::CGraphStatic() 
{ 
	m_nCurY=0; 
	m_nCurX=0; 
	m_nZoomFactorX=2; 
	m_nZoomFactorY=2; 
	m_nShowMode=1; 
} 
 
CGraphStatic::~CGraphStatic() 
{ 
} 
 
 
BEGIN_MESSAGE_MAP(CGraphStatic, CStatic) 
	//{{AFX_MSG_MAP(CGraphStatic) 
	ON_WM_PAINT() 
	ON_WM_VSCROLL() 
	ON_WM_HSCROLL() 
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd) 
	ON_BN_CLICKED(IDC_BUTTON_SUBSTRACT, OnButtonSubtract) 
	ON_BN_CLICKED(IDC_BUTTON_SHOW_MODE_SWITCH, OnButtonShowModeSwitch) 
	ON_MESSAGE(WM_NOTIFY_GRAPH_ACTION, OnNotifyGraphAction) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CGraphStatic message handlers 
 
void CGraphStatic::OnPaint()  
{ 
	CPaintDC dc(this); // device context for painting 
	CRect rect; 
	GetClientRect(&rect); 
	CBrush bkBrush(RGB(64,64,64)); 
	CPen penLine(PS_SOLID,1,RGB(0,255,0)),*pOldPen; 
	dc.FillRect(&rect,&bkBrush); 
	dc.SetBkColor(RGB(64,64,64)); 
	pOldPen=(CPen*)dc.SelectObject(&penLine); 
	CFont font,*pOldFont; 
	font.CreatePointFont(90,""); 
	pOldFont=(CFont *)dc.SelectObject(&font); 
	GRAPH graph; 
	if (m_nShowMode==0) 
	{ 
		for (int i=m_nCurX; i<m_array.GetSize(); i++) 
		{ 
			graph=m_array.GetAt(i); 
			int tempx=(i-m_nCurX)*m_nZoomFactorX+2; 
			int tempy=(rect.bottom-m_nZoomFactorY*graph.s[m_nCurY])-1; 
			if (tempx < rect.right && tempy > 0) 
			{ 
				dc.MoveTo(tempx,rect.bottom-1); 
				dc.LineTo(tempx,tempy); 
			} 
		} 
		dc.SetTextColor(RGB(255,255,0)); 
		CString str; 
		graph=m_array.GetAt(0); 
		str.Format("p=%2d n=%2d m=%2d Mx=%2d Mn=%2d Av=%5.2f",graph.s[m_nCurY],m_nWidth,m_nCurY,m_nMax[m_nCurY],m_nMin[m_nCurY],m_nAve[m_nCurY]); 
		CRect rectText=rect; 
		rectText.DeflateRect(0,0,30,rect.Height()-15); 
		dc.DrawText(str,&rectText,DT_CENTER); 
	} 
	else 
	{ 
		int count=(rect.Width()) / (m_nWidth + 1) / 5 + m_nCurX; 
		if (count > m_array.GetSize()) count=m_array.GetSize(); 
		for (int i=m_nCurX; i<count-1; i++) 
		{ 
			graph=m_array.GetAt(i); 
			for (int j=0; j<m_nWidth+1; j++) 
			{ 
				CBrush rectBrush(RGB(j*50,255-j*50,0)); 
				CRect rect1; 
				rect1.bottom=rect.bottom-1; 
				rect1.top=rect1.bottom-graph.s[j]*2; 
				rect1.left=(i-m_nCurX+1)*5*(m_nWidth+1) + j*5 + 2; 
				rect1.right=rect1.left + 4; 
				dc.FillRect(&rect1,&rectBrush); 
				rectBrush.DeleteObject(); 
			} 
		} 
		CBrush rectBrush(RGB(255,255,0)); 
		for (int j=0; j<m_nWidth+1; j++) 
		{ 
			CRect rect1; 
			rect1.bottom=rect.bottom-1; 
			rect1.top=rect1.bottom-m_proGraph.s[j]*2; 
			rect1.left=j*5 + 2; 
			rect1.right=rect1.left + 4; 
			dc.FillRect(&rect1,&rectBrush); 
		} 
		rectBrush.DeleteObject(); 
 
		dc.SetTextColor(RGB(255,255,0)); 
		CString str="cur=\t",strTemp; 
		for (i=0; i<=m_nWidth; i++) 
		{ 
			strTemp.Format("%2d\t",m_proGraph.s[i]); 
			str+=strTemp; 
		} 
		CRect rectText=rect; 
		rectText.DeflateRect(0,10,30,rect.Height()-20); 
		m_rectTopText=rectText; 
		dc.DrawText(str,&rectText,DT_LEFT|DT_EXPANDTABS); 
		str="ave=\t"; 
		for (i=0; i<=m_nWidth; i++) 
		{ 
			strTemp.Format("%3.1f\t",m_nAve[i]); 
			str+=strTemp; 
		} 
		rectText=rect; 
		rectText.DeflateRect(0,30,30,rect.Height()-40); 
		dc.DrawText(str,&rectText,DT_LEFT|DT_EXPANDTABS); 
		str="min=\t"; 
		for (i=0; i<=m_nWidth; i++) 
		{ 
			strTemp.Format("%2d\t",m_nMin[i]); 
			str+=strTemp; 
		} 
		rectText=rect; 
		rectText.DeflateRect(0,20,30,rect.Height()-30); 
		dc.DrawText(str,&rectText,DT_LEFT|DT_EXPANDTABS); 
		str="max=\t"; 
		for (i=0; i<=m_nWidth; i++) 
		{ 
			strTemp.Format("%2d\t",m_nMax[i]); 
			str+=strTemp; 
		} 
		rectText=rect; 
		rectText.DeflateRect(0,0,30,rect.Height()-10); 
		dc.DrawText(str,&rectText,DT_LEFT|DT_EXPANDTABS); 
		CFont font; 
		font.CreatePointFont(150,"Arial"); 
		dc.SelectObject(&font); 
		dc.SetTextColor(RGB(200,0,255)); 
		rectText=rect; 
		rectText.DeflateRect(10,40,130,rect.Height()-60); 
		str.Format("%d",m_nWidth); 
		dc.DrawText(str,&rectText,DT_LEFT); 
	} 
	dc.SelectObject(pOldPen); 
	dc.SelectObject(pOldFont); 
	penLine.DeleteObject(); 
	bkBrush.DeleteObject(); 
	font.DeleteObject(); 
} 
void CGraphStatic::OnButtonAdd() 
{ 
	CRect rect; 
	GetClientRect(&rect); 
	{ 
		m_nZoomFactorY++; 
		m_nZoomFactorX++; 
		NotifyGraphAction(); 
		Invalidate(); 
	} 
} 
void CGraphStatic::OnButtonSubtract() 
{ 
	if (m_nZoomFactorY > 1) 
	{ 
		m_nZoomFactorY--; 
		m_nZoomFactorX--; 
		NotifyGraphAction(); 
		Invalidate(); 
	} 
} 
void CGraphStatic::OnButtonShowModeSwitch() 
{ 
	m_btnAdd.EnableWindow(m_nShowMode); 
	m_btnSubtract.EnableWindow(m_nShowMode); 
	m_nShowMode= 1-m_nShowMode; 
	Invalidate(); 
} 
void CGraphStatic::PreSubclassWindow()  
{ 
	CStatic::PreSubclassWindow(); 
	CRect rect; 
	GetClientRect(rect); 
	CRect rect1=rect,rect2=rect,rect3=rect,rect4=rect,rect5=rect; 
	rect1.DeflateRect(rect.Width()-30,rect.Height()-20,0,0); 
	rect2.DeflateRect(rect.Width()-17,rect.Height()-50,0,20); 
	rect3.DeflateRect(rect.Width()-15,0,0,rect.Height()-17); 
	rect4.DeflateRect(rect.Width()-30,0,15,rect.Height()-17); 
	rect5.DeflateRect(rect.Width()-45,0,30,rect.Height()-17); 
	m_HScrollBar.Create(SBS_HORZ|WS_VISIBLE|WS_CHILD|WS_BORDER,rect1,this,10000); 
	m_VScrollBar.Create(SBS_VERT|WS_VISIBLE|WS_CHILD|WS_BORDER,rect2,this,10001); 
	m_btnAdd.Create("+",WS_VISIBLE|WS_CHILD,rect4,this,IDC_BUTTON_ADD); 
	m_btnSubtract.Create("-",WS_VISIBLE|WS_CHILD,rect5,this,IDC_BUTTON_SUBSTRACT); 
	m_btnShowModeSwitch.Create(".",WS_VISIBLE|WS_CHILD,rect3,this,IDC_BUTTON_SHOW_MODE_SWITCH); 
	m_btnAdd.EnableWindow(FALSE); 
	m_btnSubtract.EnableWindow(FALSE); 
} 
void CGraphStatic::FillArray(CArray<LOTTERY,LOTTERY> *pArray,int nWidth) 
{ 
	m_nWidth=nWidth; 
	int count=pArray->GetSize(); 
	LOTTERY lty; 
	FREQUENCY feq; 
	GRAPH graph; 
	for (int i=0; i<count-m_nWidth+1; i++) 
	{ 
		ZeroMemory(&feq,sizeof(FREQUENCY)); 
		for (int j=i; j<i+m_nWidth; j++) 
		{ 
			lty=pArray->GetAt(j); 
			for (int k=1; k<9; k++) 
				feq.f[lty.i[k]-1]++; 
		} 
		if (i==0) 
		{ 
			ZeroMemory(&m_freq,sizeof(FREQUENCY)); 
			for (int m=0; m<m_nWidth-1; m++) 
			{ 
				lty=pArray->GetAt(m); 
				for (int k=1; k<9; k++) 
					m_freq.f[lty.i[k]-1]++; 
			} 
		} 
		ZeroMemory(&graph,sizeof(GRAPH)); 
		for (int k=0; k<30; k++) 
		{ 
			graph.s[feq.f[k]]++; 
		} 
		m_array.Add(graph); 
	} 
	count=m_array.GetSize(); 
	for (i=0; i<MAX_SEQUENCY_NUMBER; i++) 
	{ 
		int nSum=0; 
		int nMax=0; 
		int nMin=INT_MAX; 
		for (int j=0; j<count; j++) 
		{ 
			graph=m_array.GetAt(j); 
			if (graph.s[i] > nMax) nMax=graph.s[i]; 
			if (graph.s[i] < nMin) nMin=graph.s[i]; 
			nSum+=graph.s[i]; 
		} 
		m_nMax[i]=nMax; 
		m_nMin[i]=nMin; 
		m_nAve[i]=(double)nSum/(count); 
	} 
	m_proGraph=m_array.GetAt(0);; 
	CRect rect; 
	GetClientRect(rect); 
	m_rectProGraph=rect; 
	m_rectProGraph.right=(m_nWidth + 1)*5+2; 
} 
 
void CGraphStatic::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)  
{ 
	if (nSBCode==SB_LINEUP && m_nCurY > 0) 
	{ 
		m_nCurY--; 
		NotifyGraphAction(); 
		Invalidate(); 
	} 
	if (nSBCode==SB_LINEDOWN && m_nCurY < m_nWidth) 
	{ 
		m_nCurY++;	 
		NotifyGraphAction(); 
		Invalidate(); 
	} 
} 
 
void CGraphStatic::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)  
{ 
	if (nSBCode==SB_LINEUP && m_nCurX > 0) 
	{ 
		m_nCurX--; 
		NotifyGraphAction(); 
		Invalidate(); 
	} 
	if (nSBCode==SB_LINEDOWN && m_nCurX < m_array.GetSize()-1) 
	{ 
		m_nCurX++;	 
		NotifyGraphAction(); 
		Invalidate(); 
	} 
} 
 
void CGraphStatic::NotifyGraphAction() 
{ 
	WPARAM wp=MAKEWPARAM(m_nCurX,m_nCurY); 
	LPARAM lp=MAKELPARAM(m_nZoomFactorX,m_nZoomFactorY); 
	::PostMessage(GetParent()->m_hWnd,WM_NOTIFY_GRAPH_ACTION,wp,lp); 
} 
void CGraphStatic::OnNotifyGraphAction(WPARAM wp,LPARAM lp) 
{ 
	m_nCurX=LOWORD(wp); 
	if (HIWORD(wp) <= m_nWidth)m_nCurY=HIWORD(wp); 
	m_nZoomFactorX=LOWORD(lp); 
	m_nZoomFactorY=HIWORD(lp); 
	Invalidate(); 
} 
 
void CGraphStatic::InvalidateProGraph(LOTTERY *plot) 
{ 
	FREQUENCY freq=m_freq; 
	for (int i=1; i<8; i++) 
	{ 
		freq.f[plot->i[i]-1]++; 
	} 
	GRAPH graph; 
	ZeroMemory(&graph, sizeof(GRAPH)); 
	for (i=0; i<30; i++) 
	{ 
		graph.s[freq.f[i]]++; 
	} 
	m_proGraph=graph; 
	InvalidateRect(&m_rectProGraph); 
	InvalidateRect(&m_rectTopText); 
}