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

📄 xtable.cpp

📁 一个简单方便的表格控件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// XTable.cpp : implementation file
//

#include "stdafx.h"
#include "Table.h"
#include "XTable.h"

#define XTABLE_CLASSNAME    _T("XTableCtrl")  // Window class name

// XTable

IMPLEMENT_DYNAMIC(XTable, CWnd)

XTable::XTable(int rows, int cols)
{
	RegisterWindowClass ();

	cells = new XCell [MAX_ROWS * MAX_COLS];
	rowHeight = new int [MAX_ROWS];
	colWidth = new int [MAX_COLS];
	
	defaultHeight = 20;
	defaultwidth = 80;
	
	this->rows = 0;
	this->cols = 0;
	SetRows (rows);
	SetCols (cols);	

	focusRow = 0;;
	focusCol = 0;
}

XTable::~XTable()
{
	if(cells)
	{
		delete [] cells;
		cells = NULL;
	}

	if (rowHeight)
	{
		delete [] rowHeight;
		rows = NULL;
	}

	if (colWidth)
	{
		delete [] colWidth;
		cols = NULL;
	}
}

// creates the control - use like any other window create control
BOOL XTable::Create(const RECT& rect, CWnd* pParentWnd, UINT nID, DWORD dwStyle)
{
    ASSERT(pParentWnd->GetSafeHwnd());

    if (!CWnd::Create(XTABLE_CLASSNAME, NULL, dwStyle, rect, pParentWnd, nID))
        return FALSE;

    return TRUE;
}

int XTable::SetRows(int rows)
{
	if (rows <0 || rows >= MAX_ROWS) return -1;
	
	if (this->rows == rows) return 0;

	if (this->rows < rows)
	{
		for (int i = this->rows; i < rows; i++)
		{
			for(int j = 0; j < this->cols; j++)
			{
				cells [i * MAX_COLS + j] = defaultCell;
				cells [i * MAX_COLS + j].SetSpan(1,1);				
			}

			rowHeight [i] = defaultHeight;
		}
	}
	else
	{
		for (int i = this->rows; i > rows; i--)
		{
			for (int j = 0; j < this->cols; j++)
			{
				cells [i * MAX_COLS + j].SetSpan(0,10);				
			}
		}
	}

	this->rows = rows;

	return 0;    
}

int XTable::GetRows()
{
	return rows;
}

int XTable::SetCols(int cols)
{
	if (cols < 0 || cols >= MAX_COLS) return -1;

	if (this->cols == cols) return 0;

	if(this->cols < cols)
	{
		for (int i = this->cols; i < cols; i ++)
		{
			for (int j = 0; j < rows; j ++)
			{
				cells[j*MAX_COLS+i] = defaultCell;
				cells[j*MAX_COLS+i].SetSpan(1,1);
			}

			colWidth [i] = defaultwidth;
		}
	}
	else
	{
		for (int i = this->cols; i > cols; i --)
		{
			for (int j = 0; j < rows; j ++)
			{
				cells[j*MAX_COLS+i].SetSpan(1,1);
			}
		}
	}
	
	this->cols = cols;

	return 0;
}

int XTable::GetCols()
{
	return cols;
}

int XTable::JoinCells (int startRow, int startCol, int endRow, int endCol)
{
	if (startRow < 0 || startRow >= rows) return -1;
	if (endRow < 0 || startRow >= rows) return -1;

	if (startCol < 0 || startCol >= cols) return -1;
	if (endCol < 0 || endCol >= cols) return -1;

	if (startRow > endRow || startCol > endCol) return -1;

	for (int i = startRow; i <= endRow; i++)
	{
		for (int j = startCol; j <=endCol;  j++)
		{
			cells [i * MAX_COLS + j].SetSpan(0,0);
		}
	}
	
	cells [startRow * MAX_COLS + startCol].SetSpan(endRow - startRow+1, endCol - startCol+1);
	
	return 0;
}

int XTable::UnjoinCells (int row, int col)
{
	if (row < 0 || row >= this->rows) return -1;
	if (col < 0 || col >= this->cols) return -1;

	if (cells [row * MAX_COLS + col].rowSpan <= 1 && cells [row * MAX_COLS + col].colSpan <= 1 ) return -1;

	for (int i = row; i <= row + cells [row * MAX_COLS + col].rowSpan; i++)
	{
		for (int j = col; j <= cells [row * MAX_COLS + col].colSpan; j++)
		{
			cells[i*MAX_COLS+j] = defaultCell;

			cells [i * MAX_COLS + j].SetSpan(1,1);
		}
	}

	return 0;
}

int XTable::SetRowHeight(int row, int height)
{
	rowHeight [row] = height;

	return 0;
}

