code_twain/sane/DlgIndicator.cpp

168 lines
4.2 KiB
C++
Raw Normal View History

2022-06-18 08:48:41 +00:00
// 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)
2022-06-18 08:48:41 +00:00
{
create();
SetWindowLongW(GetDlgItem(hwnd_, IDC_STATIC_ERR), GWL_STYLE, GetWindowLong(GetDlgItem(hwnd_, IDC_STATIC_ERR), GWL_STYLE) | SS_OWNERDRAW);
2022-06-18 08:48:41 +00:00
}
dlg_indicator::~dlg_indicator()
{
}
BOOL dlg_indicator::handle_message(UINT msg, WPARAM wp, LPARAM lp)
2022-06-18 08:48:41 +00:00
{
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_);
2022-06-18 08:48:41 +00:00
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();
break;
}
default:
ret = FALSE;
break;
}
return ret;
}
void dlg_indicator::handle_command(WORD code, WORD id, HANDLE ctrl)
{
if (id == IDCANCEL)
{
notify_over();
}
}
void dlg_indicator::notify_over(void)
{
if (notify_)
notify_(true, notify_param_);
}
void dlg_indicator::set_quit_notify(void(__stdcall* notify)(bool, 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()
2022-06-18 08:48:41 +00:00
{
RECT rp, r;
if (IsWindow(parent_))
GetWindowRect(parent_, &rp);
2022-06-18 08:48:41 +00:00
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();
}
}
// CDlgIndicator 消息处理程序