// DlgIndicator.cpp: 实现文件 // #include "DlgIndicator.h" #include "resource.h" #include "scanned_img.h" // for local_trans /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// dlg_choose_dev // CDlgIndicator 对话框 #define WM_SCAN_WORKING WM_USER + 1 // WPARAM: unused; LPARAM: unsed #define WM_USB_PACKET_RECEIVED WM_USER + 2 // WPARAM: unused; LPARAM: unsed #define WM_IMAGE_RECEIVED WM_USER + 3 // WPARAM: unused; LPARAM: unsed #define WM_SCAN_FINISHED WM_USER + 4 // WPARAM: std::string* msg; LPARAM: boo err dlg_indicator::dlg_indicator(HWND parent) : dlg_base(parent, IDD_INDICATOR) , papers_(0), images_(0), err_(false) { create(); SetWindowLongW(GetDlgItem(hwnd_, IDC_STATIC_ERR), GWL_STYLE, GetWindowLong(GetDlgItem(hwnd_, IDC_STATIC_ERR), GWL_STYLE) | SS_OWNERDRAW); } dlg_indicator::~dlg_indicator() { } BOOL dlg_indicator::handle_message(UINT msg, WPARAM wp, LPARAM lp) { wchar_t text[40] = { 0 }; BOOL ret = TRUE; switch (msg) { case WM_INITDIALOG: swprintf_s(text, _countof(text) - 1, L"%u", papers_); SetDlgItemTextW(hwnd_, IDC_EDIT_IMAGE, text); SetDlgItemTextW(hwnd_, IDC_EDIT_PAPER, text); UpdateWindow(hwnd_); break; case WM_USB_PACKET_RECEIVED: papers_++; swprintf_s(text, _countof(text) - 1, L"%u", papers_); SetDlgItemTextW(hwnd_, IDC_EDIT_PAPER, text); UpdateWindow(hwnd_); break; case WM_IMAGE_RECEIVED: images_++; swprintf_s(text, _countof(text) - 1, L"%u", images_); SetDlgItemTextW(hwnd_, IDC_EDIT_IMAGE, text); UpdateWindow(hwnd_); break; case WM_COMMAND: handle_command(HIWORD(wp), LOWORD(wp), (HWND)lp); break; case WM_DRAWITEM: if (wp == IDC_STATIC_ERR) { DRAWITEMSTRUCT* ds = (DRAWITEMSTRUCT*)lp; wchar_t text[128] = { 0 }; SetTextColor(ds->hDC, err_ ? RGB(255, 0, 0) : RGB(0, 0, 0)); SetBkMode(ds->hDC, TRANSPARENT); GetWindowTextW(ds->hwndItem, text, _countof(text) - 1); TextOutW(ds->hDC, 0, 0, text, lstrlenW(text)); } ret = FALSE; break; case WM_SCAN_WORKING: notify_ui_event(SANE_EVENT_WORKING); break; case WM_SCAN_FINISHED: if (lp) { std::string* str = (std::string*)wp; std::wstring err(local_trans::a2u(str->c_str(), CP_UTF8)); SetDlgItemTextW(hwnd_, IDC_STATIC_ERR, err.c_str()); delete str; } else { wchar_t msg[128] = { 0 }; std::string* str = (std::string*)wp; swprintf_s(msg, _countof(msg) - 1, L"\u603b\u8ba1\u626b\u63cf\u56fe\u7247\uff1a%u \u5f20", images_); SetDlgItemTextW(hwnd_, IDC_STATIC_ERR, msg); delete str; SetTimer(hwnd_, 1, 3000, NULL); } SetDlgItemTextW(hwnd_, IDCANCEL, L"\u5173\u95ed"); ShowWindow(GetDlgItem(hwnd_, IDC_STATIC_PAPER), SW_HIDE); ShowWindow(GetDlgItem(hwnd_, IDC_STATIC_IMAGE), SW_HIDE); ShowWindow(GetDlgItem(hwnd_, IDC_EDIT_PAPER), SW_HIDE); ShowWindow(GetDlgItem(hwnd_, IDC_EDIT_IMAGE), SW_HIDE); ShowWindow(GetDlgItem(hwnd_, IDC_STATIC_ERR), SW_SHOW); SetWindowTextW(hwnd_, L"\u626b\u63cf\u7ed3\u675f"); UpdateWindow(hwnd_); break; case WM_TIMER: if (wp == 1) { KillTimer(hwnd_, wp); notify_over(false); break; } default: ret = FALSE; break; } return ret; } void dlg_indicator::handle_command(WORD code, WORD id, HANDLE ctrl) { if (id == IDCANCEL) { notify_over(true); } } void dlg_indicator::notify_over(bool cancel) { notify_ui_event(cancel ? SANE_EVENT_UI_CLOSE_CANCEL : SANE_EVENT_UI_CLOSE_NORMAL); } HWND dlg_indicator::window(void) { return hwnd_; } HWND dlg_indicator::parent(void) { return parent_; } void dlg_indicator::notify_data_arrived(bool image) { PostMessage(hwnd_, image ? WM_IMAGE_RECEIVED : WM_USB_PACKET_RECEIVED, 0, 0); } void dlg_indicator::notify_scan_over(const char* msg, bool err) { std::string* mstr(new std::string(msg ? msg : "")); err_ = err; if (!PostMessage(hwnd_, WM_SCAN_FINISHED, (WPARAM)mstr, (LPARAM)err)) { delete mstr; notify_over(false); } } void dlg_indicator::notify_working(void) { PostMessage(hwnd_, WM_SCAN_WORKING, 0, 0); } // CDlgIndicator 消息处理程序 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// dlg_choose_dev /// dlg_choose_dev::dlg_choose_dev(HWND parent, const std::map& devs) : dlg_base(parent, IDD_CHOOSE_DEV), item_(-1) { create(); HWND lst = GetDlgItem(hwnd_, IDC_LIST1); LV_COLUMNW col = { 0 }; int ind = 0; ListView_SetExtendedListViewStyle(lst, ListView_GetExtendedListViewStyle(lst) | LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT); SetWindowLong(lst, GWL_STYLE, GetWindowLong(lst, GWL_STYLE) | LVS_SINGLESEL); col.mask = LVCF_TEXT | LVCF_WIDTH; col.cx = 180; col.pszText = (wchar_t*)L"\u8BBE\u5907\u540D\u79F0"; SendMessageW(lst, LVM_INSERTCOLUMN, ind++, (LPARAM)&col); col.pszText = (wchar_t*)L"\u5E8F\u5217\u53F7"; SendMessageW(lst, LVM_INSERTCOLUMN, ind++, (LPARAM)&col); for (std::map::const_iterator it = devs.begin(); it != devs.end(); ++it) { std::wstring n(local_trans::a2u(it->first.c_str(), CP_UTF8)), s(local_trans::a2u(it->second.c_str(), CP_UTF8)); LV_ITEM item = { 0 }; int ind = 0; item.mask = LVIF_TEXT; item.pszText = &n[0]; item.iItem = ListView_GetItemCount(lst); ind = SendMessageW(lst, LVM_INSERTITEMW, 0, (LPARAM)&item); item.pszText = &s[0]; item.iSubItem = 1; item.iItem = ind; SendMessageW(lst, LVM_SETITEMTEXTW, ind, (LPARAM)&item); if (it == devs.begin()) { item_ = 0; ListView_SetItemState(lst, ind, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED); SetFocus(lst); } } } dlg_choose_dev::~dlg_choose_dev() {} BOOL dlg_choose_dev::handle_message(UINT msg, WPARAM wp, LPARAM lp) { wchar_t text[40] = { 0 }; BOOL ret = TRUE; switch (msg) { case WM_COMMAND: handle_command(HIWORD(wp), LOWORD(wp), (HWND)lp); break; case WM_NOTIFY: handle_notify(wp, (LPNMHDR)lp); break; default: ret = FALSE; break; } return ret; } void dlg_choose_dev::handle_command(WORD code, WORD id, HANDLE ctrl) { if (id == IDOK) { HWND lst = GetDlgItem(hwnd_, IDC_LIST1); if (item_ >= 0 && item_ < ListView_GetItemCount(lst)) { wchar_t buf[128] = { 0 }; ListView_GetItemText(lst, item_, 0, buf, _countof(buf) - 1); sel_ = local_trans::u2a(buf, CP_UTF8); } id = IDCANCEL; } if (id == IDCANCEL) { abandon_hold_ = true; PostMessage(hwnd_, 0, 0, 0); } } void dlg_choose_dev::handle_notify(UINT id, LPNMHDR pnhdr) { if (pnhdr->hwndFrom == GetDlgItem(hwnd_, IDC_LIST1)) { if (pnhdr->code == LVN_ITEMCHANGED) { LPNMHEADER h = (LPNMHEADER)pnhdr; item_ = h->iItem; } else if (pnhdr->code == NM_DBLCLK) { handle_command(0, IDOK, NULL); } } } std::string dlg_choose_dev::get_selected_device(void) { return sel_; }