| | |
| | | { |
| | | CDialogEx::OnInitDialog(); |
| | | |
| | | // TODO: 在此添加额外的初始化 |
| | | // 设置对话框标题 |
| | | SetWindowText(m_recipe.vecDeviceList.empty() ? _T("新建配方") : _T("编辑配方")); |
| | | |
| | | if (m_font.m_hObject == NULL) { |
| | | // 获取对话框的字体 |
| | | CFont* pDlgFont = GetFont(); |
| | | LOGFONT lf; |
| | | if (pDlgFont && pDlgFont->GetLogFont(&lf)) { |
| | | m_font.CreateFontIndirect(&lf); |
| | | } |
| | | } |
| | | |
| | | // 计算x起始坐标 |
| | | CWnd* pWndDesc = GetDlgItem(IDC_STATIC_DESC); |
| | | CRect rDesc; |
| | | int nYStart = 0; |
| | | int nXStart = 0; |
| | | int nTotalControlWidth = 340; // 默认兜底 |
| | | if (pWndDesc) { |
| | | pWndDesc->GetWindowRect(&rDesc); |
| | | ScreenToClient(&rDesc); |
| | | nXStart = rDesc.left; |
| | | } |
| | | else { |
| | | nXStart = 30; |
| | | } |
| | | |
| | | // 计算x起始坐标 |
| | | pWndDesc = GetDlgItem(IDC_EDIT_DESC); |
| | | if (pWndDesc) { |
| | | pWndDesc->GetWindowRect(&rDesc); |
| | | ScreenToClient(&rDesc); |
| | | nYStart = rDesc.bottom + 20; |
| | | } |
| | | else { |
| | | nYStart = 30; |
| | | } |
| | | |
| | | // 保证每次进来都清空 |
| | | for (auto& ctrl : m_vecDevices) { |
| | | if (ctrl.editDeviceID) { |
| | | delete ctrl.editDeviceID; |
| | | ctrl.editDeviceID = nullptr; |
| | | } |
| | | |
| | | if (ctrl.editDeviceName) { |
| | | delete ctrl.editDeviceName; |
| | | ctrl.editDeviceName = nullptr; |
| | | } |
| | | |
| | | if (ctrl.comboRecipeID) { |
| | | delete ctrl.comboRecipeID; |
| | | ctrl.comboRecipeID = nullptr; |
| | | } |
| | | } |
| | | m_vecDevices.clear(); |
| | | |
| | | // 创建控件(不管新建还是编辑,都先创建好) |
| | | CRect rClient; |
| | | GetClientRect(&rClient); |
| | | nTotalControlWidth = (rClient.Width() - nXStart * 2); |
| | | const int nRowHeight = 30; |
| | | const int nRowCount = static_cast<int>(g_vecBindDevices.size()); |
| | | for (int i = 0; i < nRowCount; ++i) { |
| | | int y = nYStart + i * nRowHeight; |
| | | const auto& meta = g_vecBindDevices[i]; |
| | | |
| | | CEdit* pEditID = new CEdit(); |
| | | pEditID->Create(WS_CHILD | WS_VISIBLE | WS_BORDER, CRect(nXStart, y, nXStart + 100, y + 25), this, IDC_EDIT_DEVICEID_BASE + i); |
| | | |
| | | CEdit* pEditName = new CEdit(); |
| | | pEditName->Create(WS_CHILD | WS_VISIBLE | WS_BORDER, CRect(nXStart + 110, y, nXStart + 210, y + 25), this, IDC_EDIT_DEVICENAME_BASE + i); |
| | | |
| | | CComboBox* pCombo = new CComboBox(); |
| | | pCombo->Create(WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST, CRect(nXStart + 220, y, nXStart + nTotalControlWidth, y + 25), this, IDC_COMBO_RECIPEID_BASE + i); |
| | | |
| | | pEditID->SetFont(&m_font); |
| | | pEditName->SetFont(&m_font); |
| | | pCombo->SetFont(&m_font); |
| | | |
| | | m_vecDevices.push_back({ pEditID, pEditName, pCombo }); |
| | | } |
| | | |
| | | // 填充内容 |
| | | if (m_recipe.vecDeviceList.empty()) { |
| | | SetWindowText(_T("新建配方")); |
| | | |
| | | // 设置固定大小(例如 600x400) |
| | | SetWindowPos(nullptr, 0, 0, 600, 400, SWP_NOMOVE | SWP_NOZORDER); |
| | | |
| | | // 创建控件 |
| | | const int totalControlWidth = 340; |
| | | CRect clientRect; |
| | | GetClientRect(&clientRect); |
| | | int xStart = (clientRect.Width() - totalControlWidth) / 2; |
| | | |
| | | const int nRowHeight = 30; |
| | | const int yStart = 30; // 顶部起始高度 |
| | | |
| | | const int nRowCount = static_cast<int>(g_vecBindDevices.size()); |
| | | // 新建模式 |
| | | for (int i = 0; i < nRowCount; ++i) { |
| | | int y = yStart + i * nRowHeight; |
| | | const auto& meta = g_vecBindDevices[i]; |
| | | |
| | | CEdit* pEditID = new CEdit(); |
| | | pEditID->Create(WS_CHILD | WS_VISIBLE | WS_BORDER, CRect(xStart, y, xStart + 100, y + 25), this, IDC_EDIT_DEVICEID_BASE + i); |
| | | |
| | | CString strID; |
| | | strID.Format(_T("%d"), meta.nDeviceID); |
| | | pEditID->SetWindowText(strID); |
| | | pEditID->SetReadOnly(TRUE); // 设备ID只读 |
| | | m_vecDevices[i].editDeviceID->SetWindowText(strID); |
| | | m_vecDevices[i].editDeviceID->SetReadOnly(TRUE); |
| | | |
| | | CEdit* pEditName = new CEdit(); |
| | | pEditName->Create(WS_CHILD | WS_VISIBLE | WS_BORDER, CRect(xStart + 110, y, xStart + 210, y + 25), this, IDC_EDIT_DEVICENAME_BASE + i); |
| | | pEditName->SetWindowText(CA2T(meta.strDeviceName)); |
| | | pEditName->SetReadOnly(TRUE); // 设备名称只读 |
| | | m_vecDevices[i].editDeviceName->SetWindowText(CA2T(meta.strDeviceName)); |
| | | m_vecDevices[i].editDeviceName->SetReadOnly(TRUE); |
| | | |
| | | CComboBox* pCombo = new CComboBox(); |
| | | pCombo->Create(WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST, CRect(xStart + 220, y, xStart + 340, y + 300), this, IDC_COMBO_RECIPEID_BASE + i); |
| | | |
| | | // 添加选项到 ComboBox |
| | | m_vecDevices.push_back({ pEditID, pEditName, pCombo }); |
| | | // 这里你可以初始化 ComboBox 选项,例如: |
| | | // for (const auto& r : GetRecipeListForDevice(meta.nDeviceID)) { |
| | | // int idx = m_vecDevices[i].comboRecipeID->AddString(CA2T(r.strRecipeName)); |
| | | // m_vecDevices[i].comboRecipeID->SetItemData(idx, r.nRecipeID); |
| | | // } |
| | | } |
| | | } |
| | | else { |
| | | SetWindowText(_T("编辑配方")); |
| | | |
| | | // 编辑模式 |
| | | m_strPPID = CA2T(m_recipe.strPPID.c_str()); |
| | | m_strDesc = CA2T(m_recipe.strDescription.c_str()); |
| | | UpdateData(FALSE); |
| | | |
| | | // 设置设备行数据 |
| | | for (size_t i = 0; i < m_recipe.vecDeviceList.size() && i < m_vecDevices.size(); ++i) { |
| | | const DeviceRecipe& d = m_recipe.vecDeviceList[i]; |
| | | CString str; |
| | | // 设置设备ID和名称 |
| | | // 设备ID |
| | | str.Format(_T("%d"), d.nDeviceID); |
| | | m_vecDevices[i].editDeviceID->SetWindowText(str); |
| | | m_vecDevices[i].editDeviceID->SetReadOnly(TRUE); |
| | | |
| | | str.Format(_T("%s"), d.strDeviceName.c_str()); |
| | | m_vecDevices[i].editDeviceName->SetWindowText(str); |
| | | // 设备名称 |
| | | m_vecDevices[i].editDeviceName->SetWindowText(CA2T(d.strDeviceName.c_str())); |
| | | m_vecDevices[i].editDeviceName->SetReadOnly(TRUE); |
| | | |
| | | /*ComboBox选择配方ID,后面需要修改****/ |
| | | //int nCount = m_vecDevices[i].comboRecipeID->GetCount(); |
| | | //for (int idx = 0; idx < nCount; ++idx) { |
| | | // if ((int)m_vecDevices[i].comboRecipeID->GetItemData(idx) == d.nRecipeID) { |
| | | // m_vecDevices[i].comboRecipeID->SetCurSel(idx); |
| | | // break; |
| | | // } |
| | | //} |
| | | // ComboBox 选项填充 |
| | | // for (const auto& r : GetRecipeListForDevice(d.nDeviceID)) { |
| | | // int idx = m_vecDevices[i].comboRecipeID->AddString(CA2T(r.strRecipeName)); |
| | | // m_vecDevices[i].comboRecipeID->SetItemData(idx, r.nRecipeID); |
| | | // // 默认选中对应配方ID |
| | | // if (r.nRecipeID == d.nRecipeID) |
| | | // m_vecDevices[i].comboRecipeID->SetCurSel(idx); |
| | | // } |
| | | } |
| | | } |
| | | |