修改和新增单元格内容的对齐方式

This commit is contained in:
睿 安
2026-01-28 23:21:04 +08:00
parent 814f42120c
commit 76be13001f
16 changed files with 134 additions and 2 deletions

View File

@@ -423,13 +423,17 @@ namespace ezui {
g.SetFont(font);
g.SetColor(foreColor);
// 获取对齐方式
TextAlign textAlign = GetCellTextAlign(row, col);
switch (colInfo.Type) {
case CellType::TextBox:
case CellType::ReadOnly: {
// 绘制文本(禁用自动换行,只根据实际换行符换行)
font.Get()->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP);
// 使用单元格实际高度这样Middle和Bottom对齐才能正确显示
TextLayout layout(cellData.Text.unicode(), font,
SizeF(cellRect.Width - 4, EZUI_FLOAT_MAX), TextAlign::TopLeft);
SizeF(cellRect.Width - 4, cellRect.Height - 2), textAlign);
g.DrawTextLayout(layout, PointF(cellRect.X + 2, cellRect.Y + 1));
break;
}
@@ -785,6 +789,7 @@ namespace ezui {
std::vector<std::vector<CellData>> newData(m_data.size());
std::vector<int> newRowHeights(m_rowHeights.size());
std::vector<bool> newRowChecked(m_rowChecked.size());
std::vector<TextAlign> newRowTextAlign(m_rowTextAlign.size());
for (int i = 0; i < (int)indices.size(); ++i) {
int oldIndex = indices[i];
@@ -795,11 +800,15 @@ namespace ezui {
if (oldIndex < (int)m_rowChecked.size()) {
newRowChecked[i] = m_rowChecked[oldIndex];
}
if (oldIndex < (int)m_rowTextAlign.size()) {
newRowTextAlign[i] = m_rowTextAlign[oldIndex];
}
}
m_data = std::move(newData);
m_rowHeights = std::move(newRowHeights);
m_rowChecked = std::move(newRowChecked);
m_rowTextAlign = std::move(newRowTextAlign);
Invalidate();
}
@@ -1245,6 +1254,7 @@ namespace ezui {
std::vector<CellData> newRow(m_columns.size());
m_data.push_back(newRow);
m_rowHeights.push_back(m_defaultRowHeight);
m_rowTextAlign.push_back(m_cellTextAlign); // 添加默认对齐
if (m_firstColumnType == FirstColumnType::CheckBox) {
m_rowChecked.push_back(false);
@@ -1262,6 +1272,14 @@ namespace ezui {
m_data.insert(m_data.begin() + rowIndex, newRow);
m_rowHeights.insert(m_rowHeights.begin() + rowIndex, m_defaultRowHeight);
// 确保rowTextAlign大小与数据一致
while (m_rowTextAlign.size() < m_data.size()) {
m_rowTextAlign.push_back(m_cellTextAlign);
}
if (rowIndex < (int)m_rowTextAlign.size()) {
m_rowTextAlign.insert(m_rowTextAlign.begin() + rowIndex, m_cellTextAlign);
}
if (m_firstColumnType == FirstColumnType::CheckBox) {
m_rowChecked.insert(m_rowChecked.begin() + rowIndex, false);
}
@@ -1275,6 +1293,10 @@ namespace ezui {
m_data.erase(m_data.begin() + rowIndex);
m_rowHeights.erase(m_rowHeights.begin() + rowIndex);
if (rowIndex < (int)m_rowTextAlign.size()) {
m_rowTextAlign.erase(m_rowTextAlign.begin() + rowIndex);
}
if (rowIndex < (int)m_rowChecked.size()) {
m_rowChecked.erase(m_rowChecked.begin() + rowIndex);
}
@@ -1288,6 +1310,7 @@ namespace ezui {
m_data.clear();
m_rowHeights.clear();
m_rowChecked.clear();
m_rowTextAlign.clear();
m_headerSelectAll = false;
m_scrollOffsetX = 0;
m_scrollOffsetY = 0;
@@ -1631,6 +1654,60 @@ namespace ezui {
Invalidate();
}
void TableView::SetDefaultTextAlign(TextAlign align) {
m_cellTextAlign = align;
Invalidate();
}
void TableView::SetColumnTextAlign(int colIndex, TextAlign align) {
if (colIndex >= 0 && colIndex < (int)m_columns.size()) {
m_columns[colIndex].CellTextAlign = align;
Invalidate();
}
}
void TableView::SetRowTextAlign(int rowIndex, TextAlign align) {
// 自动扩展
while (rowIndex >= (int)m_rowTextAlign.size()) {
m_rowTextAlign.push_back(m_cellTextAlign); // 使用默认值
}
if (rowIndex >= 0 && rowIndex < (int)m_rowTextAlign.size()) {
m_rowTextAlign[rowIndex] = align;
Invalidate();
}
}
void TableView::SetCellTextAlign(int row, int col, TextAlign align) {
if (row >= 0 && row < (int)m_data.size() &&
col >= 0 && col < (int)m_data[row].size()) {
m_data[row][col].Style.SetTextAlign(align);
Invalidate();
}
}
TextAlign TableView::GetCellTextAlign(int row, int col) const {
// 优先级:单元格 > 行 > 列 > 默认
if (row >= 0 && row < (int)m_data.size() &&
col >= 0 && col < (int)m_data[row].size()) {
// 单元格级别
if (m_data[row][col].Style.HasTextAlign) {
return m_data[row][col].Style.Align;
}
}
// 行级别(只有当行索引在 m_rowTextAlign 范围内才使用)
if (row >= 0 && row < (int)m_rowTextAlign.size()) {
// 这里应该检查是否真的设置过,但为了兼容性,我们假设设置过就使用
// 如果数组已扩展,就使用该行的对齐方式
return m_rowTextAlign[row];
}
// 列级别
if (col >= 0 && col < (int)m_columns.size()) {
return m_columns[col].CellTextAlign;
}
// 默认值
return m_cellTextAlign;
}
int TableView::GetHoverRow() const {
return m_hoverRow;
}