BOOL CSListView::SetAverageItem( CGridCtrl &grid, CStockContainer & container, BOOL bRedraw ) { if( grid.GetColumnCount() <= 0 ) return FALSE; container.Lock(); // Get LPARAM CUIntArray anParams; anParams.SetSize( 0, grid.GetColumnCount() ); for( int 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; }
/***************************************************************************** Allows Tree + Btn object to call drawing member *****************************************************************************/ BOOL CGridTreeCellBase::DrawTreeCell(CDC* pDC, int nRow, int nCol, CRect* prect, BOOL /* bEraseBkgnd */) { ASSERT( m_pTreeColumn != NULL); if( !m_pTreeColumn->GetAllowDraw()) return FALSE; CGridCtrl* pGrid = GetGrid(); if (!pGrid || !pDC) return FALSE; if( m_pTreeColumn->GetFixedRowCount() != pGrid->GetFixedRowCount() || m_pTreeColumn->GetRowCount() > pGrid->GetRowCount() ) { ASSERT( FALSE); // if ASSERT here, this means that you a tree in a column // but you called CGridCtrl::SetFixedRowCount() or // or CGridCtrl::SetRowCount() directly. You can't do this. // To change the layout of rows when you are using the tree, // you must call CTreeColumn::Setup(). return FALSE; } if( prect->Width() <= 0 || prect->Height() <= 0) // prevents imagelist item from drawing even return FALSE; // though cell is hidden // tree drawing graphic logic begins here int nSavedDC = pDC->SaveDC(); if( nCol == m_pTreeColumn->GetColumnWithTree()) { // this column has a tree // move text over to allow for level indentation if( IsViewable() ) { // if row is showing, draw the tree graphic if( m_pTreeColumn->GetTreeLines()) { TreeDrawGraphic( pDC, // current CDC *prect); // coordinates of bounding cell rectangle } else if( m_pTreeColumn->GetTreeUsesImages()) { TREE_IMAGE TreeImage = TREE_IMAGE_DOCUMENT; // is it not the very last row? if( nRow + m_pTreeColumn->GetFixedRowCount() < m_pTreeColumn->GetRowCount()) { // is it a plus or minus? BOOL bIsPlus; BOOL bIsMinus; BOOL bIsLastLeaf; if( m_pTreeColumn->TreeCellHasPlusMinus( nRow, // row of Cell to check &bIsPlus, // returns: T=Is a plus &bIsMinus, // returns: T=Is a minus &bIsLastLeaf) )// returns: T=Is Last Leaf { // returns: T=cell has a plus or minus; F=not if( bIsPlus) TreeImage = TREE_IMAGE_FOLDER_CLOSED; else TreeImage = TREE_IMAGE_FOLDER_OPEN; } } SetImage( TreeImage); } } prect->left += GetTreeIndent(); } pDC->RestoreDC(nSavedDC); return TRUE; }