// CLinkReportDlg.cpp: 实现文件 // #include "pch.h" #include "EAPSimulator.h" #include "CLinkReportDlg.h" #include "afxdialogex.h" #include #include #include "CLinkReportDetailDlg.h" // CLinkReportDlg 对话框 IMPLEMENT_DYNAMIC(CLinkReportDlg, CDialogEx) CLinkReportDlg::CLinkReportDlg(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_DIALOG_LINK_REPORT, pParent) { } CLinkReportDlg::~CLinkReportDlg() { } void CLinkReportDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CLinkReportDlg, CDialogEx) ON_NOTIFY(NM_DBLCLK, IDC_LIST1, &CLinkReportDlg::OnListCtrlDoubleClick) ON_BN_CLICKED(IDC_BUTTON_SEND, &CLinkReportDlg::OnBnClickedButtonSend) ON_WM_DESTROY() END_MESSAGE_MAP() // CLinkReportDlg 消息处理程序 BOOL CLinkReportDlg::OnInitDialog() { CDialogEx::OnInitDialog(); CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST1); DWORD dwStyle = pListCtrl->GetExtendedStyle(); dwStyle |= LVS_EX_FULLROWSELECT; dwStyle |= LVS_EX_GRIDLINES; pListCtrl->SetExtendedStyle(dwStyle); pListCtrl->ModifyStyle(0, LVS_SHOWSELALWAYS); HIMAGELIST imageList = ImageList_Create(24, 24, ILC_COLOR24, 1, 1); ListView_SetImageList(pListCtrl->GetSafeHwnd(), imageList, LVSIL_SMALL); pListCtrl->InsertColumn(0, _T(""), LVCFMT_RIGHT, 0); pListCtrl->InsertColumn(1, _T("CEID"), LVCFMT_LEFT, 120); pListCtrl->InsertColumn(2, _T("CD Name"), LVCFMT_LEFT, 120); pListCtrl->InsertColumn(3, _T("Descriptions"), LVCFMT_LEFT, 180); pListCtrl->InsertColumn(4, _T("Attached RPTID"), LVCFMT_LEFT, 120); pListCtrl->SetColumnWidth(4, LVSCW_AUTOSIZE_USEHEADER); CString strFile; strFile.Format(_T("%s\\CollectionEventList.txt"), (LPTSTR)(LPCTSTR)theApp.m_strAppDir); loadCollectionEvents((LPTSTR)(LPCTSTR)strFile); for (auto item : m_collectionEvents) { int index = pListCtrl->InsertItem(pListCtrl->GetItemCount(), _T("")); pListCtrl->SetItemData(index, (DWORD_PTR)item); pListCtrl->SetItemText(index, 1, std::to_string(item->getEventId()).c_str()); pListCtrl->SetItemText(index, 2, item->getName().c_str()); pListCtrl->SetItemText(index, 3, item->getDescription().c_str()); pListCtrl->SetItemText(index, 4, item->getReportIdsText().c_str()); } return TRUE; // return TRUE unless you set the focus to a control // 异常: OCX 属性页应返回 FALSE } int CLinkReportDlg::loadCollectionEvents(const char* pszFilepath) { CStdioFile file; if (!file.Open(pszFilepath, CFile::modeRead)) { return -1; } std::regex pattern("^\\d+,[^,]*,[^,]*,\\(\\d+(,\\d+)*\\).*"); // 匹配以数字+逗号开头的字符串 std::vector events; int index, last; CString strLine, strRPTIDs; CString strId, strName, strDescription; while (file.ReadString(strLine)) { if (!std::regex_match((LPTSTR)(LPCTSTR)strLine, pattern)) { continue; } last = 0; index = strLine.Find(",", last); if (index < 0) continue; strId = strLine.Left(index); last = index + 1; index = strLine.Find(",", last); if (index < 0) continue; strName = strLine.Mid(last, index - last); last = index + 1; index = strLine.Find(",", last); if (index < 0) continue; strDescription = strLine.Mid(last, index - last); strRPTIDs = strLine.Right(strLine.GetLength() - index - 1); strRPTIDs.Delete(0); strRPTIDs.Delete(strRPTIDs.GetLength() - 1); auto prtids = parseVidList(strRPTIDs); SERVO::CCollectionEvent* pEvent = new SERVO::CCollectionEvent( atoi(strId), (LPTSTR)(LPCTSTR)strName, (LPTSTR)(LPCTSTR)strDescription, prtids); events.push_back(pEvent); } if (!events.empty()) { clearAllCollectionEvent(); for (auto item : events) { m_collectionEvents.push_back(item); } } file.Close(); return 0; } std::vector CLinkReportDlg::parseVidList(CString& strNums) { // 1. 先去掉可能出现的空白符(空格、制表符等) strNums.Trim(); // 2️. std::vector result; int i = 0; CString strVid; while (1) { if (!AfxExtractSubString(strVid, (LPCTSTR)strNums, i, ',')) { break; } if (!strVid.IsEmpty()) // 防御性检查 result.push_back(std::stoi((LPTSTR)(LPCTSTR)strVid)); i++; } return result; } void CLinkReportDlg::clearAllCollectionEvent() { for (auto item : m_collectionEvents) { delete item; } m_collectionEvents.clear(); } void CLinkReportDlg::OnListCtrlDoubleClick(NMHDR* pNMHDR, LRESULT* pResult) { LPNMITEMACTIVATE pNMItem = (LPNMITEMACTIVATE)pNMHDR; int nItem = pNMItem->iItem; if (nItem >= 0) { CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST1); SERVO::CCollectionEvent* pEvent = (SERVO::CCollectionEvent*)pListCtrl->GetItemData(nItem); CLinkReportDetailDlg dlg; dlg.SetCollectionEvent(pEvent); if (IDOK == dlg.DoModal()) { pListCtrl->SetItemText(nItem, 4, pEvent->getReportIdsText().c_str()); } } *pResult = 0; } void CLinkReportDlg::OnBnClickedButtonSend() { std::vector events; CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST1); POSITION pos = pListCtrl->GetFirstSelectedItemPosition(); while (pos) { int nItem = pListCtrl->GetNextSelectedItem(pos); // 获取选中项索引 SERVO::CCollectionEvent* pEvent = (SERVO::CCollectionEvent*)pListCtrl->GetItemData(nItem); events.push_back(pEvent); } for (auto item : events) { TRACE("name:%s\n", item->getName().c_str()); } } void CLinkReportDlg::OnDestroy() { CDialogEx::OnDestroy(); clearAllCollectionEvent(); }