新增:单元格编辑完成回调;单击第一列选中整行

This commit is contained in:
睿 安
2026-01-28 16:51:51 +08:00
parent abcfc78596
commit 2ff880c267
17 changed files with 149 additions and 122 deletions

View File

@@ -7,6 +7,28 @@ void mainForm::OnNotify(Control* sender, EventArgs& args)
UIString btnName = sender->Name; // 控件id UIString btnName = sender->Name; // 控件id
Event eventType = args.EventType; // 事件类型 Event eventType = args.EventType; // 事件类型
// 针对管理界面的表格控件
if (btnName == "tableViewAdmin") {
switch (eventType)
{
case 8: break;
case Event::OnMouseUp://鼠标抬起
{
TableView* tableView = (TableView*)FindControl("tableViewAdmin"); //获取表格控件
int pRow = tableView->GetHoverRow(); //当前行号
int pCol = tableView->GetHoverCol(); //当前列号
std::cout << "当前行列号: (" << pRow << ", " << pCol << ")\n";
UIString celContent = tableView->GetData(pRow, pCol);
std::cout << "单元格内容: " << celContent.ansi() << std::endl;
}
break;
default:
std::cout << "表格事件:" << (long long)eventType << std::endl;
break;
}
}
switch (eventType) switch (eventType)
{ {
case ezui::OnMouseDown: //鼠标按下 case ezui::OnMouseDown: //鼠标按下
@@ -57,12 +79,14 @@ void mainForm::OnNotify(Control* sender, EventArgs& args)
TableView* tableView = (TableView*)FindControl("tableViewAdmin"); //获取表格控件 TableView* tableView = (TableView*)FindControl("tableViewAdmin"); //获取表格控件
if (tableView){ if (tableView){
tableView->SetColumnType(4, ezui::CellType::CheckBox); int rowCount = tableView->GetRowCount(); //总行数
tableView->SetColumnType(5, ezui::CellType::ComboBox); // 表格增加一行数据
tableView->SetColumnComboItems(5, {L"选择1", L"选择2" , L"选择3" }); tableView->InsertRow(rowCount);
tableView->InsertRow(1); tableView->SetRowData(rowCount, { L"uid" + std::to_wstring(rowCount), L"192.168.200.131" , L"默认"});
tableView->SetRowData(tableView->GetRowCount() - 1, {L"uid1", L"192.168.200.131"}); // 获取表格指定位置数据
} }
} }
} }
break; break;
@@ -99,6 +123,22 @@ mainForm::mainForm() :LayeredWindow(1000, 750)
SetMiniSize(Size(600, 450)); // 设置最小尺寸 SetMiniSize(Size(600, 450)); // 设置最小尺寸
umg.LoadXml("res/mainForm.htm");//加载xml里面的控件与样式 umg.LoadXml("res/mainForm.htm");//加载xml里面的控件与样式
umg.SetupUI(this); umg.SetupUI(this);
AllocConsole();
FILE* fp = nullptr;
freopen_s(&fp, "CONOUT$", "w", stdout);
freopen_s(&fp, "CONOUT$", "w", stderr);
// 初始化设置表格各项属性
TableView* tableView = (TableView*)FindControl("tableViewAdmin"); //获取表格控件
if (tableView) {
tableView->SelectedRowBackColor = Color(200, 230, 255); // 设置选中行背景色
tableView->SetColumnType(5, ezui::CellType::CheckBox);
tableView->SetColumnType(2, ezui::CellType::ComboBox);
tableView->SetColumnComboItems(2, { L"默认", L"禁止" , L"验机" });
//设置列宽
//std::vector<int> withs = {80, 100};
}
} }
mainForm::~mainForm() mainForm::~mainForm()

View File

