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

📄 stockview.cpp

📁 程序经过了调试,直接运行就可以显示结果,为股市应用程序,VC++的
💻 CPP
字号:
// stockView.cpp : implementation of the CStockView class
//

#include "stdafx.h"
#include "stock.h"

#include "stockDoc.h"
#include "stockView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int  CStockView:: Num=0;
float  CStockView:: Max=0;
float  CStockView::Min=0;
KLine  CStockView::kline[30];
CString CStockView::strName;
CString CStockView::strCode;
int    CStockView::xpoint[30];
/////////////////////////////////////////////////////////////////////////////
// CStockView

IMPLEMENT_DYNCREATE(CStockView, CView)

BEGIN_MESSAGE_MAP(CStockView, CView)
//{{AFX_MSG_MAP(CStockView)
// NOTE - the ClassWizard will add and remove mapping macros here.
//    DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStockView construction/destruction
CStockView::CStockView()
{
	// TODO: add construction code here
	strCode=_T( "600001");
	strName=_T("");
}

CStockView::~CStockView()
{
}
void CStockView::GetData(CData& data)
{
	strCode=data.m_strCode;
	strName=data.m_strName;
	Num=Readdate(strCode);
/*	if(Num==0)
	{
		for(int i=0;i<30;i++)
		{
	    kline[i].Highest=0;
		kline[i].Lowest=0;
        kline[i].Open=0;
		kline[i].Volume=0;
		kline[i].LastClose=0;
		}
	}
	else
	{
	if(Num<30&&Num>0)
	{
		kline[Num].Highest=data.m_pAllData->m_KLine.Highest;
		kline[Num].Lowest=data.m_pAllData->m_KLine.Lowest;
        kline[Num].Open=data.m_pAllData->m_KLine.Open;
		kline[Num].Volume=data.m_pAllData->m_KLine.Volume;
		kline[Num].LastClose=data.m_pAllData->m_KLine.LastClose;
	}
	else
	{
        kline[0].Highest=data.m_pAllData->m_KLine.Highest;
		kline[0].Lowest=data.m_pAllData->m_KLine.Lowest;
        kline[0].Open=data.m_pAllData->m_KLine.Open;
		kline[0].Volume=data.m_pAllData->m_KLine.Volume;
		kline[0].LastClose=data.m_pAllData->m_KLine.LastClose;
	}
	}*/

}
float CStockView::MaxValue(int n)
{
	float temp=kline[0].Highest;
	for(int i=0;i<n;i++)
	{
		if(temp<kline[i].Highest)
			temp=kline[i].Highest; 
	}
	return temp;    
}
float CStockView::MaxVolume(int n)
{
	float temp=kline[0].Volume;
	for(int i=0;i<n;i++)
	{
		if(temp<kline[i].Volume)
			temp=kline[i].Volume;
	}
	return temp; 
	
}
float CStockView::MinValue(int n)
{
	float temp=kline[0].Lowest;
	for(int i=0;i<n;i++)
	{
		if(temp>kline[i].Lowest)
			temp=kline[i].Lowest;

	}
	return temp;    
}
int CStockView::Readdate(CString strCode)
{
	CString name=_T("");
	int Num;
	try
	{ 
		name=strCode+_T(".txt");
		CFile file(name, CFile::modeReadWrite|CFile::modeReadWrite|CFile::modeNoTruncate);
		CArchive ar(&file,CArchive::load);
		int size=file.GetLength();
		if (size==0)
		{
			Num=0;
		}
		Num=size/(4*5);
		for (int i=0; i<Num; i++ )
		{
			ar>>kline[i].Open>>kline[i].LastClose>>kline[i].Highest>>kline[i].Lowest>>kline[i].Volume;
			//		TRACE("%10.3f,%10.3f,%10.3f,%10.3f,%10.3f \n",data1[i].Open,data1[i].LastClose,data1[i].Highest,data1[i].Lowest,data1[i].Volume);
		}
		file.Close();
		}

	catch(CFileException* e)
	{
		e->ReportError();
		e->Delete();
	}
	return Num;
}
void  CStockView::GetXPoint()
{
   CRect     rect;	
   GetClientRect(&rect);
   int m=(int)(rect.right-75)/30;
   for(int i=0;i<30;i++)
   {
   xpoint[i]=60+(i+1)*m;
   }
}
BOOL CStockView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CStockView drawing

