GridCellBase.cpp
资源名称:MyStock.rar [点击查看]
上传用户:wenjimin
上传日期:2014-08-12
资源大小:111k
文件大小:7k
源码类别:
金融证券系统
开发平台:
Visual C++
- // GridCellBase.cpp: implementation of the CGridCellBase class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "GridCtrl.h"
- #include "GridCellBase.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- IMPLEMENT_DYNAMIC(CGridCellBase, CObject)
- CGridCellBase::CGridCellBase()
- {
- m_nState=0;//如果不对它初始化,非固定的单元格背景色会不对
- }
- CGridCellBase::~CGridCellBase()
- {
- }
- BOOL CGridCellBase::Draw(CDC* pDC,int nRow,int nCol,CRect rect,BOOL bEraseBkgng/*=TRUE*/)
- {
- CGridCtrl* pGrid = GetGrid();
- ASSERT(pGrid);
- if (!pGrid ||!pDC)
- return FALSE;
- if( rect.Width() <= 0 || rect.Height() <= 0) // prevents imagelist item from drawing even
- return FALSE; // though cell is hidden
- TRACE3("Drawing %scell %d, %dn", IsFixed()? _T("Fixed ") : _T(""), nRow, nCol);
- int nSavedDC = pDC->SaveDC();
- pDC->SetBkMode(TRANSPARENT);//背景透明模式
- CGridDefaultCell *pDefaultCell = (CGridDefaultCell*) GetDefaultCell();//得到却省得单元格设置
- if (!pDefaultCell)
- return FALSE;
- // Set up text and background colours
- COLORREF TextClr, TextBkClr;//文本色和文本背景色
- TextClr = (GetTextClr() == CLR_DEFAULT)? pDefaultCell->GetTextClr() : GetTextClr();//取得颜色
- if (GetBackClr() == CLR_DEFAULT)//CLR_DEFAULT是Windows却省的背景颜色
- TextBkClr = pDefaultCell->GetBackClr();
- else
- {
- TextBkClr = GetBackClr();
- }
- if(!IsFixed())
- {
- TRY
- {
- CBrush brush(TextBkClr);
- pDC->FillRect(rect, &brush);//填充矩形
- }
- CATCH(CResourceException, e)
- {
- e->ReportError();
- }
- END_CATCH
- }
- else
- {
- CPen lightpen(PS_SOLID, 1, ::GetSysColor(COLOR_3DHIGHLIGHT)),
- darkpen(PS_SOLID, 1, ::GetSysColor(COLOR_3DDKSHADOW)),
- *pOldPen = pDC->GetCurrentPen();
- pDC->SelectObject(&lightpen);//使用亮色
- pDC->MoveTo(rect.right, rect.top);
- pDC->LineTo(rect.left, rect.top);//从右到左画顶边
- pDC->LineTo(rect.left, rect.bottom);//从上到下画左边
- pDC->SelectObject(&darkpen);//使用暗色
- pDC->MoveTo(rect.right, rect.top);
- pDC->LineTo(rect.right, rect.bottom);//从上到下画右边
- pDC->LineTo(rect.left, rect.bottom);//从右到左画底边
- pDC->SelectObject(pOldPen);//恢复笔
- }
- CFont *pFont = GetFontObject();//在这里选择了字体
- ASSERT(pFont);
- if(pFont)
- pDC->SelectObject(pFont);
- // Draw sort arrow
- //画排序箭头:
- if (pGrid->GetSortColumn() == nCol && nRow == 0)//如果排序的列是nCol并且行号是0才画
- {
- CSize size = pDC->GetTextExtent(_T("M"));
- int nOffset = 2;
- // Base the size of the triangle on the smaller of the column
- // height or text height with a slight offset top and bottom.
- // Otherwise, it can get drawn outside the bounds of the cell.
- size.cy -= (nOffset * 2);
- if (size.cy >= rect.Height())
- size.cy = rect.Height() - (nOffset * 2);
- size.cx = size.cy; // Make the dimensions square
- // Kludge for vertical text
- BOOL bVertical = (GetFont()->lfEscapement == 900);
- // Only draw if it'll fit!
- if (size.cx + rect.left < rect.right+2)
- {
- int nTriangleBase = rect.bottom - nOffset - size.cy; // Triangle bottom right
- int nTriangleLeft;
- if (bVertical)
- nTriangleLeft = (rect.right + rect.left - size.cx)/2; // Triangle middle
- else
- nTriangleLeft = rect.right - size.cx; // Triangle RHS
- CPen penShadow(PS_SOLID, 0, /*::GetSysColor(COLOR_3DSHADOW)*/RGB(0,0,255));
- //CPen penLight(PS_SOLID, 0, ::GetSysColor(COLOR_3DHILIGHT));
- if (pGrid->GetSortAscending())//升序的
- {
- // Draw triangle pointing upwards
- //画顶尖向上的三角形
- CPen *pOldPen = (CPen*) pDC->SelectObject(&penShadow/*penLight*/);
- pDC->MoveTo( nTriangleLeft-4 +(size.cx)/2, nTriangleBase +size.cy );
- pDC->LineTo( nTriangleLeft-4 +(size.cx)/2, nTriangleBase -(size.cy)/2-1);
- pDC->LineTo( nTriangleLeft-4 +(size.cx)-(size.cx)/4 , nTriangleBase+(size.cy)/4);
- pDC->MoveTo(nTriangleLeft-4 +(size.cx)/2, nTriangleBase-(size.cy)/2);
- pDC->LineTo( nTriangleLeft-4 +(size.cx)/4, nTriangleBase+(size.cy)/4 );
- pDC->SelectObject(pOldPen);
- }
- else
- {
- // Draw triangle pointing downwards
- //画顶尖向下的三角形
- CPen *pOldPen = (CPen*) pDC->SelectObject(&penShadow);
- pDC->MoveTo( nTriangleLeft-4 + (size.cx )/ 2 , nTriangleBase - (size.cy)/2);
- pDC->LineTo( nTriangleLeft-4 + (size.cx )/ 2 , nTriangleBase + size.cy+1 );
- pDC->LineTo( nTriangleLeft-4 + (size.cx)- (size.cx)/4, nTriangleBase +(size.cy)/4);
- pDC->MoveTo(nTriangleLeft -4+ (size.cx )/2 , nTriangleBase + size.cy );
- pDC->LineTo(nTriangleLeft-4+(size.cx)/4 , nTriangleBase + (size.cy)/4 );
- pDC->SelectObject(pOldPen);
- }
- if (!bVertical)
- rect.right -= size.cy;
- }
- }
- //以上所有都是画单元格的
- pDC->SetTextColor(TextClr);
- //以下是负责显示文字的
- // We want to see '&' characters so use DT_NOPREFIX
- // 我们想看见'&'字符....
- // GetTextRect(rect);//文字要显示的位置
- //DrawText(pDC->m_hDC, GetText(), -1, rect, GetFormat() | DT_NOPREFIX);
- DrawText(pDC->m_hDC, GetText(), -1, rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER/*DT_LEFT/*GetFormat() | DT_NOPREFIX*/);
- //画文字,DT_SINGLELINE|DT_BOTTOM|DT_RIGHT/*DT_LEFT*/是对齐方式的设定
- //DT_SINGLELINE|DT_VCENTER|DT_RIGHT(垂直居中右对齐)
- //DT_SINGLELINE|DT_CENTER|(水平居中)
- //DT_SINGLELINE|DT_CENTER|DT_VCENTER(垂直水平都居中)
- //这几种组合都要结合DT_SINGLELINE设定才能见效
- pDC->RestoreDC(nSavedDC);//恢复原DC模式(不透明)
- return TRUE;
- }
- void CGridCellBase::operator=(const CGridCellBase& cell)
- {
- if (this == &cell) return;
- SetGrid(cell.GetGrid()); // do first in case of dependencies
- SetText(cell.GetText());
- SetTextClr(cell.GetTextClr());
- SetBackClr(cell.GetBackClr());
- SetFont(cell.IsDefaultFont()? NULL : cell.GetFont());
- }
- // Returns a pointer to a cell that holds default values for this particular type of cell
- CGridCellBase* CGridCellBase::GetDefaultCell() const
- {
- if (GetGrid())//得到表格类的指针
- //返回保存在表格类中的缺省单元格的信息:
- return GetGrid()->GetDefaultCell(IsFixedRow(), IsFixedCol());
- return NULL;
- }