@@ -159,6 +159,12 @@ namespace ezui {
int m_lastClickRow = -1; int m_lastClickRow = -1;
int m_lastClickCol = -1; int m_lastClickCol = -1;
// 选中行非CheckBox模式下通过单击第一列选中的行
int m_selectedRow = -1;
// 编辑前的原始值(用于编辑完成回调)
UIString m_editOriginalValue;
private: private:
void Init(); void Init();
@@ -236,13 +242,17 @@ namespace ezui {
virtual const Size& GetContentSize() override; virtual const Size& GetContentSize() override;
public: public:
// 选中行背景色当第一列为CheckBox且被选中时使用 // 选中行背景色当第一列为CheckBox且被选中时使用,或者单击选中行时使用
Color SelectedRowBackColor = Color(0xFFADD8E6); // 浅蓝色 Color SelectedRowBackColor = Color(0xFFADD8E6); // 浅蓝色
// 单元格内容变化回调 // 单元格内容变化回调(内容变化时立即触发)
// 参数: row, col, newValue // 参数: row, col, newValue
std::function<void(int, int, const UIString&)> CellValueChanged = nullptr; std::function<void(int, int, const UIString&)> CellValueChanged = nullptr;
// 单元格编辑完成回调编辑结束时触发比如TextBox失去焦点或按Enter时
// 参数: row, col, oldValue, newValue
std::function<void(int, int, const UIString&, const UIString&)> CellEditFinished = nullptr;
public: public:
TableView(Object* parentObject = nullptr); TableView(Object* parentObject = nullptr);
virtual ~TableView(); virtual ~TableView();
@@ -376,6 +386,15 @@ namespace ezui {
// 获取所有选中的行索引 // 获取所有选中的行索引
std::vector<int> GetCheckedRows() const; std::vector<int> GetCheckedRows() const;
// 获取当前选中的行非CheckBox模式通过单击第一列选中
int GetSelectedRow() const;
// 设置当前选中的行非CheckBox模式
void SetSelectedRow(int row);
// 清除选中行非CheckBox模式
void ClearSelection();
// 全选 // 全选
void SelectAll(); void SelectAll();

View File

@@ -1,106 +0,0 @@
#include "pch.h"
#include "mainForm.h"
void mainForm::OnNotify(Control* sender, EventArgs& args)
{
UIString btnName = sender->Name; // 控件id
Event eventType = args.EventType; // 事件类型
switch (eventType)
{
case ezui::OnMouseDown: //鼠标按下
{
if (btnName == "btnExitMain") { //退出
int result = ::MessageBoxW(Hwnd(), L"要退出程序吗?", L"提示", MB_YESNO | MB_ICONQUESTION);
if (result == IDYES)
exit(0);
}
else if (btnName == "btnMinMain") { //最小化
this->ShowMinimized();
}
else if (btnName == "btnMaxMain") { //最大化|还原
if (this->IsMaximized()) {
this->ShowNormal();
// 修改控件文字
Button* btnMax = (Button*)FindControl("btnMaxMain");
btnMax->SetText(L"🗖");
}
else {
this->ShowMaximized();
// 修改控件文字
Button* btnMax = (Button*)FindControl("btnMaxMain");
btnMax->SetText(L"🗗");
}
}
else if (btnName == "btnAdmin") { //到管理页面 获取 mainTab 设置页面为0
TabLayout* mainTab = (TabLayout*)FindControl("mainTab");
mainTab->SetPageIndex(0);
mainTab->Invalidate();
}
else if (btnName == "btnTools") { //到工具页面 获取 mainTab 设置页面为1
TabLayout* mainTab = (TabLayout*)FindControl("mainTab");
mainTab->SetPageIndex(1);
mainTab->Invalidate();
}
else if (btnName == "btnAdminConnect") { //管理页面的连接按钮按下,先获取 btnAdminConnect 按钮,设置为不可用,修改文字为“连接中...”
Button* btnAdminConnect = (Button*)FindControl("btnAdminConnect");
btnAdminConnect->SetText(L"🔄");
btnAdminConnect->Style.BackColor = Color(0, 185, 107);
}
else if (btnName == "btnAdminTemp") { //管理页面的测试按钮
TextBox* textAdmin = (TextBox*)FindControl("textAdminOutput"); //获取输出框
if (textAdmin) {
textAdmin->Insert(L"\n测试成功!",true);
textAdmin->GetScrollBar()->ScrollTo(1.0);
}
TableView* tableView = (TableView*)FindControl("tableViewAdmin"); //获取表格控件
if (tableView) {
tableView->SetColumnType(4, ezui::CellType::CheckBox);
tableView->SetColumnType(5, ezui::CellType::ComboBox);
tableView->SetColumnComboItems(5, {L"选择1", L"选择2"});
tableView->InsertRow(1);
tableView->SetRowData(tableView->GetRowCount() - 1, {L"uid1", L"192.168.200.131"});
}
}
}
break;
case ezui::OnMouseDoubleClick: //鼠标双击
{
//if (btnName == "titleMain") { //标题布局
// if (this->IsMaximized()) {
// this->ShowNormal();
// }
// else {
// this->ShowMaximized();
// }
//}
}
break;
default:
break;
}
if (args.EventType == Event::OnMouseDown) {
}
__super::OnNotify(sender, args);
}
void mainForm::OnClose(bool& close)
{
Application::Exit();
}
mainForm::mainForm() :LayeredWindow(1000, 750)
{
SetResizable(true); // 启用窗口大小调整
SetMiniSize(Size(600, 450)); // 设置最小尺寸
umg.LoadXml("res/mainForm.htm");//加载xml里面的控件与样式
umg.SetupUI(this);
}
mainForm::~mainForm()
{
}

View File

@@ -159,6 +159,12 @@ namespace ezui {
int m_lastClickRow = -1; int m_lastClickRow = -1;
int m_lastClickCol = -1; int m_lastClickCol = -1;
// 选中行非CheckBox模式下通过单击第一列选中的行
int m_selectedRow = -1;
// 编辑前的原始值(用于编辑完成回调)
UIString m_editOriginalValue;
private: private:
void Init(); void Init();
@@ -236,13 +242,17 @@ namespace ezui {
virtual const Size& GetContentSize() override; virtual const Size& GetContentSize() override;
public: public:
// 选中行背景色当第一列为CheckBox且被选中时使用 // 选中行背景色当第一列为CheckBox且被选中时使用,或者单击选中行时使用
Color SelectedRowBackColor = Color(0xFFADD8E6); // 浅蓝色 Color SelectedRowBackColor = Color(0xFFADD8E6); // 浅蓝色
// 单元格内容变化回调 // 单元格内容变化回调(内容变化时立即触发)
// 参数: row, col, newValue // 参数: row, col, newValue
std::function<void(int, int, const UIString&)> CellValueChanged = nullptr; std::function<void(int, int, const UIString&)> CellValueChanged = nullptr;
// 单元格编辑完成回调编辑结束时触发比如TextBox失去焦点或按Enter时
// 参数: row, col, oldValue, newValue
std::function<void(int, int, const UIString&, const UIString&)> CellEditFinished = nullptr;
public: public:
TableView(Object* parentObject = nullptr); TableView(Object* parentObject = nullptr);
virtual ~TableView(); virtual ~TableView();
@@ -376,6 +386,15 @@ namespace ezui {
// 获取所有选中的行索引 // 获取所有选中的行索引
std::vector<int> GetCheckedRows() const; std::vector<int> GetCheckedRows() const;
// 获取当前选中的行非CheckBox模式通过单击第一列选中
int GetSelectedRow() const;
// 设置当前选中的行非CheckBox模式
void SetSelectedRow(int row);
// 清除选中行非CheckBox模式
void ClearSelection();
// 全选 // 全选
void SelectAll(); void SelectAll();

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -311,6 +311,11 @@ namespace ezui {
backColor = SelectedRowBackColor; backColor = SelectedRowBackColor;
} }
// 如果第一列不是 CheckBox 且当前行被选中,使用选中行背景色
if (m_firstColumnType != FirstColumnType::CheckBox && row == m_selectedRow) {
backColor = SelectedRowBackColor;
}
// 绘制背景 // 绘制背景
g.SetColor(backColor); g.SetColor(backColor);
g.FillRectangle(RectF(cellRect)); g.FillRectangle(RectF(cellRect));
@@ -365,11 +370,16 @@ namespace ezui {
// 确定背景色 // 确定背景色
Color backColor = m_cellBackColor; Color backColor = m_cellBackColor;
// 如果第一列是 CheckBox 且选中,使用选中行背景色(优先级低) // 如果第一列是 CheckBox 且选中,使用选中行背景色
if (m_firstColumnType == FirstColumnType::CheckBox && row < (int)m_rowChecked.size() && m_rowChecked[row]) { if (m_firstColumnType == FirstColumnType::CheckBox && row < (int)m_rowChecked.size() && m_rowChecked[row]) {
backColor = SelectedRowBackColor; backColor = SelectedRowBackColor;
} }
// 如果第一列不是 CheckBox 且当前行被选中,使用选中行背景色
if (m_firstColumnType != FirstColumnType::CheckBox && row == m_selectedRow) {
backColor = SelectedRowBackColor;
}
// 如果单元格有独立背景色,使用独立设置(优先级高) // 如果单元格有独立背景色,使用独立设置(优先级高)
if (cellData.Style.HasBackColor) { if (cellData.Style.HasBackColor) {
backColor = cellData.Style.BackColor; backColor = cellData.Style.BackColor;
@@ -487,6 +497,20 @@ namespace ezui {
EndEdit(true); // 结束之前的编辑 EndEdit(true); // 结束之前的编辑
// 保存编辑前的原始值
if (colInfo.Type == CellType::TextBox) {
m_editOriginalValue = m_data[row][col].Text;
} else if (colInfo.Type == CellType::CheckBox) {
m_editOriginalValue = m_data[row][col].Checked ? L"true" : L"false";
} else if (colInfo.Type == CellType::ComboBox) {
int idx = m_data[row][col].ComboIndex;
if (idx >= 0 && idx < (int)colInfo.ComboItems.size()) {
m_editOriginalValue = colInfo.ComboItems[idx];
} else {
m_editOriginalValue = L"";
}
}
m_editing = true; m_editing = true;
m_editRow = row; m_editRow = row;
m_editCol = col; m_editCol = col;
@@ -558,12 +582,22 @@ namespace ezui {
return; return;
} }
if (save && m_editRow >= 0 && m_editCol >= 0) { int editRow = m_editRow;
if (m_editRow < (int)m_data.size() && m_editCol < (int)m_data[m_editRow].size()) { int editCol = m_editCol;
const auto& colInfo = m_columns[m_editCol]; UIString newValue;
if (save && editRow >= 0 && editCol >= 0) {
if (editRow < (int)m_data.size() && editCol < (int)m_data[editRow].size()) {
const auto& colInfo = m_columns[editCol];
if (colInfo.Type == CellType::TextBox) { if (colInfo.Type == CellType::TextBox) {
m_data[m_editRow][m_editCol].Text = m_editBox->GetText(); newValue = m_editBox->GetText();
UpdateRowHeight(m_editRow); m_data[editRow][editCol].Text = newValue;
UpdateRowHeight(editRow);
// 触发编辑完成回调
if (CellEditFinished && newValue != m_editOriginalValue) {
CellEditFinished(editRow, editCol, m_editOriginalValue, newValue);
}
} }
} }
} }
@@ -942,6 +976,7 @@ namespace ezui {
// 点击第一列 // 点击第一列
if (col == -1) { if (col == -1) {
if (m_firstColumnType == FirstColumnType::CheckBox && row < (int)m_rowChecked.size()) { if (m_firstColumnType == FirstColumnType::CheckBox && row < (int)m_rowChecked.size()) {
// CheckBox 模式:切换该行的选中状态
m_rowChecked[row] = !m_rowChecked[row]; m_rowChecked[row] = !m_rowChecked[row];
// 更新全选状态 // 更新全选状态
@@ -954,6 +989,10 @@ namespace ezui {
} }
m_headerSelectAll = allChecked; m_headerSelectAll = allChecked;
Invalidate();
} else if (m_firstColumnType != FirstColumnType::CheckBox) {
// 非 CheckBox 模式:单击选中当前行
m_selectedRow = row;
Invalidate(); Invalidate();
} }
return; return;
@@ -1452,6 +1491,22 @@ namespace ezui {
return result; return result;
} }
int TableView::GetSelectedRow() const {
return m_selectedRow;
}
void TableView::SetSelectedRow(int row) {
if (row >= -1 && row < (int)m_data.size()) {
m_selectedRow = row;
Invalidate();
}
}
void TableView::ClearSelection() {
m_selectedRow = -1;
Invalidate();
}
void TableView::SelectAll() { void TableView::SelectAll() {
if (m_firstColumnType == FirstColumnType::CheckBox) { if (m_firstColumnType == FirstColumnType::CheckBox) {
m_headerSelectAll = true; m_headerSelectAll = true;