void CStockView::OnDraw(CDC* pDC)
{
	CStockDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	CRect rectClient; 
    CBrush brushBkColor;     
    brushBkColor.CreateSolidBrush(RGB(0,0,0)); 
	
	GetClientRect(rectClient); 
    pDC->FillRect(rectClient,&brushBkColor);
	DrawBkgLine(pDC);
	if(Num>0)
	{
    DrawVolume( pDC);
	DrawKLine(pDC);
	}
	else
		return;
}

/////////////////////////////////////////////////////////////////////////////
// CStockView printing

BOOL CStockView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CStockView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CStockView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CStockView diagnostics

#ifdef _DEBUG
void CStockView::AssertValid() const
{
	CView::AssertValid();
}

void CStockView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CStockDoc* CStockView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CStockDoc)));
	return (CStockDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CStockView message handlers
void CStockView::DrawBkgLine(CDC* pDC)
{
    int i=0;
	char      chName[300];
	CRect     rect;	
	CPoint    client;
	CPen      redpen(PS_SOLID,2,RGB(153,0,0));
	CPen      dashpen(PS_DOT,1,RGB(255,255,255));
	
	pDC->SetBkMode(TRANSPARENT); 
	pDC->SetTextColor(RGB(255,255,255));
	GetClientRect (&rect);
	client.x=rect.right;
	client.y=rect.bottom;
	sprintf(chName, "%8s", strName);
	pDC->TextOut(85, 30, chName, lstrlen(chName));
	
	sprintf(chName, "%8s", strCode);
	pDC->TextOut(180, 30, chName, lstrlen(chName));
	
	//画边框日线边框
	pDC->SelectObject(&redpen);
	
	rect.left = 80;
	rect.top = 60;
	rect.right = client.x-10;
	rect.bottom = client.y-80;
	
	pDC->SelectStockObject(NULL_BRUSH);
	pDC->Rectangle(&rect);
	
	//画下边框
	rect.left = 10;
	rect.top = client.y-60;
	rect.right = client.x-10;
	rect.bottom = client.y-10;
	pDC->Rectangle(&rect);
	
	//draw big rect
	rect.left=10;
	rect.top=10;
	rect.bottom=client.y-10;
	rect.right=client.x-10;
	pDC->Rectangle(&rect);

	//画横中间实线
	pDC->MoveTo( 10, 60+((client.y-140)/2) );
	pDC->LineTo( client.x-10, 60+((client.y-140)/2) );

	pDC->MoveTo(10,60);
	pDC->LineTo(client.x-10,60);

	pDC->MoveTo(10,client.y-80);
	pDC->LineTo(client.x-10,client.y-80);

	
	//背景虚线
	pDC->SelectObject(&dashpen);
	for ( i=0; i<5; i++ )
	{
		pDC->MoveTo( 80, 60+(i+1)*((client.y-140)/2/6) );
		pDC->LineTo( client.x-10, 60+(i+1)*((client.y-140)/2/6) );
	}
	for ( i=6; i<11; i++ )
	{
		pDC->MoveTo( 80, 60+(i+1)*((client.y-140)/2/6) );
		pDC->LineTo( client.x-10, 60+(i+1)*((client.y-140)/2/6) );
	}
	float slope=(rect.bottom-160)/2;	
	for(int j=0;j<5;j++)
	{
      sprintf(chName, "%9.0f", 0);
	  pDC->TextOut(10, rect.bottom-75-(j+1)*slope/5, chName, lstrlen(chName));
	}
	float ydelt=(rect.bottom-160)/2;
	for(int k=0;k<5;k++)
	{
     sprintf(chName, "%10.3f", 0.0);
	  pDC->TextOut(10, rect.bottom/2-10-(k+1)*ydelt/5, chName, lstrlen(chName));
	}

	//画下边框内文字
	sprintf(chName, "%8s", "◆股市有风险   祝君好运道");
	pDC->TextOut(65, client.y-40, chName, lstrlen(chName));
	pDC->SetBkMode(OPAQUE);
	pDC->SetBkColor(RGB(0,0,0));			
}
void  CStockView::DrawVolume(CDC* pDC)
{
	CPen  redpen(PS_SOLID,8,RGB(150,0,0));
	CPen  bluepen(PS_SOLID,8,RGB(0,150,0));
	CRect     rect;	
	char      chName[200];
    GetClientRect(&rect);
	Num=Readdate(strCode);
	GetXPoint();
	CPoint point[30];
	float MaxV=MaxVolume(Num);
	float slope=(rect.bottom-160)/2;
	for(int i=0;i<Num;i++)
	{
          point[i].x=xpoint[i];
		  point[i].y=rect.bottom-80-((kline[i].Volume)*slope)/MaxV;
		  if(kline[i].Open<=kline[i].LastClose)
		  pDC->SelectObject(&redpen);
		  else
			  pDC->SelectObject(&bluepen);
		  pDC->MoveTo(point[i].x,rect.bottom-80);
		  pDC->LineTo(point[i].x,point[i].y);
	}
    pDC->SelectObject(&redpen);
	for(int j=0;j<5;j++)
	{
      sprintf(chName, "%9.0f", MaxV*(j+1)/5);
	  pDC->TextOut(10, rect.bottom-80-(j+1)*slope/5, chName, lstrlen(chName));
	}
}
void CStockView::DrawKLine(CDC *pDC)
{
    CPen  redpen(PS_SOLID,8,RGB(150,0,0));
	CPen  bluepen(PS_SOLID,8,RGB(0,150,0));
	CPen  redpenthin(PS_SOLID,2,RGB(150,0,0));
	CPen  bluepenthin(PS_SOLID,2,RGB(0,150,0));
    Num=Readdate(strCode);
	GetXPoint();
	CRect     rect;	
	char      chName[200];
    GetClientRect(&rect);
	CPoint point[30][4];
    Max=MaxValue(Num);
	Min=MinValue(Num);
	float delt=(Max-Min);
	float ydelt=(rect.bottom-160)/2;
	for(int i=0;i<Num;i++)
	{
		point[i][0].y=rect.bottom/2-10-((kline[i].Lowest-Min)*ydelt)/delt;
		point[i][1].y=rect.bottom/2-10-((kline[i].Open-Min)*ydelt)/delt;
		point[i][2].y=rect.bottom/2-10-((kline[i].LastClose-Min)*ydelt)/delt;
		point[i][3].y=rect.bottom/2-10-((kline[i].Highest-Min)*ydelt)/delt;
        point[i][0].x=xpoint[i];
		point[i][1].x=xpoint[i];
		point[i][2].x=xpoint[i];
		point[i][3].x=xpoint[i];
		if(kline[i].Open<=kline[i].LastClose)
		{
          pDC->SelectObject(&redpenthin);
          pDC->MoveTo(point[i][0]);
		  pDC->LineTo(point[i][1]);
		  pDC->MoveTo(point[i][2]);
		  pDC->LineTo(point[i][3]);
          pDC->SelectObject(&redpen);
          pDC->MoveTo(point[i][1]);
		  pDC->LineTo(point[i][2]);

		}
		else
		{
		pDC->SelectObject(&bluepenthin);
          pDC->MoveTo(point[i][0]);
		  pDC->LineTo(point[i][2]);
		  pDC->MoveTo(point[i][1]);
		  pDC->LineTo(point[i][3]);
          pDC->SelectObject(&bluepen);
          pDC->MoveTo(point[i][2]);
		  pDC->LineTo(point[i][1]);
		}
	}
	for(int j=0;j<5;j++)
	{
     sprintf(chName, "%10.3f", Min+delt*(j+1)/5);
	  pDC->TextOut(10, rect.bottom/2-10-(j+1)*ydelt/5, chName, lstrlen(chName));
	}
}

⌨️ 快捷键说明

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