?? gridcellbase.cpp
字號:
// 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, %d\n", 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;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -