修改和新增单元格内容的对齐方式
This commit is contained in:
@@ -131,6 +131,7 @@ mainForm::mainForm() :LayeredWindow(1000, 750)
|
||||
tableView->SetColumnType(5, ezui::CellType::CheckBox);
|
||||
tableView->SetColumnType(2, ezui::CellType::ComboBox);
|
||||
tableView->SetColumnComboItems(2, { L"默认", L"禁止" , L"验机" });
|
||||
tableView->SetDefaultTextAlign(Align::MiddleCenter);
|
||||
|
||||
//设置列宽
|
||||
std::vector<int> withs = {60, 120, 50, 90, 85, 85, 100, 70, 70, 80, 80, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90};
|
||||
|
||||
@@ -44,10 +44,12 @@ namespace ezui {
|
||||
StrokeStyle BorderStyle = StrokeStyle::None;
|
||||
Color BackColor;
|
||||
Color ForeColor;
|
||||
TextAlign Align = TextAlign::TopLeft;
|
||||
bool HasBorderColor = false;
|
||||
bool HasBorderStyle = false;
|
||||
bool HasBackColor = false;
|
||||
bool HasForeColor = false;
|
||||
bool HasTextAlign = false;
|
||||
|
||||
void SetBorderColor(const Color& color) {
|
||||
BorderColor = color;
|
||||
@@ -65,11 +67,16 @@ namespace ezui {
|
||||
ForeColor = color;
|
||||
HasForeColor = true;
|
||||
}
|
||||
void SetTextAlign(ezui::TextAlign align) {
|
||||
Align = align;
|
||||
HasTextAlign = true;
|
||||
}
|
||||
void Reset() {
|
||||
HasBorderColor = false;
|
||||
HasBorderStyle = false;
|
||||
HasBackColor = false;
|
||||
HasForeColor = false;
|
||||
HasTextAlign = false;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -88,6 +95,7 @@ namespace ezui {
|
||||
CellType Type = CellType::TextBox; // 单元格类型
|
||||
std::vector<UIString> ComboItems; // 下拉选项(仅 ComboBox 类型有效)
|
||||
SortOrder CurrentSort = SortOrder::None; // 当前排序状态
|
||||
TextAlign CellTextAlign = TextAlign::TopLeft; // 该列的默认对齐方式
|
||||
};
|
||||
|
||||
class UI_EXPORT TableView : public Control {
|
||||
@@ -121,6 +129,8 @@ namespace ezui {
|
||||
Color m_cellForeColor = Color::Black;
|
||||
int m_cellFontSize = 14;
|
||||
std::wstring m_cellFontFamily = L"Microsoft YaHei";
|
||||
TextAlign m_cellTextAlign = TextAlign::TopLeft; // 默认对齐方式
|
||||
std::vector<TextAlign> m_rowTextAlign; // 每行的对齐方式(未设置时使用默认值)
|
||||
|
||||
// 表头样式
|
||||
Color m_headerBackColor = Color(0xFFE0E0E0);
|
||||
@@ -429,6 +439,23 @@ namespace ezui {
|
||||
void SetHeaderBackColor(const Color& color);
|
||||
void SetHeaderForeColor(const Color& color);
|
||||
|
||||
// ============ 对齐方式设置 ============
|
||||
|
||||
// 设置默认对齐方式(影响所有未单独设置对齐方式的单元格)
|
||||
void SetDefaultTextAlign(TextAlign align);
|
||||
|
||||
// 设置某列的对齐方式
|
||||
void SetColumnTextAlign(int colIndex, TextAlign align);
|
||||
|
||||
// 设置某行的对齐方式
|
||||
void SetRowTextAlign(int rowIndex, TextAlign align);
|
||||
|
||||
// 设置单个单元格的对齐方式
|
||||
void SetCellTextAlign(int row, int col, TextAlign align);
|
||||
|
||||
// 获取单元格的对齐方式(考虑优先级:单元格 > 行 > 列 > 默认)
|
||||
TextAlign GetCellTextAlign(int row, int col) const;
|
||||
|
||||
// ============ 鼠标悬停信息 ============
|
||||
|
||||
// 获取鼠标当前悬停的行号(-1表示表头或未悬停在任何行)
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -44,10 +44,12 @@ namespace ezui {
|
||||
StrokeStyle BorderStyle = StrokeStyle::None;
|
||||
Color BackColor;
|
||||
Color ForeColor;
|
||||
TextAlign Align = TextAlign::TopLeft;
|
||||
bool HasBorderColor = false;
|
||||
bool HasBorderStyle = false;
|
||||
bool HasBackColor = false;
|
||||
bool HasForeColor = false;
|
||||
bool HasTextAlign = false;
|
||||
|
||||
void SetBorderColor(const Color& color) {
|
||||
BorderColor = color;
|
||||
@@ -65,11 +67,16 @@ namespace ezui {
|
||||
ForeColor = color;
|
||||
HasForeColor = true;
|
||||
}
|
||||
void SetTextAlign(ezui::TextAlign align) {
|
||||
Align = align;
|
||||
HasTextAlign = true;
|
||||
}
|
||||
void Reset() {
|
||||
HasBorderColor = false;
|
||||
HasBorderStyle = false;
|
||||
HasBackColor = false;
|
||||
HasForeColor = false;
|
||||
HasTextAlign = false;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -88,6 +95,7 @@ namespace ezui {
|
||||
CellType Type = CellType::TextBox; // 单元格类型
|
||||
std::vector<UIString> ComboItems; // 下拉选项(仅 ComboBox 类型有效)
|
||||
SortOrder CurrentSort = SortOrder::None; // 当前排序状态
|
||||
TextAlign CellTextAlign = TextAlign::TopLeft; // 该列的默认对齐方式
|
||||
};
|
||||
|
||||
class UI_EXPORT TableView : public Control {
|
||||
@@ -121,6 +129,8 @@ namespace ezui {
|
||||
Color m_cellForeColor = Color::Black;
|
||||
int m_cellFontSize = 14;
|
||||
std::wstring m_cellFontFamily = L"Microsoft YaHei";
|
||||
TextAlign m_cellTextAlign = TextAlign::TopLeft; // 默认对齐方式
|
||||
std::vector<TextAlign> m_rowTextAlign; // 每行的对齐方式(未设置时使用默认值)
|
||||
|
||||
// 表头样式
|
||||
Color m_headerBackColor = Color(0xFFE0E0E0);
|
||||
@@ -429,6 +439,23 @@ namespace ezui {
|
||||
void SetHeaderBackColor(const Color& color);
|
||||
void SetHeaderForeColor(const Color& color);
|
||||
|
||||
// ============ 对齐方式设置 ============
|
||||
|
||||
// 设置默认对齐方式(影响所有未单独设置对齐方式的单元格)
|
||||
void SetDefaultTextAlign(TextAlign align);
|
||||
|
||||
// 设置某列的对齐方式
|
||||
void SetColumnTextAlign(int colIndex, TextAlign align);
|
||||
|
||||
// 设置某行的对齐方式
|
||||
void SetRowTextAlign(int rowIndex, TextAlign align);
|
||||
|
||||
// 设置单个单元格的对齐方式
|
||||
void SetCellTextAlign(int row, int col, TextAlign align);
|
||||
|
||||
// 获取单元格的对齐方式(考虑优先级:单元格 > 行 > 列 > 默认)
|
||||
TextAlign GetCellTextAlign(int row, int col) const;
|
||||
|
||||
// ============ 鼠标悬停信息 ============
|
||||
|
||||
// 获取鼠标当前悬停的行号(-1表示表头或未悬停在任何行)
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user