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

📄 xcell.cpp

📁 一个简单方便的表格控件
💻 CPP
字号:
#include "StdAfx.h"
#include "xcell.h"

XCell::XCell()
{
	rowSpan = 0;
	colSpan = 0;

	backColor = RGB (255,255,255);
	textColor = RGB (0,0,0);
	borderColor = RGB (0xDC,0xDC,0xDC);

	borderStyle = 0;

	textFontSize = 14;
	textFontFace = "Arial";
	textFont = NULL;

	backMode = TRANSPARENT;
	format = 0;
	overlap = false;

	leftMargin = 4;
	rightMargin = 4;
}

XCell::~XCell()
{
}

XCell* XCell::operator = (XCell& cell)
{
	rowSpan = cell.rowSpan;
	colSpan = cell.colSpan;

	backColor = cell.backColor;
	textColor = cell.textColor;
	backMode = cell.backColor;

	borderColor = cell.borderColor;
	borderStyle = cell.borderStyle;

	if (textFont)
	{
		delete textFont;
		textFont = NULL;
	}

	if (cell.textFont)
	{
		LOGFONT logFont;
		cell.textFont->GetLogFont (&logFont);

		textFont = new CFont;
		textFont->CreateFontIndirect (&logFont);
	}
	
	format = cell.format;

	leftMargin = cell.leftMargin;
	rightMargin = cell.rightMargin;

	return this;
}

int XCell::SetSpan(int rows, int cols)
{
	rowSpan = rows;
	colSpan = cols;

	return 0;
}

int XCell::SetText(CString str)
{
	text = str;

	return 0;
}

CString XCell::GetText()
{
	return text;
}

int XCell::SetTextColor(COLORREF color)
{
	textColor = color;

	return 0;
}

COLORREF XCell::GetTextColor()
{
	return textColor;
}

int XCell::SetAlignment (int align)
{
	if ( (align & ~ALIGN_MASK) != 0) return -1;

	format = (format & ~ALIGN_MASK) | align;

	return 0;
}

int XCell::GetAlignment ()
{
	return format & ALIGN_MASK;
}

int XCell::SetFormat (int format)
{
	this->format = format;

	return 0;
}

int XCell::GetFormat ()
{
	return format;
}

int XCell::SetSingleLine (bool enable)
{
	if (enable)
		format |= DT_SINGLELINE;
	else
		format &= ~DT_SINGLELINE;

	return 0;
}

bool XCell::GetSingleLine ()
{
	return (format & DT_SINGLELINE) != 0;
}

int XCell::SetWordbreak (bool enable)
{
	if (enable)
		format |= DT_WORDBREAK;
	else
		format &= ~DT_WORDBREAK;

	return 0;
}

bool XCell::GetWordbreak ()
{
	return (format & DT_WORDBREAK) != 0;
}

int XCell::SetEllipsis (bool enable)
{
	if (enable)
		format |= DT_END_ELLIPSIS;
	else
		format &= ~DT_END_ELLIPSIS;

	return 0;
}

bool XCell::GetEllipsis ()
{
	return (format & DT_END_ELLIPSIS) != 0;
}

int XCell::SetLeftMargin (int pixels)
{
	leftMargin = pixels;

	return 0;
}

int XCell::GetLeftMargin ()
{
	return leftMargin;
}

int XCell::SetRightMargin (int pixels)
{
	rightMargin = pixels;

	return 0;
}

int XCell::GetRightMargin ()
{
	return rightMargin;
}



int XCell::SetLabel (CString str)
{
	label = str;

	return 0;
}

CString XCell::GetLabel ()
{
	return label;
}

int XCell::SetOverlap (bool enable)
{
	overlap = enable;

	return 0;
}

bool XCell::GetOverlap ()
{
	return overlap;
}

int XCell::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* XCell::GetTextFont()
{
	return textFont;
}

int XCell::SetTextFontSize(int size)
{
	textFontSize = size;

	return 0;
}

int XCell::GetTextFontSize()
{
	return textFontSize;
}



int XCell::SetLabelColor(COLORREF color)
{
	labelColor = color;

	return 0;
}

COLORREF XCell::GetLabelColor()
{
	return labelColor;
}

int XCell::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* XCell::GetLabelFont()
{
	return labelFont;
}

int XCell::SetLabelFontSize(int size)
{
	labelFontSize = size;

	return 0;
}

int XCell::GetLabelFontSize()
{
	return labelFontSize;
}

int XCell::SetBackMode(int mode)
{
	backMode = mode;

	return 0;
}

int XCell::GetBackMode()
{
	return backMode;
}

int XCell::SetBackColor(COLORREF color)
{
	backColor = color;

	return 0;
}

COLORREF XCell::GetBackColor()
{
	return backColor;
}

int XCell::SetBorderSyle(int syle)
{
	borderStyle = syle;

	return 0;
}

int XCell::GetBorderSyle()
{
	return borderStyle;
}



int XCell::DrawText(CDC* pDC, RECT rect)
{
	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 XCell::DrawLabel(CDC* pDC, RECT rect)
{
	return 0;
}

int XCell::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 XCell::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 XCell::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 XCell::DrawBackground (CDC* pDC, RECT rect)
{
	CBrush bkBrush (backColor);

	pDC->FillRect (&rect, &bkBrush);

	return 0;
}

int XCell::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;
}

⌨️ 快捷键说明

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