www.pudn.com > LotterySrc > CurveView.cpp, change:2000-12-08,size:14457b


// CurveView.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "lottery.h" 
#include "CurveView.h" 
#include "ChildView.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CCurveView dialog 
extern CChildView *pCV; 
 
CCurveView::CCurveView(CWnd* pParent /*=NULL*/) 
	: CDialog(CCurveView::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CCurveView) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
} 
 
 
void CCurveView::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CCurveView) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(CCurveView, CDialog) 
	//{{AFX_MSG_MAP(CCurveView) 
	ON_WM_PAINT() 
	ON_WM_HSCROLL() 
	ON_WM_VSCROLL() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CCurveView message handlers 
 
void CCurveView::OnPaint()  
{ 
	int w=pCV->m_array.GetSize(); 
	CPaintDC dc(this); // device context for painting 
	if (!m_bGetRect) 
	{ 
		int xWidth=30; 
		int yWidth=20; 
		GetClientRect(&m_Rect); 
		m_leftRect=m_Rect; 
		m_leftRect.DeflateRect(0,0,m_Rect.right-xWidth,0); 
		m_rightRect=m_Rect; 
		m_rightRect.DeflateRect(m_Rect.right-xWidth,0,0,0); 
		m_topRect=m_Rect; 
		m_topRect.DeflateRect(xWidth,0,xWidth,m_Rect.bottom-yWidth); 
		m_bottomRect=m_Rect; 
		m_bottomRect.DeflateRect(xWidth,m_Rect.bottom-yWidth,xWidth,0); 
		m_centerRect=m_Rect; 
		m_centerRect.DeflateRect(xWidth,yWidth,xWidth,yWidth); 
		m_bGetRect=TRUE; 
	} 
	//设置坐标及投影方式 
	CRect rect=m_Rect; 
	dc.SetMapMode(MM_HIMETRIC); 
	dc.SetViewportOrg(rect.left,rect.bottom); 
	dc.DPtoLP(&rect); 
 
	//填充背景 
	dc.SetBkColor(RGB(64,64,64)); 
	CBrush bkBrush(RGB(64,64,64)); 
	dc.FillRect(&rect,&bkBrush); 
	 
	//设置画笔风格和文字颜色 
	dc.SetTextColor(RGB(255,0,0)); 
	CPen penCurve(PS_SOLID,1,RGB(0,255,0)); 
 	CPen penCord(PS_SOLID,1,RGB(255,255,255)), *pOldPen; 
	CPen bkPen(PS_DOT,1,RGB(100,100,0)); 
 
	pOldPen=dc.SelectObject(&penCord); 
	//画左部和右部号码,画横线 
	int nPosV=GetScrollPos(SB_VERT); 
	int nEndV=((nPosV+m_iVLine) > 31)?(31):(nPosV+m_iVLine); 
	CRect leftRect=m_leftRect; 
	CRect rightRect=m_rightRect; 
	CRect centerRect=m_centerRect; 
	CRect bottomRect=m_bottomRect; 
	dc.DPtoLP(&leftRect); 
	dc.DPtoLP(&rightRect); 
	dc.DPtoLP(¢erRect); 
	dc.DPtoLP(&bottomRect); 
	m_iHeightInterval=-centerRect.Height()/m_iVLine; 
	m_iWidthInterval=centerRect.Width()/m_iHLine; 
	m_iHeightFactor=m_iHeightInterval/pCV->m_iWidth; 
 
	char numBuf[5]; 
	for (int i=nPosV; i<nEndV; i++) 
	{ 
		itoa(i,numBuf,10); 
		dc.TextOut(leftRect.left+100, m_iHeightInterval*(i-nPosV+1)+bottomRect.top,numBuf,strlen(numBuf)); 
		dc.TextOut(rightRect.left+200, m_iHeightInterval*(i-nPosV+1)+bottomRect.top,numBuf,strlen(numBuf)); 
		dc.MoveTo(leftRect.right,m_iHeightInterval*(i-nPosV+1)-300+bottomRect.top); 
		dc.LineTo(rightRect.left,m_iHeightInterval*(i-nPosV+1)-300+bottomRect.top); 
	} 
	int nPosH=GetScrollPos(SB_HORZ); 
	//画底部期号 
	for (i=nPosH; i<m_iHLine+nPosH; i+=5) 
	{ 
		ZeroMemory(numBuf,5); 
		itoa(i,numBuf,10); 
		dc.TextOut(bottomRect.left+m_iWidthInterval*(i-nPosH),bottomRect.top+100,numBuf,strlen(numBuf)); 
	} 
	//画暗方格 
	{ 
		dc.SelectObject(&bkPen); 
		//画横线 
		//画竖线 
		for (i=nPosH; i<m_iHLine+nPosH; i++) 
		{ 
			dc.MoveTo(bottomRect.left+m_iWidthInterval*(i-nPosH),bottomRect.top+200); 
			dc.LineTo(bottomRect.left+m_iWidthInterval*(i-nPosH),rect.top); 
		} 
	} 
	//画走势图 
	int iEndH=((nPosH+m_iHLine) > (w - (int)pCV->m_iWidth))? (w - pCV->m_iWidth):(nPosH+m_iHLine); 
	dc.SelectObject(&penCurve); 
	for (i=nPosV; i<nEndV; i++) 
	{ 
		for (int j=nPosH+1; j<=iEndH+1; j++) 
		{ 
			if (j==iEndH+1 && j==w-(int)pCV->m_iWidth+1)  
			{ 
				CPen upPen(PS_SOLID,1,RGB(255,0,0)); 
				CPen downPen(PS_SOLID,1,RGB(255,255,0)); 
				CPen *old_pen; 
				old_pen=(CPen *)dc.SelectObject(&downPen); 
				int y = m_iHeightInterval*(i-nPosV+1)-200 + m_iHeightFactor*(int)(pCV->m_pFreq[j-1].f[i-1]) + bottomRect.top; 
				dc.MoveTo(leftRect.right + m_iWidthInterval * (j-nPosH-1),y); 
				y = m_iHeightInterval*(i-nPosV+1)-200 + m_iHeightFactor*(int)(pCV->m_pFreq[w].f[i-1]) + bottomRect.top; 
				dc.LineTo(leftRect.right + m_iWidthInterval * (j-nPosH),y); 
				dc.SelectObject(&upPen); 
				y = m_iHeightInterval*(i-nPosV+1)-200 + m_iHeightFactor*(int)(pCV->m_pFreq[j-1].f[i-1]) + bottomRect.top; 
				dc.MoveTo(leftRect.right + m_iWidthInterval * (j-nPosH-1),y); 
				y = m_iHeightInterval*(i-nPosV+1)-200 + m_iHeightFactor*(int)((pCV->m_pFreq[w].f[i-1])+1) + bottomRect.top; 
				dc.LineTo(leftRect.right + m_iWidthInterval * (j-nPosH),y); 
				dc.SelectObject(old_pen); 
				break; 
			} 
			int y = m_iHeightInterval*(i-nPosV+1)-200 + m_iHeightFactor*(int)(pCV->m_pFreq[j-1].f[i-1]) + bottomRect.top; 
			dc.MoveTo(leftRect.right + m_iWidthInterval * (j-nPosH-1),y); 
			y = m_iHeightInterval*(i-nPosV+1)-200 + m_iHeightFactor*(int)(pCV->m_pFreq[j].f[i-1]) + bottomRect.top; 
			dc.LineTo(leftRect.right + m_iWidthInterval * (j-nPosH),y); 
		} 
	} 
	 
	dc.SelectObject(pOldPen); 
 
	bkBrush.DeleteObject(); 
	penCurve.DeleteObject(); 
	penCord.DeleteObject(); 
	bkPen.DeleteObject(); 
} 
 
