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

📄 slistview.cpp

📁 股票软件
💻 CPP
📖 第 1 页 / 共 2 页
字号:

#include "stdafx.h"
#include "SListView.h"

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

#define	SLV_TIMER_REFRESH		1

#define	IDC_SLISTVIEW_GRID		100

/////////////////////////////////////////////////////////////////////////////
// CSListView

IMPLEMENT_DYNCREATE(CSListView, CView)

BEGIN_MESSAGE_MAP(CSListView, CView)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_ERASEBKGND()
	ON_WM_TIMER()
	ON_WM_SIZE()
	ON_WM_SETFOCUS()
	ON_WM_LBUTTONDOWN()
	ON_MESSAGE(WM_APP_STKRECEIVER_DATA, OnStkReceiverData)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSListView construction/destruction

CSListView::CSListView()/* : CView(CSListView::IDD)*/
{
	m_nColSort		=	-1;
	m_bSortAscend	=	FALSE;
	m_bFirstUpdate	=	TRUE;
}

CSListView::~CSListView()
{
}

void CSListView::SetFont(LPLOGFONT pLogFont)
{
	ASSERT(pLogFont);

	HFONT hFont = ::CreateFontIndirect(pLogFont);
	m_Grid.SendMessage(WM_SETFONT, (WPARAM)hFont, MAKELPARAM(1, 0));
	m_Grid.AutoSize();
	DeleteObject(hFont);
}

void CSListView::GetSelectedStocks(CSPStringArray & astr)
{
	CStockContainer & container = AfxGetStockContainer();
	container.Lock();

	int	nTotalCount	=	m_Grid.GetSelectedCount();
	astr.RemoveAll();
	astr.SetSize(0, nTotalCount > 10 ? nTotalCount : -1);
	for (int nRow=1; nRow<m_Grid.GetRowCount(); nRow++)
	{
		BOOL	bSelected	=	FALSE;
		for (int nCol=0; nCol<m_Grid.GetColumnCount(); nCol ++)
			bSelected	|=	(m_Grid.GetItemState(nRow,nCol) & GVIS_SELECTED);
		if (!bSelected)
			continue;

		LPARAM	id	=	m_Grid.GetItemData(nRow,0);
		CStockInfo & info	=	container.GetStockInfoByID(id);
		astr.Add(info.GetStockCode());
	}
	container.UnLock();
}

void CSListView::StoreColumnOrderArray()
{
	// Now The CGridCtrl cannot drag and drop columns, so nothing to do
	// in this function
	return;
}

