diff --git a/demo/Adminstor/Adminstor/mainForm.cpp b/demo/Adminstor/Adminstor/mainForm.cpp index 480593a..703cdbc 100644 --- a/demo/Adminstor/Adminstor/mainForm.cpp +++ b/demo/Adminstor/Adminstor/mainForm.cpp @@ -131,7 +131,8 @@ 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 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}; for(auto i=0;i< withs.size();i++) diff --git a/demo/Adminstor/ThirdParty/EzUI/include/EzUI/TableView.h b/demo/Adminstor/ThirdParty/EzUI/include/EzUI/TableView.h index bb1b8d4..791af35 100644 --- a/demo/Adminstor/ThirdParty/EzUI/include/EzUI/TableView.h +++ b/demo/Adminstor/ThirdParty/EzUI/include/EzUI/TableView.h @@ -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 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 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表示表头或未悬停在任何行) diff --git a/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Debug_Win32.lib b/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Debug_Win32.lib index 76a9f9a..ae12bcd 100644 Binary files a/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Debug_Win32.lib and b/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Debug_Win32.lib differ diff --git a/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Debug_Win32.pdb b/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Debug_Win32.pdb index f69d8cc..dca6579 100644 Binary files a/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Debug_Win32.pdb and b/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Debug_Win32.pdb differ diff --git a/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Debug_x64.lib b/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Debug_x64.lib index 83db7c0..e703cf0 100644 Binary files a/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Debug_x64.lib and b/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Debug_x64.lib differ diff --git a/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Debug_x64.pdb b/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Debug_x64.pdb index f16d58e..ae3dbc0 100644 Binary files a/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Debug_x64.pdb and b/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Debug_x64.pdb differ diff --git a/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Release_Win32.lib b/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Release_Win32.lib index 376e304..4a8d335 100644 Binary files a/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Release_Win32.lib and b/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Release_Win32.lib differ diff --git a/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Release_x64.lib b/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Release_x64.lib index 86b5d01..a9a11f6 100644 Binary files a/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Release_x64.lib and b/demo/Adminstor/ThirdParty/EzUI/lib/EzUI_Release_x64.lib differ diff --git a/include/EzUI/TableView.h b/include/EzUI/TableView.h index bb1b8d4..791af35 100644 --- a/include/EzUI/TableView.h +++ b/include/EzUI/TableView.h @@ -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 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 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表示表头或未悬停在任何行) diff --git a/lib/EzUI_Debug_Win32.lib b/lib/EzUI_Debug_Win32.lib index 76a9f9a..ae12bcd 100644 Binary files a/lib/EzUI_Debug_Win32.lib and b/lib/EzUI_Debug_Win32.lib differ diff --git a/lib/EzUI_Debug_Win32.pdb b/lib/EzUI_Debug_Win32.pdb index f69d8cc..dca6579 100644 Binary files a/lib/EzUI_Debug_Win32.pdb and b/lib/EzUI_Debug_Win32.pdb differ diff --git a/lib/EzUI_Debug_x64.lib b/lib/EzUI_Debug_x64.lib index 83db7c0..e703cf0 100644 Binary files a/lib/EzUI_Debug_x64.lib and b/lib/EzUI_Debug_x64.lib differ diff --git a/lib/EzUI_Debug_x64.pdb b/lib/EzUI_Debug_x64.pdb index f16d58e..ae3dbc0 100644 Binary files a/lib/EzUI_Debug_x64.pdb and b/lib/EzUI_Debug_x64.pdb differ diff --git a/lib/EzUI_Release_Win32.lib b/lib/EzUI_Release_Win32.lib index 376e304..4a8d335 100644 Binary files a/lib/EzUI_Release_Win32.lib and b/lib/EzUI_Release_Win32.lib differ diff --git a/lib/EzUI_Release_x64.lib b/lib/EzUI_Release_x64.lib index 86b5d01..a9a11f6 100644 Binary files a/lib/EzUI_Release_x64.lib and b/lib/EzUI_Release_x64.lib differ diff --git a/sources/TableView.cpp b/sources/TableView.cpp index 21e98c0..3ccbe26 100644 --- a/sources/TableView.cpp +++ b/sources/TableView.cpp @@ -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> newData(m_data.size()); std::vector newRowHeights(m_rowHeights.size()); std::vector newRowChecked(m_rowChecked.size()); + std::vector 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 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; }