BOOL CCurveView::OnInitDialog()  
{ 
	CDialog::OnInitDialog(); 
	 
	int w=pCV->m_array.GetSize(); 
 
	m_VScrollInfo.cbSize=sizeof(SCROLLINFO); 
	m_VScrollInfo.fMask=SIF_PAGE|SIF_RANGE|SIF_POS; 
	m_VScrollInfo.nMin=1; 
	m_VScrollInfo.nMax=30; 
	m_VScrollInfo.nPage=3; 
	m_VScrollInfo.nPos=0; 
 
	m_HScrollInfo.cbSize=sizeof(SCROLLINFO); 
	m_HScrollInfo.fMask=SIF_PAGE|SIF_RANGE|SIF_POS; 
	m_HScrollInfo.nMin=0; 
	m_HScrollInfo.nMax=w-1; 
	m_HScrollInfo.nPage=(w-1)/10; 
	m_HScrollInfo.nPos=0; 
 
	SetScrollInfo(SB_VERT,&m_VScrollInfo); 
	SetScrollInfo(SB_HORZ,&m_HScrollInfo); 
 
	m_iHeightInterval=2000; 
	m_iHeightFactor=400; 
	m_iHLine=100; 
	m_iVLine=9; 
	m_bGetRect=FALSE; 
	m_Rect.SetRectEmpty(); 
	return TRUE;  // return TRUE unless you set the focus to a control 
	              // EXCEPTION: OCX Property Pages should return FALSE 
} 
//-------------响应水平滚动条消息-----------------// 
void CCurveView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)  
{ 
	// TODO: Add your message handler code here and/or call default 
	SCROLLINFO si; 
	si.cbSize=sizeof(si); 
	GetScrollInfo(SB_HORZ,&si); 
 
	int nOldPos=GetScrollPos(SB_HORZ); 
	switch (nSBCode) 
	{ 
	case SB_BOTTOM: 
		nPos=si.nMax; 
		break; 
	case SB_ENDSCROLL: 
		nPos=GetScrollPos(SB_HORZ); 
		break; 
	case SB_LINEDOWN: 
		nPos=GetScrollPos(SB_HORZ); 
		if ((int)nPos < si.nMax) 
			nPos++; 
		break; 
	case SB_LINEUP: 
		nPos=GetScrollPos(SB_HORZ); 
		if ((int)nPos > si.nMin) 
			nPos--; 
		break; 
	case SB_PAGEDOWN: 
		nPos=GetScrollPos(SB_HORZ); 
		nPos+=si.nPage; 
		if ((int)nPos > si.nMax) 
			nPos=si.nMax; 
		break; 
	case SB_PAGEUP: 
		nPos=GetScrollPos(SB_HORZ); 
		nPos-=si.nPage; 
		if ((int)nPos < si.nMin) 
			nPos=si.nMin; 
		break; 
	case SB_THUMBPOSITION: 
		break; 
	case SB_THUMBTRACK: 
		break; 
	case SB_TOP: 
		nPos=si.nMin; 
		break; 
	} 
	if (nOldPos != (int)nPos) 
	{ 
		EraseCenterRect(); 
		SetScrollPos(SB_HORZ,nPos); 
		ReDrawCenterRect(); 
		InvalidateRect(&m_bottomRect); 
		InvalidateRect(&m_topRect); 
	} 
} 
//-------------响应竖直滚动条消息-----------------// 
void CCurveView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)  
{ 
	SCROLLINFO si; 
	si.cbSize=sizeof(si); 
	GetScrollInfo(SB_VERT,&si); 
 
	int nOldPos=GetScrollPos(SB_VERT); 
 
	switch (nSBCode) 
	{ 
	case SB_BOTTOM: 
		nPos=si.nMax; 
		break; 
	case SB_ENDSCROLL: 
		nPos=GetScrollPos(SB_VERT); 
		break; 
	case SB_LINEDOWN: 
		nPos=GetScrollPos(SB_VERT); 
		if ((int)nPos < si.nMax) 
			nPos++; 
		break; 
	case SB_LINEUP: 
		nPos=GetScrollPos(SB_VERT); 
		if ((int)nPos > si.nMin) 
			nPos--; 
		break; 
	case SB_PAGEDOWN: 
		nPos=GetScrollPos(SB_VERT); 
		nPos+=si.nPage; 
		if ((int)nPos > si.nMax) 
			nPos=si.nMax; 
		break; 
	case SB_PAGEUP: 
		nPos=GetScrollPos(SB_VERT); 
		nPos-=si.nPage; 
		if ((int)nPos < si.nMin) 
			nPos=si.nMin; 
		break; 
	case SB_THUMBPOSITION: 
		break; 
	case SB_THUMBTRACK: 
		break; 
	case SB_TOP: 
		nPos=si.nMin; 
		break; 
	} 
	if (nOldPos != (int)nPos) 
	{ 
		EraseCenterRect(); 
		SetScrollPos(SB_VERT,nPos); 
		ReDrawCenterRect(); 
 
		InvalidateRect(&m_topRect); 
		InvalidateRect(&m_rightRect); 
		InvalidateRect(&m_leftRect); 
	} 
} 
 
