优化编辑框控件的多行编辑和显示;

优化表格控件单元格的多行编辑和显示效果
This commit is contained in:
睿 安
2026-01-28 22:27:21 +08:00
parent 4fe4749826
commit 814f42120c
18 changed files with 78 additions and 43 deletions

View File

@@ -33,7 +33,8 @@ namespace ezui {
// 创建编辑控件(初始隐藏)
m_editBox = new TextBox();
m_editBox->SetVisible(false);
m_editBox->SetMultiLine(true);
// 设置为不自动换行但允许手动换行Shift+Enter
m_editBox->SetMultiLine(false, true);
// 为编辑框设置默认字体样式
m_editBox->Style.FontSize = m_cellFontSize;
m_editBox->Style.FontFamily = L"Microsoft YaHei";
@@ -45,6 +46,16 @@ namespace ezui {
if (m_editRow < (int)m_data.size() && m_editCol < (int)m_data[m_editRow].size()) {
m_data[m_editRow][m_editCol].Text = text;
UpdateRowHeight(m_editRow);
// 同步更新编辑框的高度以匹配新的行高
if (m_editCol < (int)m_columns.size() && m_editRow < (int)m_rowHeights.size()) {
int x = GetColumnX(m_editCol);
int y = GetRowY(m_editRow);
int width = m_columns[m_editCol].Width;
int height = m_rowHeights[m_editRow]; // 使用更新后的行高
m_editBox->SetRect(Rect(x, y, width, height));
}
if (CellValueChanged) {
CellValueChanged(m_editRow, m_editCol, text);
}
@@ -415,9 +426,10 @@ namespace ezui {
switch (colInfo.Type) {
case CellType::TextBox:
case CellType::ReadOnly: {
// 绘制文本
// 绘制文本(禁用自动换行,只根据实际换行符换行)
font.Get()->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP);
TextLayout layout(cellData.Text.unicode(), font,
SizeF(cellRect.Width - 4, cellRect.Height - 2), TextAlign::MiddleLeft);
SizeF(cellRect.Width - 4, EZUI_FLOAT_MAX), TextAlign::TopLeft);
g.DrawTextLayout(layout, PointF(cellRect.X + 2, cellRect.Y + 1));
break;
}
@@ -644,13 +656,15 @@ namespace ezui {
return 1;
}
Font font(m_cellFontFamily, m_cellFontSize);
TextLayout layout(text.unicode(), font, SizeF(width, EZUI_FLOAT_MAX));
Size box = layout.GetFontBox();
int fontHeight = m_cellFontSize + 4;
int lines = (box.Height + fontHeight - 1) / fontHeight;
return (std::max)(1, lines);
// 由于单元格设置为不自动换行,只根据实际换行符计算行数
std::wstring wtext = text.unicode();
int lines = 1;
for (wchar_t c : wtext) {
if (c == L'\n') {
lines++;
}
}
return lines;
}
void TableView::RefreshScrollBars() {