www.pudn.com > TableTest.rar > XCell.cpp
// XCell.cpp: implementation of the CXCell class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "TableTest.h"
#include "XCell.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CXCell::CXCell(CXTable* parent)
{
rowSpan = 0;
colSpan = 0;
backColor = RGB (255,255,255);
borderColor = RGB (0xDC,0xDC,0xDC);
borderStyle = 0;
text = "";
textFontSize = 14;
textFontFace = "Arial";
textFont = NULL;
textColor = RGB (0,0,0);
label = "";
labelFontSize = 14;
labelFontFace = "Arial";
labelFont = NULL;
labelColor = RGB (0,0,0);
backMode = TRANSPARENT;
format = 0;
overlap = false;
leftMargin = 4;
rightMargin = 4;
}
CXCell::~CXCell()
{
delete textFont;
delete labelFont;
}
int CXCell::SetSpan(int rows, int cols)
{
rowSpan = rows;
colSpan = cols;
return 0;
}
int CXCell::SetText(CString str)
{
text = str;
return 0;
}
CString CXCell::GetText()
{
return text;
}
int CXCell::SetTextColor(COLORREF color)
{
textColor = color;
return 0;
}
COLORREF CXCell::GetTextColor()
{
return textColor;
}
int CXCell::SetAlignment (int align)
{
if ( (align & ~ALIGN_MASK) != 0) return -1;
format = (format & ~ALIGN_MASK) | align;
return 0;
}
int CXCell::GetAlignment ()
{
return format & ALIGN_MASK;
}
int CXCell::SetFormat (int format)
{
this->format = format;
return 0;
}
int CXCell::GetFormat ()
{
return format;
}
int CXCell::SetSingleLine (bool enable)
{
if (enable)
format |= DT_SINGLELINE;
else
format &= ~DT_SINGLELINE;
return 0;
}
bool CXCell::GetSingleLine ()
{
return (format & DT_SINGLELINE) != 0;
}
int CXCell::SetWordbreak (bool enable)
{
if (enable)
format |= DT_WORDBREAK;
else
format &= ~DT_WORDBREAK;
return 0;
}
bool CXCell::GetWordbreak ()
{
return (format & DT_WORDBREAK) != 0;
}
int CXCell::SetEllipsis (bool enable)
{
if (enable)
format |= DT_END_ELLIPSIS;
else
format &= ~DT_END_ELLIPSIS;
return 0;
}
bool CXCell::GetEllipsis ()
{
return (format & DT_END_ELLIPSIS) != 0;
}
int CXCell::SetLeftMargin (int pixels)
{
leftMargin = pixels;
return 0;
}
int CXCell::GetLeftMargin ()
{
return leftMargin;
}
int CXCell::SetRightMargin (int pixels)
{
rightMargin = pixels;
return 0;
}
int CXCell::GetRightMargin ()
{
return rightMargin;
}
int CXCell::SetLabel (CString str)
{
label = str;
return 0;
}
CString CXCell::GetLabel ()
{
return label;
}
int CXCell::SetOverlap (bool enable)
{
overlap = enable;
return 0;
}
bool CXCell::GetOverlap ()
{
return overlap;
}
int CXCell::SetTextFont(CFont* font)
{
LOGFONT longfont;
if (!font->GetLogFont(&longfont))
return -1;
if (textFont)
{
delete textFont;
textFont = NULL;
}
textFont = new CFont;
textFont->CreateFontIndirect(&longfont);
return 0;
}
CFont* CXCell::GetTextFont()
{
return textFont;
}
int CXCell::SetTextFontSize(int size)
{
textFontSize = size;
return 0;
}
int CXCell::GetTextFontSize()
{
return textFontSize;
}
int CXCell::SetLabelColor(COLORREF color)
{
labelColor = color;
return 0;
}
COLORREF CXCell::GetLabelColor()
{
return labelColor;
}
int CXCell::SetLabelFont(CFont* font)
{
LOGFONT logfont;
if (!font->GetLogFont(&logfont))
return -1;
if (labelFont)
{
labelFont->DeleteObject();
labelFont = NULL;
}
labelFont = new CFont;
labelFont->CreateFontIndirect(&logfont);
return 0;
}
CFont* CXCell::GetLabelFont()
{
return labelFont;
}
int CXCell::SetLabelFontSize(int size)
{
labelFontSize = size;
return 0;
}
int CXCell::GetLabelFontSize()
{
return labelFontSize;
}
int CXCell::SetBackMode(int mode)
{
backMode = mode;
return 0;
}
int CXCell::GetBackMode()
{
return backMode;
}
int CXCell::SetBackColor(COLORREF color)
{
backColor = color;
return 0;
}
COLORREF CXCell::GetBackColor()
{
return backColor;
}
int CXCell::SetBorderSyle(int syle)
{
borderStyle = syle;
return 0;
}
int CXCell::GetBorderSyle()
{
return borderStyle;
}
int CXCell::DrawText(CDC* pDC, RECT rect)
{
if (text.IsEmpty()) return 0;
COLORREF oldTextColor = pDC->SetTextColor (textColor);
int oldBkMode = pDC->SetBkMode (backMode);
CFont* oldFont;
CFont tempFont;
if (textFont)
oldFont = pDC->SelectObject (textFont);
else
{
tempFont.CreateFont (textFontSize,0,0, 0, FW_NORMAL, FALSE, FALSE, 0,
ANSI_CHARSET,OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, textFontFace);
oldFont = pDC->SelectObject (&tempFont);
}
if (leftMargin)
rect.left += leftMargin;
if (rightMargin)
rect.right -= rightMargin;
pDC->DrawText (text, &rect, format);
pDC->SetTextColor(oldTextColor);
pDC->SetBkMode (oldBkMode);
pDC->SelectObject (oldFont);
tempFont.DeleteObject ();
return 0;
}
int CXCell::DrawLabel(CDC* pDC, RECT rect)
{
return 0;
}
int CXCell::Draw(CDC* pDC, RECT rect)
{
DrawBorder (pDC, rect);
RECT rect1;
rect1.left = rect.left+1;
rect1.top = rect.top+1;
rect1.right = rect.right;
rect1.bottom = rect.bottom;
DrawBackground (pDC, rect1);
DrawText (pDC, rect1);
return 0;
}
int CXCell::DrawBorder(CDC* pDC, RECT rect)
{
CPen linePen (PS_SOLID, 1, borderColor);
CPen* pOldPen = pDC->SelectObject(&linePen);
pDC->MoveTo (rect.left, rect.top);
pDC->LineTo (rect.right, rect.top);
pDC->LineTo (rect.right, rect.bottom);
pDC->LineTo (rect.left, rect.bottom);
pDC->LineTo (rect.left, rect.top);
pDC->SelectObject(pOldPen);
return 0;
}
int CXCell::DrawHitBorder (CDC* pDC, RECT rect, COLORREF color)
{
CPen pen (PS_SOLID, 3, color);
CPen* oldPen = pDC->SelectObject(&pen);
pDC->MoveTo (rect.left, rect.top);
pDC->LineTo (rect.right, rect.top);
pDC->LineTo (rect.right, rect.bottom);
pDC->LineTo (rect.left, rect.bottom);
pDC->LineTo (rect.left, rect.top);
pDC->SelectObject(oldPen);
return 0;
}
int CXCell::DrawBackground (CDC* pDC, RECT rect)
{
CBrush bkBrush (backColor);
pDC->FillRect (&rect, &bkBrush);
return 0;
}
int CXCell::CalcTextRect (CDC* pDC, RECT* rect)
{
CFont* oldFont;
CFont tempFont;
if (textFont)
oldFont = pDC->SelectObject (textFont);
else
{
tempFont.CreateFont (textFontSize,0,0, 0, FW_NORMAL, FALSE, FALSE, 0,
ANSI_CHARSET,OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, textFontFace);
oldFont = pDC->SelectObject (&tempFont);
}
if (leftMargin)
rect->left += leftMargin;
pDC->DrawText (text, rect, format | DT_CALCRECT);
pDC->SelectObject (oldFont);
tempFont.DeleteObject ();
return 0;
}