void CSListView::ResetColumns()
{
	CStockContainer& container = AfxGetStockContainer();
	container.Lock();

	// its list control through a call to GetListCtrl().
	CSPDWordArray& auint = AfxGetProfile().GetSListColumnsShow();
	ASSERT(auint.GetSize() > 0);

	m_Grid.SetRedraw(FALSE);

	m_Grid.DeleteAllItems();

	m_Grid.SetRowCount(1);
	m_Grid.SetFixedRowCount(1);
	m_Grid.SetFixedColumnCount(2);
	m_Grid.SetColumnCount(auint.GetSize());
	m_Grid.SetRowHeight(0, 25);

	for (int nCol = 0; nCol < auint.GetSize(); nCol++)
	{
		CString	string = AfxGetVariantName(auint[nCol], TRUE);
		m_Grid.SetItemText(0, nCol, string);
		m_Grid.SetItemData(0, nCol, auint[nCol]);
		m_Grid.SetItemFormat(0, nCol, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
		m_Grid.SetItemBkColour(0, nCol, AfxGetProfile().GetColor(CColorClass::clrSListBK));
		m_Grid.SetItemFgColour(0, nCol, AfxGetProfile().GetColor(CColorClass::clrTitle));
	}

	m_Grid.AutoSizeColumns();
	m_Grid.SetColumnWidth(0, 60);
	m_Grid.SetColumnWidth(1, 50);
	m_Grid.SetColumnWidth(2, 60);
	m_Grid.SetColumnWidth(3, 60);
	m_Grid.SetColumnWidth(4, 60);
	m_Grid.SetColumnWidth(5, 60);
	m_Grid.SetColumnWidth(6, 60);
	m_Grid.SetColumnWidth(7, 60);
	m_Grid.SetColumnWidth(8, 60);

	m_Grid.SetRedraw(TRUE, TRUE);
	m_Grid.Invalidate();

	container.UnLock();
}

BOOL CSListView::SetAverageItem(CGridCtrl &grid, CStockContainer & container, BOOL bRedraw)
{
	if (grid.GetColumnCount() <= 0)
		return FALSE;

	container.Lock();

	int nCol=0, nRow=0;

	// Get LPARAM
	CUIntArray	anParams;
	anParams.SetSize(0, grid.GetColumnCount());
	for ( nCol=0; nCol < grid.GetColumnCount(); nCol ++)
	{
		LPARAM	lParam	=	grid.GetItemData(0, nCol);
		anParams.Add(lParam);
	}

	// Set Average
	CStockInfo & infoAve = container.GetAverage();
	CStockInfo & infoWAve = container.GetWeightAverage();
	int	iRowAve=0, iRowWAve=0;

	if (grid.GetRowCount() >= 3
		&& grid.GetItemData(grid.GetRowCount()-2,0) == (LPARAM)ID_STOCKCNTN_AVERAGE
		&& grid.GetItemData(grid.GetRowCount()-1,0) == (LPARAM)ID_STOCKCNTN_WEIGHTAVERAGE)
	{
		// get item id
		iRowAve		=	grid.GetRowCount()-2;
		iRowWAve	=	grid.GetRowCount()-1;
	}
	else
	{
		// Insert item
		iRowAve = grid.InsertRow(infoAve.GetStockName());
		grid.SetItemData(iRowAve, 0, (LPARAM)ID_STOCKCNTN_AVERAGE);
		iRowWAve = grid.InsertRow(infoWAve.GetStockName());
		grid.SetItemData(iRowWAve, 0, (LPARAM)ID_STOCKCNTN_WEIGHTAVERAGE);
	}

	// Set Average
	for ( nCol=0; nCol<anParams.GetSize(); nCol++)
	{
		grid.SetItemText(iRowAve, nCol, AfxGetVariantDispString(anParams[nCol], infoAve, NULL));
		grid.SetItemBkColour(iRowAve, nCol, AfxGetProfile().GetColor(CColorClass::clrSListBK));
		grid.SetItemFgColour(iRowAve, nCol, AfxGetVariantColor(anParams[nCol], infoAve));
	}

	// Set Weight Average
	for ( nCol=0; nCol<anParams.GetSize(); nCol++)
	{
		grid.SetItemText(iRowWAve, nCol, AfxGetVariantDispString(anParams[nCol], infoWAve, NULL));
		grid.SetItemBkColour(iRowWAve, nCol, AfxGetProfile().GetColor(CColorClass::clrSListBK));
		grid.SetItemFgColour(iRowWAve, nCol, AfxGetVariantColor(anParams[nCol], infoWAve));
	}

	//	Set Param which is
	//	SLH_MARKETVALUE, SLH_MARKETVALUEA, SLH_MARKETVALUEB and etc,  and more than SLH_USERDEFINE_BEGIN
	for ( nCol=0; nCol < anParams.GetSize(); nCol ++)
	{
		UINT	lParam	=	anParams[nCol];
		if (SLH_DIFF == lParam || SLH_DIFFPERCENT == lParam || SLH_SCOPE == lParam
			|| SLH_DIFFPERCENT_MIN5 == lParam || SLH_PE == lParam
			|| SLH_PMAININCOME == lParam || SLH_RATIO_PCASH == lParam
			|| SLH_RATIO_CURRENCY == lParam || SLH_RATIO_CHANGEHAND == lParam
			|| SLH_RATIO_VOLUME == lParam || SLH_RS == lParam
			|| SLH_MARKETVALUE == lParam || SLH_MARKETVALUEA == lParam
			|| SLH_MARKETVALUEB == lParam || lParam >= SLH_USERDEFINE_BEGIN)
		{
			double	dc = 0., average = 0.;
			double	wsum = 0.0001, waverage = 0., w = 0.;
			for (int iRow=1; iRow<grid.GetRowCount(); iRow++)
			{
				if (iRow == iRowAve || iRow == iRowWAve)
					continue;

				int	id	=	grid.GetItemData(iRow,0);
				if (id < 0 || id > container.GetSize())
					continue;

				CStockInfo	& info = container.ElementAt(id);
				w	=	info.m_fShare_count_total;
				double	dValue	=	0.;
				if (!AfxGetVariantValue(lParam, info, &dValue, &container))
					continue;

				average		=	(average * dc + dValue)/(dc+1);
				waverage	=	(waverage * wsum + dValue * w)/(wsum+w);

				dc		+=	1;
				wsum	+=	w;
			}

			CString	strText;
			if (SLH_MARKETVALUE == lParam || SLH_MARKETVALUEA == lParam || SLH_MARKETVALUEB == lParam)
			{
				strText.Format("%u", (DWORD)average);
				grid.SetItemText(iRowAve, nCol, strText);
				grid.SetItemText(iRowWAve, nCol, "-");
			}
			else
			{
				strText.Format("%.2f", average);
				grid.SetItemText(iRowAve, nCol, strText);
				strText.Format("%.2f", waverage);
				grid.SetItemText(iRowWAve, nCol, strText);
			}
		}
	}

	container.UnLock();

	if (bRedraw)
	{
		grid.RedrawRow(iRowAve);
		grid.RedrawRow(iRowWAve);
	}
	return TRUE;
}

void CSListView::OnDblclkItem(int nStockIndex)
{
	CStockContainer & container = AfxGetStockContainer();
	int	nType;
	CSPString	strDomain;
	DWORD	dwDate	=	-1;
	if (nStockIndex >= 0)
	{
		container.GetCurrentType(&nType, &strDomain, &dwDate);

		if ((nType == CStockContainer::typeDomain && strDomain.IsEmpty())
			|| (nType == CStockContainer::typeGroup && strDomain.IsEmpty()))
		{
			CStockInfo	& info	=	container.GetStockInfoByID(nStockIndex);
//			AfxShowSlist(nType, CString(info.GetStockName()), dwDate);
		}
//		else
//			AfxShowStockRealTime(nStockIndex, TRUE);
	}
}

void CSListView::StockInfoChanged(LONG infoid, CStockInfo & info)
{
	CStockContainer & container = AfxGetStockContainer();

	container.Lock();

	BOOL bFind = FALSE;
	// update grid value
	for (int nRow=m_Grid.GetRowCount()-1; nRow > 0; nRow--)
	{
		LPARAM	id	=	m_Grid.GetItemData(nRow,0);
		if (id == infoid)
		{
			for (int nCol=0; nCol<m_Grid.GetColumnCount(); nCol++)
			{
				LPARAM lParam = m_Grid.GetItemData(0,nCol);
				m_Grid.SetItemText(nRow, nCol, AfxGetVariantDispString(lParam, info, &container));
				m_Grid.SetItemFgColour(nRow, nCol, AfxGetVariantColor(lParam, info));
			}
			m_Grid.RedrawRow(nRow);
			bFind = TRUE;
			break;
		}
	}

	if(!bFind) // 如果没有发现, 添加此行
	{
		int nRow = m_Grid.InsertRow(info.GetStockName());
		m_Grid.SetItemData(nRow, 0, infoid);

		int	nColumnCount=m_Grid.GetColumnCount(), nCol=0;
		
		for (nCol=0; nCol<nColumnCount; nCol++)
		{
			LPARAM lParam = m_Grid.GetItemData(0,nCol);
			m_Grid.SetItemFormat(nRow, nCol, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
			m_Grid.SetItemText(nRow, nCol, AfxGetVariantDispString(lParam, info, &container));
			m_Grid.SetItemBkColour(nRow, nCol, AfxGetProfile().GetColor(CColorClass::clrSListBK));
			m_Grid.SetItemFgColour(nRow, nCol, AfxGetVariantColor(lParam, info));
		}
		m_Grid.RedrawRow(nRow);
	}

	container.UnLock();
}

void CSListView::SendRequestQuote(BOOL bForced)
{
	if (!AfxGetStkReceiver().EngineIsWorking())
		return;

	static int nLastStart = -1;
	static int nLastCount = -1;

	BOOL bQuoteTipSended = FALSE;

	BOOL bInTrade = CSPTime::InTradeTime(CSPTime::GetCurrentTime().GetTime(), 900);

	if (bForced)
	{
		CStockContainer stocks;
		CStockInfo info;
		if (AfxGetStockContainer().GetStockInfo(STKLIB_CODE_MAIN, &info))
			stocks.Add(info);
		if (AfxGetStockContainer().GetStockInfo(STKLIB_CODE_MAINSZN, &info))
			stocks.Add(info);
		if (stocks.GetSize() > 0)
			AfxGetStkReceiver().RequestStockData(CStock::dataReport, stocks.GetData(), stocks.GetSize(), 0, 0);
	}

	if (bForced)
	{
		CCellRange cr = m_Grid.GetVisibleNonFixedCellRange();
		int nStart = cr.GetMinRow() - 1;
		int nCount = cr.GetMaxRow() - cr.GetMinRow() + 1;
		CStockContainer& container = AfxGetStockContainer();
		container.Lock();
		if (nStart >= 0 && nCount > 0 && container.GetSize() > 0 && (bInTrade || bForced || nLastStart != nStart || nLastCount != nCount))
		{
			nLastStart = nStart;
			nLastCount = nCount;
			AfxGetStkReceiver().RequestStockData(CStock::dataReport, container.GetData()+nStart, min(nCount,container.GetSize()-nStart), 0, 0);
			bQuoteTipSended = (0 == nStart && nCount >= 9);
		}
		container.UnLock();
	}

⌨️ 快捷键说明

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