//----------------删除中心区域----------------// 
void CCurveView::EraseCenterRect() 
{ 
	int w=pCV->m_array.GetSize(); 
	CDC *pDC=GetDC(); 
	//设置坐标及投影方式 
	CRect rect=m_Rect; 
	pDC->SetMapMode(MM_HIMETRIC); 
	pDC->SetViewportOrg(rect.left,rect.bottom); 
	pDC->DPtoLP(&rect); 
 
	//填充背景 
	pDC->SetBkColor(RGB(64,64,64)); 
	 
	//设置画笔风格和文字颜色 
 	CPen penErase(PS_SOLID,1,RGB(64,64,64)),*pOldPen; 
	pOldPen=pDC->SelectObject(&penErase); 
	 
	int nPosV=GetScrollPos(SB_VERT); 
	int nEndV=((nPosV+m_iVLine) > 31)?(31):(nPosV+m_iVLine); 
	CRect leftRect=m_leftRect; 
	CRect rightRect=m_rightRect; 
	CRect centerRect=m_centerRect; 
	CRect bottomRect=m_bottomRect; 
	pDC->DPtoLP(&bottomRect); 
	pDC->DPtoLP(&leftRect); 
	pDC->DPtoLP(&rightRect); 
	pDC->DPtoLP(¢erRect); 
	m_iHeightInterval=-centerRect.Height()/m_iVLine; 
	m_iWidthInterval=centerRect.Width()/m_iHLine; 
	m_iHeightFactor=m_iHeightInterval/pCV->m_iWidth; 
 
	int nPosH=GetScrollPos(SB_HORZ); 
	 
	int iEndH=((nPosH+m_iHLine) > (w - (int)pCV->m_iWidth))? (w - pCV->m_iWidth):(nPosH+m_iHLine); 
	for (int i=nPosV; i<nEndV; i++) 
	{ 
		for (int j=nPosH+1; j<=iEndH+1; j++) 
		{ 
			if (j==iEndH+1 && j==w-(int)pCV->m_iWidth+1)  
			{ 
				int y = m_iHeightInterval*(i-nPosV+1)-200 + m_iHeightFactor*(int)(pCV->m_pFreq[j-1].f[i-1]) + bottomRect.top; 
				pDC->MoveTo(leftRect.right + m_iWidthInterval * (j-nPosH-1),y); 
				y = m_iHeightInterval*(i-nPosV+1)-200 + m_iHeightFactor*(int)(pCV->m_pFreq[w].f[i-1]) + bottomRect.top; 
				pDC->LineTo(leftRect.right + m_iWidthInterval * (j-nPosH),y); 
				y = m_iHeightInterval*(i-nPosV+1)-200 + m_iHeightFactor*(int)(pCV->m_pFreq[j-1].f[i-1]) + bottomRect.top; 
				pDC->MoveTo(leftRect.right + m_iWidthInterval * (j-nPosH-1),y); 
				y = m_iHeightInterval*(i-nPosV+1)-200 + m_iHeightFactor*(int)((pCV->m_pFreq[w].f[i-1])+1) + bottomRect.top; 
				pDC->LineTo(leftRect.right + m_iWidthInterval * (j-nPosH),y); 
				break; 
			} 
			int y = m_iHeightInterval*(i-nPosV+1)-200 + m_iHeightFactor*(int)(pCV->m_pFreq[j-1].f[i-1]) + bottomRect.top; 
			pDC->MoveTo(leftRect.right + m_iWidthInterval * (j-nPosH-1),y); 
			y = m_iHeightInterval*(i-nPosV+1)-200 + m_iHeightFactor*(int)(pCV->m_pFreq[j].f[i-1]) + bottomRect.top; 
			pDC->LineTo(leftRect.right + m_iWidthInterval * (j-nPosH),y); 
		} 
	} 
	 
	pDC->SelectObject(pOldPen); 
	penErase.DeleteObject(); 
} 
//-----------重画中心区域----------------// 
void CCurveView::ReDrawCenterRect() 
{ 
	int w=pCV->m_array.GetSize(); 
	CDC *pDC=GetDC(); 
	//设置坐标及投影方式 
	CRect rect=m_Rect; 
	pDC->SetMapMode(MM_HIMETRIC); 
	pDC->SetViewportOrg(rect.left,rect.bottom); 
	pDC->DPtoLP(&rect); 
 
	//填充背景 
	pDC->SetBkColor(RGB(64,64,64)); 
	 
	//设置画笔风格和文字颜色 
	CPen penCurve(PS_SOLID,1,RGB(0,255,0)),*pOldPen; 
	pOldPen=pDC->SelectObject(&penCurve); 
	 
	int nPosV=GetScrollPos(SB_VERT); 
	int nEndV=((nPosV+m_iVLine) > 31)?(31):(nPosV+m_iVLine); 
	CRect leftRect=m_leftRect; 
	CRect rightRect=m_rightRect; 
	CRect centerRect=m_centerRect; 
	CRect bottomRect=m_bottomRect; 
	pDC->DPtoLP(&bottomRect); 
	pDC->DPtoLP(&leftRect); 
	pDC->DPtoLP(&rightRect); 
	pDC->DPtoLP(¢erRect); 
 
	int nPosH=GetScrollPos(SB_HORZ); 
	 
	int iEndH=((nPosH+m_iHLine) > (w - (int)pCV->m_iWidth))? (w - pCV->m_iWidth):(nPosH+m_iHLine); 
	for (int i=nPosV; i<nEndV; i++) 
	{ 
		for (int j=nPosH+1; j<=iEndH+1; j++) 
		{ 
			if (j==iEndH+1 && j==w-(int)pCV->m_iWidth+1)  
			{ 
				CPen upPen(PS_SOLID,1,RGB(255,0,0)); 
				CPen downPen(PS_SOLID,1,RGB(255,255,0)); 
				CPen *old_pen; 
				old_pen=(CPen *)pDC->SelectObject(&downPen); 
				int y = m_iHeightInterval*(i-nPosV+1)-200 + m_iHeightFactor*(int)(pCV->m_pFreq[j-1].f[i-1]) + bottomRect.top; 
				pDC->MoveTo(leftRect.right + m_iWidthInterval * (j-nPosH-1),y); 
				y = m_iHeightInterval*(i-nPosV+1)-200 + m_iHeightFactor*(int)(pCV->m_pFreq[w].f[i-1]) + bottomRect.top; 
				pDC->LineTo(leftRect.right + m_iWidthInterval * (j-nPosH),y); 
				pDC->SelectObject(&upPen); 
				y = m_iHeightInterval*(i-nPosV+1)-200 + m_iHeightFactor*(int)(pCV->m_pFreq[j-1].f[i-1]) + bottomRect.top; 
				pDC->MoveTo(leftRect.right + m_iWidthInterval * (j-nPosH-1),y); 
				y = m_iHeightInterval*(i-nPosV+1)-200 + m_iHeightFactor*(int)((pCV->m_pFreq[w].f[i-1])+1) + bottomRect.top; 
				pDC->LineTo(leftRect.right + m_iWidthInterval * (j-nPosH),y); 
				pDC->SelectObject(old_pen); 
				break; 
			} 
			int y = m_iHeightInterval*(i-nPosV+1)-200 + m_iHeightFactor*(int)(pCV->m_pFreq[j-1].f[i-1]) + bottomRect.top; 
			pDC->MoveTo(leftRect.right + m_iWidthInterval * (j-nPosH-1),y); 
			y = m_iHeightInterval*(i-nPosV+1)-200 + m_iHeightFactor*(int)(pCV->m_pFreq[j].f[i-1]) + bottomRect.top; 
			pDC->LineTo(leftRect.right + m_iWidthInterval * (j-nPosH),y); 
		} 
	} 
	pDC->SelectObject(pOldPen); 
	penCurve.DeleteObject(); 
} 
//-------------响应按键消息----------------// 
BOOL CCurveView::PreTranslateMessage(MSG* pMsg)  
{ 
	if (pMsg->message==WM_KEYDOWN) 
	{ 
		int nChar=pMsg->wParam; 
		int w = pCV->m_array.GetSize() - pCV->m_iWidth; 
		BOOL bShouldRedraw=FALSE; 
		switch (nChar) 
		{ 
			case VK_SUBTRACT: 
				if (m_iVLine < 30) 
				{ 
					m_iVLine++; 
					bShouldRedraw=TRUE; 
					if (m_iHLine < w) 
					{ 
						m_iHLine++; 
						bShouldRedraw=TRUE; 
					} 
				} 
				break; 
			case VK_ADD: 
				if (m_iVLine > 2) 
				{ 
					m_iVLine--; 
					bShouldRedraw=TRUE; 
					if (m_iHLine >1)  
					{ 
						m_iHLine--; 
						bShouldRedraw=TRUE; 
					} 
				} 
				break; 
			case VK_F5: 
				bShouldRedraw=TRUE; 
				break; 
			case VK_LEFT: 
				OnHScroll(SB_LINEUP,0,NULL); 
				break; 
			case VK_RIGHT: 
				OnHScroll(SB_LINEDOWN,0,NULL); 
				break; 
			case VK_UP: 
				OnVScroll(SB_LINEDOWN,0,NULL); 
				break; 
			case VK_DOWN: 
				OnVScroll(SB_LINEUP,0,NULL); 
				break; 
		} 
		if (bShouldRedraw) 
			 Invalidate(); 
	} 
	return CDialog::PreTranslateMessage(pMsg); 
}