// DlgIndicator.cpp: 实现文件 // #include "DlgIndicator.h" #include "resource.h" #include "scanned_img.h" // for local_trans // CDlgIndicator 对话框 #define WM_USB_PACKET_RECEIVED WM_USER + 1 #define WM_IMAGE_RECEIVED WM_USER + 2 #define WM_SCAN_FINISHED WM_USER + 3 // WPARAM: std::string* msg; LPARAM: boo err dlg_indicator::dlg_indicator(HWND parent) : dlg_base(parent, IDD_INDICATOR) , papers_(0), images_(0), notify_(NULL), notify_param_(NULL), 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_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) { if (notify_) notify_(cancel ? UI_EVENT_CLOSE_CANCEL : UI_EVENT_CLOSE_NORMAL, notify_param_); } void dlg_indicator::set_quit_notify(void(__stdcall* notify)(ui_event, void*), void* param) { notify_ = notify; notify_param_ = param; } HWND dlg_indicator::window(void) { return hwnd_; } HWND dlg_indicator::parent(void) { return parent_; } void dlg_indicator::show() { RECT rp, r; if (IsWindow(parent_)) GetWindowRect(parent_, &rp); else GetWindowRect(GetDesktopWindow(), &rp); GetWindowRect(hwnd_, &r); rp.left += (rp.right - rp.left - (r.right - r.left)) / 2; rp.top += (rp.bottom - rp.top - (r.bottom - r.top)) / 2; SetWindowPos(hwnd_, HWND_TOPMOST, rp.left, rp.top, r.right - r.left, r.bottom - r.top, SWP_NOSIZE | SWP_SHOWWINDOW); UpdateWindow(hwnd_); } void dlg_indicator::hide(void) { ShowWindow(hwnd_, SW_HIDE); } 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); } } // CDlgIndicator 消息处理程序