// CDlgItemMgr.cpp: 实现文件 // #include "pch.h" #include "scanner-check.h" #include "CDlgItemMgr.h" #include "afxdialogex.h" #include #include // CDlgItemMgr 对话框 #pragma warning(disable: 4996) static bool is_checked(CWnd* ctrl) { return ((CButton*)ctrl)->GetCheck() == BST_CHECKED; } static bool is_checked(CDialogEx* dlg, UINT id) { return is_checked(dlg->GetDlgItem(id)); } static void set_checked(CWnd* ctrl, bool checked) { ((CButton*)ctrl)->SetCheck(checked ? BST_CHECKED : BST_UNCHECKED); } static void set_checked(CDialogEx* dlg, UINT id, bool checked) { return set_checked(dlg->GetDlgItem(id), checked); } IMPLEMENT_DYNAMIC(CDlgItemMgr, CDialogEx) CDlgItemMgr::CDlgItemMgr(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_TEST_ITEM, pParent) { } CDlgItemMgr::~CDlgItemMgr() { } void CDlgItemMgr::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Control(pDX, IDC_LIST1, list_); } BOOL CDlgItemMgr::OnInitDialog() { CDialogEx::OnInitDialog(); int ind = list_.InsertColumn(list_.GetHeaderCtrl()->GetItemCount(), TEXT("\u5E8F\u53F7"), 0, 40); list_.InsertColumn(list_.GetHeaderCtrl()->GetItemCount(), TEXT("ID"), 0, 60); list_.InsertColumn(list_.GetHeaderCtrl()->GetItemCount(), TEXT("\u540D\u79F0"), 0, 102); list_.InsertColumn(list_.GetHeaderCtrl()->GetItemCount(), TEXT("\u5FC5\u987B\u8054\u673A\u6D4B\u8BD5"), 0, 87); list_.InsertColumn(list_.GetHeaderCtrl()->GetItemCount(), TEXT("\u4E0D\u901A\u8FC7\u5219\u505C\u6B62"), 0, 87); list_.SetExtendedStyle((list_.GetExStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES) & (~LVS_EX_CHECKBOXES)); list_.ModifyStyle(0, LVS_SINGLESEL); load_stored_config(); return TRUE; // 除非将焦点设置到控件,否则返回 TRUE } BEGIN_MESSAGE_MAP(CDlgItemMgr, CDialogEx) ON_BN_CLICKED(IDC_BUTTON_ADD, &CDlgItemMgr::OnBnClickedButtonAdd) ON_NOTIFY(NM_DBLCLK, IDC_LIST1, &CDlgItemMgr::OnNMDblclkList1) ON_WM_DROPFILES() END_MESSAGE_MAP() // CDlgItemMgr 消息处理程序 void CDlgItemMgr::OnBnClickedButtonAdd() { // TODO: 在此添加控件通知处理程序代码 wchar_t text[128] = { 0 }, name[128] = { 0 }; bool online = is_checked(this, IDC_CHECK_ONLINE), fatal = is_checked(this, IDC_CHECK_FATAL); int ind = -1; if (::GetWindowTextW(GetDlgItem(IDC_EDIT_TITLE)->m_hWnd, text, _countof(text) - 1) == 0 || ::GetWindowTextW(GetDlgItem(IDC_EDIT_NAME)->m_hWnd, name, _countof(name) - 1) == 0) return; for (int i = 0; i < list_.GetItemCount(); ++i) { wchar_t val[128] = { 0 }; list_.GetItemText(i, 1, val, _countof(val) - 1); if (wcscmp(name, val) == 0) { ind = i; break; } } if (ind == -1) { wchar_t sn[20] = { 0 }; swprintf_s(sn, _countof(sn) - 1, L"%u", list_.GetItemCount() + 1); ind = list_.InsertItem(list_.GetItemCount(), sn); list_.SetItemText(ind, 1, name); } list_.SetItemText(ind, 2, text); list_.SetItemText(ind, 3, online ? L"true" : L"false"); list_.SetItemText(ind, 4, fatal ? L"true" : L"false"); list_.SetItemState(ind, LVNI_FOCUSED | LVIS_SELECTED, LVNI_FOCUSED | LVIS_SELECTED); item_changed(); } void CDlgItemMgr::OnNMDblclkList1(NMHDR* pNMHDR, LRESULT* pResult) { LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast(pNMHDR); // TODO: 在此添加控件通知处理程序代码 POINT pos = { 0 }; RECT r = { 0 }; GetCursorPos(&pos); list_.ScreenToClient(&pos); r.left = list_.HitTest(pos); if (r.left != -1) { std::vector width; int ind = r.left; for (int i = 0; i < list_.GetHeaderCtrl()->GetItemCount(); ++i) { RECT r = { 0 }; list_.GetHeaderCtrl()->GetItemRect(i, &r); width.push_back(r.right - r.left); } for (int i = 0; i < width.size(); ++i) { if (pos.x <= width[i]) { wchar_t text[128] = { 0 }; if (i < 3) { std::wstring tips(L"\u5220\u9664\u6D4B\u8BD5\u9879\uFF1A"); list_.GetItemText(ind, 1, text, _countof(text) - 1); tips += text; tips += L"\uFF1F"; if (::MessageBoxW(m_hWnd, tips.c_str(), L"\u786E\u8BA4", MB_YESNO | MB_ICONQUESTION) == IDYES) { list_.DeleteItem(ind); for (; ind < list_.GetItemCount(); ++ind) { swprintf_s(text, _countof(text) - 1, L"%u", ind + 1); list_.SetItemText(ind, 0, text); } } } else { list_.GetItemText(ind, i, text, _countof(text) - 1); if (wcscmp(text, L"true")) list_.SetItemText(ind, i, L"true"); else list_.SetItemText(ind, i, L"false"); } break; } pos.x -= width[i]; } } *pResult = 0; item_changed(); } std::wstring CDlgItemMgr::get_stored_config_file(void) { wchar_t path[MAX_PATH] = { 0 }; std::wstring file(L""); size_t pos = 0; GetModuleFileNameW(NULL, path, _countof(path) - 1); file = path; pos = file.rfind(L'\\'); if (pos++ != std::wstring::npos) file.erase(pos); file += L"config\\stored.txt"; return file; } void CDlgItemMgr::get_item(int ind, page_config::ITEM* item) { wchar_t text[128] = { 0 }; list_.GetItemText(ind, 1, text, _countof(text) - 1); item->name = text; list_.GetItemText(ind, 2, text, _countof(text) - 1); item->title = text; list_.GetItemText(ind, 3, text, _countof(text) - 1); item->man = wcscmp(text, L"false") == 0; list_.GetItemText(ind, 4, text, _countof(text) - 1); item->fatal = wcscmp(text, L"true") == 0; } void CDlgItemMgr::load_from_file(const wchar_t* file) { wchar_t path[MAX_PATH] = { 0 }, *sn = path, *n = sn + 40, *t = n + 40, *m = t + 40, *f = m + 40; size_t pos = 0; std::string bom(""); std::wstring cont(L""); list_.DeleteAllItems(); file_util::load_file(file, got_str, &bom); coding_util::bom::to_unicode(bom.c_str(), bom.length(), got_wstr, &cont); cont += L"\r\n"; while ((pos = cont.find(L"\r\n")) != std::wstring::npos) { int cnt = swscanf(cont.substr(0, pos).c_str(), L"%s %s %s %s", n, t, m, f); if (cnt >= 2) { bool man = false, fatal = true; if (cnt >= 3) man = wcscmp(m, L"false") == 0; if (cnt >= 4) fatal = wcscmp(f, L"true") == 0; swprintf_s(sn, 20, L"%u", list_.GetItemCount() + 1); cnt = list_.InsertItem(list_.GetItemCount(), sn); list_.SetItemText(cnt, 1, n); list_.SetItemText(cnt, 2, t); list_.SetItemText(cnt, 3, man ? L"false" : L"true"); list_.SetItemText(cnt, 4, f ? L"true" : L"false"); } cont.erase(0, pos + 2); } ::PostMessage(GetParent()->m_hWnd, WM_TEST_ITEM_CHANGED, 0, 0); } void CDlgItemMgr::load_stored_config(void) { std::wstring file(get_stored_config_file()); load_from_file(file.c_str()); } void CDlgItemMgr::item_changed(void) { std::vector items; std::wstring text(L""); std::string bom(""); get_all_items(items); for (size_t i = 0; i < items.size(); ++i) { text += items[i].name + L" " + items[i].title + L" "; text += items[i].man ? L"false " : L"true "; text += items[i].fatal ? L"true\r\n" : L"false\r\n"; } coding_util::bom::from_unicode(text.c_str(), text.length() * 2, got_str, &bom); file_util::save_2_file(bom.c_str(), bom.length(), get_stored_config_file().c_str()); ::PostMessage(GetParent()->m_hWnd, WM_TEST_ITEM_CHANGED, 0, 0); } bool CDlgItemMgr::get_name(const wchar_t* title, page_config::ITEM* item) { wchar_t text[128] = { 0 }; for (size_t i = 0; i < list_.GetItemCount(); ++i) { list_.GetItemText(i, 2, text, _countof(text) - 1); if (wcscmp(text, title) == 0) { get_item(i, item); return true; } text[0] = 0; } return false; } void CDlgItemMgr::get_all_items(std::vector& items) { wchar_t text[128] = { 0 }; for (size_t i = 0; i < list_.GetItemCount(); ++i) { page_config::ITEM item; get_item(i, &item); items.push_back(item); } } void CDlgItemMgr::OnDropFiles(HDROP hDropInfo) { // TODO: 在此添加消息处理程序代码和/或调用默认值 // TODO: 在此添加消息处理程序代码和/或调用默认值 wchar_t path[MAX_PATH] = { 0 }; std::wstring cont(L""); std::string bom(""); DragQueryFileW(hDropInfo, 0, path, _countof(path) - 1); load_from_file(path); CDialogEx::OnDropFiles(hDropInfo); }