int XTable::GetRowHeight(int row)
{
	return rowHeight [row];
}

int XTable::SetColWidth(int col, int width)
{
	colWidth [col] = width;

	return 0;
}

int XTable::GetColWidth(int col)
{
	return colWidth [col];
}

int XTable::GetRowsHeight(int startRow, int endRow)
{
	if (startRow < 0 || startRow >= rows) return -1;
	if (endRow < 0 || endRow >= rows) return -1;
	if (startRow > endRow) return -1;

	int height = 0;
	for (int i = startRow; i <= endRow; i++)
	{
		height += rowHeight[i];
	}

	return height;
}

int XTable::GetColsWidth(int startCol, int endCol)
{
	if (startCol < 0 || startCol >= cols) return -1;
	if (endCol < 0 || endCol >= cols) return -1;
	if (startCol > endCol) return -1;

	int width = 0;
	for (int i = startCol; i <= endCol; i++)
	{
		width += colWidth[i];
	}

	return width;
}

int XTable::SetCells(int row, int col, XCell& cell)
{
	cells[row * MAX_COLS + col] = cell;

	return 0;
}

XCell* XTable::GetCells(int row, int col)
{
	return &cells [row * MAX_COLS + col];
}

int XTable::SetText(int row, int col, CString str)
{

	XCell* cell = GetCells (row, col);
	if (!cell) return -1;

	return cell->SetText (str);
}

CString XTable::GetText(int row, int col)
{
	return	cells[row * MAX_COLS + col].GetText();
}

int XTable::SetTextColor(int row, int col, COLORREF color)
{
	cells[row * MAX_COLS + col].SetTextColor(color);

	return 0;
}

COLORREF XTable::GetTextColor(int row, int col)
{
	return 	cells[row * MAX_COLS + col].GetTextColor();
}

int XTable::SetTextFont(int row, int col, CFont& font)
{
	cells[row * MAX_COLS + col].SetTextFont(&font);

	return 0;

}
CFont* XTable::GetTextFont(int row, int col)
{
	return	cells[row * MAX_COLS + col].GetTextFont();
}

int XTable::SetTextFontSize(int row, int col, int size)
{
	cells[row * MAX_COLS + col].SetTextFontSize(size);

	return 0;
}

int XTable::GetTextFontSize(int row, int col)
{
	return 	cells[row * MAX_COLS + col].GetTextFontSize();
}

int XTable::SetOverlap (int row, int col, bool enable)
{
	XCell* cell = GetCells (row, col);
	if (!cell) return -1;

	return cell->SetOverlap (enable);
}

bool XTable::GetOverlap (int row, int col)
{
	XCell* cell = GetCells (row, col);
	if (!cell) return false;

	return cell->GetOverlap ();
}

int XTable::SetAlignment (int row, int col, int align)
{
	XCell* cell = GetCells (row, col);
	if (!cell) return -1;

	return cell->SetAlignment (align);
}

int XTable::GetAlignment (int row, int col)
{
	XCell* cell = GetCells (row, col);
	if (!cell) return -1;

	return cell->GetAlignment ();
}

int XTable::SetWordbreak (int row, int col, bool wordbreak)
{
	XCell* cell = GetCells (row, col);
	if (!cell) return -1;

	return cell->SetWordbreak (wordbreak);
}

bool XTable::GetWordbreak (int row, int col)
{
	XCell* cell = GetCells (row, col);
	if (!cell) return false;

	return cell->GetWordbreak ();
}

int XTable::SetBackColor(int row, int col, COLORREF color)
{
	cells[row * MAX_COLS + col].SetBackColor(color);

	return 0;
}

COLORREF XTable::GetBackColor(int row, int col)
{
	return 	cells[row * MAX_COLS + col].GetBackColor();
}

int XTable::SetBackMode(int row, int col, int mode)
{
	cells[row * MAX_COLS + col].SetBackMode(mode);

	return 0;
}

int XTable::GetBackMode(int row, int col)
{
	return 	cells[row * MAX_COLS + col].GetBackMode();
}

RECT XTable::GetRect(int row, int col)
{
	RECT rect;
	int rowSpan = GetCells(row, col)->rowSpan;
	int colSpan = GetCells(row, col)->colSpan;
	rect.top = GetRowsHeight(0, row-1);
	rect.left = GetColsWidth(0, col-1);

	rect.bottom = rect.top + GetRowsHeight (row, row + rowSpan-1);
	rect.right = rect.left + GetColsWidth (col, col + colSpan-1);

	return rect;
}

bool XTable::HitTest (CPoint point, int& row, int& col)
{

⌨️ 快捷键说明

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