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

📄 stu3901070115view.cpp

📁 这是一个股票系统
💻 CPP
字号:
// STU3901070115View.cpp : CSTU3901070115View 类的实现
//

#include "stdafx.h"
#include "STU3901070115.h"

#include "STU3901070115Doc.h"
#include "STU3901070115View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CSTU3901070115View

IMPLEMENT_DYNCREATE(CSTU3901070115View, CScrollView)

BEGIN_MESSAGE_MAP(CSTU3901070115View, CScrollView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()

// CSTU3901070115View 构造/析构

CSTU3901070115View::CSTU3901070115View()
{
	// TODO: 在此处添加构造代码

}

CSTU3901070115View::~CSTU3901070115View()
{
}

BOOL CSTU3901070115View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CScrollView::PreCreateWindow(cs);
}

// CSTU3901070115View 绘制

void CSTU3901070115View::OnDraw(CDC* pDC)
{
//	CSTU3901070115Doc* pDoc = GetDocument();
//	ASSERT_VALID(pDoc);
//	if (!pDoc)
//		return;
//
//	// Save the current state of the device context
//	int nDC = pDC->SaveDC();
//	
//	// Create font for axis labels
//	CFont aFont;
//
//	if( aFont.
//		CreateFont( 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, FF_MODERN, 0 ))   
//
//		pDC->SelectObject( &aFont );
//	else
//	{
//		AfxMessageBox( _T("Unable to create font") );
//		return;
//	}
//
//	
//	const CStockDataList & pData = pDoc->GetDocList();
//
//	int yPos = 10;
//	int nTextHeight = pDC->GetTextExtent( _T("A") ).cy;
//
//	POSITION pos = pData.GetHeadPosition();
//
//	while( pos )
//	{
//		CStockData sd = pData.GetNext( pos );
//	
//		pDC->TextOut( 10, yPos, sd.GetAsString() );
//		yPos += nTextHeight;
//	}
//
//	// Restore the original device context
//	pDC->RestoreDC( nDC );
	CSTU3901070115Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// Save the current state of the device context
	int nDC = pDC->SaveDC();
	
	const CStockDataList & pData = pDoc->GetDocList();

	// Make a small array containing the
	// records for the current fund.
	// We use an array to take advantage of indexed access
	CArray< CStockData, CStockData & > arrFundData;

	POSITION pos = pData.GetHeadPosition();

	while( pos )
	{
		CStockData sd = pData.GetNext( pos );

		if( sd.GetFund() == pDoc->GetCurrentFund() )
			arrFundData.Add( sd );
	}

	INT_PTR nPrices = arrFundData.GetSize();
	if( nPrices == 0 ) 
		return; 

	// Some constant sizes (in device units)
	const int AXIS_DIVIDER_LENGTH = 6;
	const int AXIS_FONT_HEIGHT = 24;
	const int HEADING_FONT_HEIGHT = 36;

	// Create font for axis labels
	CFont AxisFont;
	if( AxisFont.
		CreateFont( AXIS_FONT_HEIGHT, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, FF_ROMAN, 0 ))   

		pDC->SelectObject( &AxisFont );
	else
	{
		AfxMessageBox( _T("Unable to create Axis font") );
		return;
	}


	CPen AxisPen;
    if( AxisPen.CreatePen( PS_SOLID, 1, RGB(0,0,0) ) )    
        pDC->SelectObject( &AxisPen );
	else
	{
		AfxMessageBox( _T("Unable to create Axis Pen") );
		return;
	}

	// Array to graph co-ordinates as we go
	CArray< CPoint, CPoint& > CoordArray;
	for( int i = 0; i < nPrices; i++ )
		CoordArray.Add( CPoint( 0, 0) );

	// Set viewport origin to bottom left corner of window 
	CPoint ptBottomLeft( 0, -850 );
	pDC->LPtoDP( &ptBottomLeft );
	pDC->SetViewportOrg( ptBottomLeft );

	// Base co-ordinates for axes
	const CPoint ORIGIN(100, 100);
	const CPoint Y_EXTENT( ORIGIN.x, ORIGIN.y + 650);
	const CPoint X_EXTENT(ORIGIN.x + 900, ORIGIN.y);

	// Draw axes
	pDC->MoveTo( Y_EXTENT );
	pDC->LineTo( ORIGIN );
	pDC->LineTo( X_EXTENT );

	int nLabelPos = Y_EXTENT.y + ((ORIGIN.y - Y_EXTENT.y) / 2);
	pDC->TextOut( ORIGIN.x - 50, nLabelPos, _T("$") ); 

	// Divide x-axis into number of prices held in the file
	int nXIncrement = (X_EXTENT.x - ORIGIN.x) / nPrices;

	double nMaxPrice = 0;
	double nMinPrice = 0;

	int i;
	for( i = 0; i < nPrices; i++ )
	{
		int xPoint = (ORIGIN.x + (i * nXIncrement));
		CoordArray[ i ].x = xPoint;

		pDC->MoveTo( xPoint, ORIGIN.y );
		pDC->LineTo( xPoint, ORIGIN.y + AXIS_DIVIDER_LENGTH );
		
		COleDateTime aDate = arrFundData[ i ].GetDate();
		double aPrice = arrFundData[ i ].GetPrice();

		nMaxPrice = max( nMaxPrice, aPrice );
		nMinPrice = nMinPrice == 0 ? 
                                 nMaxPrice : 
		                         min( nMinPrice, aPrice );

		CString strDate = aDate.Format( _T("%m/%d/%y") ); 
		
		if( i == 0 || i == (nPrices-1) )
			pDC->TextOut( xPoint-2, ORIGIN.y - AXIS_FONT_HEIGHT / 2, 
						  strDate );
		else
		{
			CString strDay = strDate.Mid( strDate.Find( _T('/') ) + 1);
			strDay = strDay.Left( strDay.Find( _T('/') ) );
			pDC->TextOut( xPoint-6, ORIGIN.y - AXIS_FONT_HEIGHT / 2, 
						  strDay );
		}
	}


	// Divide y-axis into suitable scale based on 
	// the difference between max and min prices on file
	nMaxPrice += 2.0;
	nMinPrice -= 1.0;
	int iScale = int(nMaxPrice) - int(nMinPrice);

	int nYIncrement = ( ORIGIN.y - Y_EXTENT.y ) / iScale;
	
	for( i = 0; i < iScale; i++ )
	{
		int yPoint = (ORIGIN.y - (i * nYIncrement));
		pDC->MoveTo( ORIGIN.x, yPoint);
		pDC->LineTo( ORIGIN.x - AXIS_DIVIDER_LENGTH, yPoint);
		
		int iCurrentPrice = int(nMinPrice) + i;

		for( int j = 0; j < nPrices; j++ )
		{
			double aPrice = arrFundData[ j ].GetPrice();
			if( aPrice >= double(iCurrentPrice) && 
				aPrice <  double(iCurrentPrice) + 1.0 )
			{
				double dFraction = aPrice - double(iCurrentPrice);
				CoordArray[ j ].y = 
					yPoint - int(dFraction * double( nYIncrement ) );
			}
		}


		CString strPrice;
		strPrice.Format( _T("%d"), iCurrentPrice );

		int nTextSize = pDC->GetTextExtent( strPrice ).cx;
		nTextSize += 10;
		
		pDC->TextOut( ORIGIN.x - nTextSize, yPoint+12, strPrice );
	}


	// Graph figures stored in CoordArray
	CPen GraphPen;	
    if( GraphPen.CreatePen( PS_SOLID, 1, RGB(255,0,0) ) )  // Red pen  
	{
        pDC->SelectObject( &GraphPen );
	}
	else
	{
		AfxMessageBox( _T("Unable to create Graph Pen") );
		return;
	}


	// Draw Graph
	// Label graph points with price value (in blue)

	COLORREF crOldText = pDC->SetTextColor( RGB( 0,0,255 ) );

	pDC->MoveTo( CoordArray[ 0 ] );

	for( i = 0; i < nPrices; i++ )
	{

		pDC->LineTo( CoordArray[ i ] );
		
		CPoint TextPoint;

		if( (i+1) < nPrices )
		{
			if( CoordArray[ i + 1 ].y >= CoordArray[ i ].y )
				TextPoint = CoordArray[ i ] + CPoint( 5, 0 );
			else
				TextPoint = CoordArray[ i ] + CPoint( 5, AXIS_FONT_HEIGHT );
		}
		else
			TextPoint = CoordArray[ i ] + CPoint( 5, 0);

		CString strPrice;
		strPrice.Format( _T("%.2f"), arrFundData[ i ].GetPrice() );

		pDC->TextOut( TextPoint.x, TextPoint.y, strPrice  ); 
	}

	pDC->SetTextColor( crOldText );


	// Create heading
	CFont HeadingFont;
	if( HeadingFont.
		CreateFont( HEADING_FONT_HEIGHT, 0, 0, 0, FW_BOLD, 1, 0, 0, 0, 0, 0, 0, FF_ROMAN, 0 ))   

		pDC->SelectObject( &HeadingFont );
	else
	{
		AfxMessageBox( _T("Unable to create Heading Font") );
		return;
	}

	CString strHeading = pDoc->GetCurrentFund();
	strHeading += _T(" - Closing Prices ");

	COleDateTime aDate = arrFundData[ 0 ].GetDate();
	strHeading += aDate.Format( _T("%m/%d/%y") ); 
	strHeading += _T(" to ");
	aDate = arrFundData[ nPrices - 1 ].GetDate();
	strHeading += aDate.Format( _T("%m/%d/%y") ); 

	CSize sizeText = pDC->GetTextExtent( strHeading );

	pDC->TextOut( X_EXTENT.x - sizeText.cx, 
				  Y_EXTENT.y + sizeText.cy, strHeading );

	// Restore the original device context
	pDC->RestoreDC( nDC );


	// TODO: 在此处为本机数据添加绘制代码
}

void CSTU3901070115View::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;
	// TODO: 计算此视图的合计大小
	sizeTotal.cx =  1100;
	sizeTotal.cy = 850;
	SetScrollSizes(MM_LOENGLISH, sizeTotal);
}


// CSTU3901070115View 打印

BOOL CSTU3901070115View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CSTU3901070115View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加额外的打印前进行的初始化过程
}

void CSTU3901070115View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加打印后进行的清除过程
}


// CSTU3901070115View 诊断

#ifdef _DEBUG
void CSTU3901070115View::AssertValid() const
{
	CScrollView::AssertValid();
}

void CSTU3901070115View::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CSTU3901070115Doc* CSTU3901070115View::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSTU3901070115Doc)));
	return (CSTU3901070115Doc*)m_pDocument;
}
#endif //_DEBUG


// CSTU3901070115View 消息处理程序

⌨️ 快捷键说明

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