245 lines
6.1 KiB
C++
245 lines
6.1 KiB
C++
|
// 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
|
|||
|
|
|||
|
ATOM dlg_indicator::indicator_class_atom = 0;
|
|||
|
std::wstring dlg_indicator::handle_name = L"dlg_indicator_prop_handle";
|
|||
|
std::wstring dlg_indicator::indicator_class_name = L"dlg_indicator_class";
|
|||
|
extern HMODULE g_my_inst;
|
|||
|
|
|||
|
dlg_indicator::dlg_indicator() : hwnd_(NULL), papers_(0), images_(0), notify_(NULL), notify_param_(NULL), parent_(NULL), err_(false)
|
|||
|
{
|
|||
|
HANDLE wait = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|||
|
#ifdef USE_SOLE_WIN_THREAD
|
|||
|
thread_.reset(new std::thread(&dlg_indicator::create, this, wait));
|
|||
|
#else
|
|||
|
create(wait);
|
|||
|
#endif
|
|||
|
WaitForSingleObject(wait, INFINITE);
|
|||
|
CloseHandle(wait);
|
|||
|
}
|
|||
|
dlg_indicator::~dlg_indicator()
|
|||
|
{
|
|||
|
#ifdef USE_SOLE_WIN_THREAD
|
|||
|
if (IsWindow(hwnd_))
|
|||
|
PostMessage(hwnd_, WM_QUIT, 0, 0);
|
|||
|
|
|||
|
if (thread_.get() && thread_->joinable())
|
|||
|
thread_->join();
|
|||
|
thread_.reset();
|
|||
|
#else
|
|||
|
if (IsWindow(hwnd_))
|
|||
|
DestroyWindow(hwnd_);
|
|||
|
#endif
|
|||
|
}
|
|||
|
|
|||
|
BOOL CALLBACK dlg_indicator::dlg_indicator_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
|
|||
|
{
|
|||
|
if (msg == WM_INITDIALOG)
|
|||
|
{
|
|||
|
dlg_indicator *obj = (dlg_indicator*)lp;
|
|||
|
|
|||
|
SetPropW(hwnd, dlg_indicator::handle_name.c_str(), (HANDLE)obj);
|
|||
|
}
|
|||
|
|
|||
|
dlg_indicator* obj = (dlg_indicator*)GetPropW(hwnd, dlg_indicator::handle_name.c_str());
|
|||
|
BOOL handled = FALSE, ret = FALSE;
|
|||
|
|
|||
|
if (obj)
|
|||
|
ret = obj->handle_msg(msg, wp, lp);
|
|||
|
|
|||
|
return ret;
|
|||
|
}
|
|||
|
ATOM dlg_indicator::register_indicator_class(void)
|
|||
|
{
|
|||
|
WNDCLASSEXW wcex;
|
|||
|
|
|||
|
wcex.cbSize = sizeof(WNDCLASSEXW);
|
|||
|
wcex.style = WS_POPUP;
|
|||
|
wcex.lpfnWndProc = (WNDPROC)dlg_indicator::dlg_indicator_proc;
|
|||
|
wcex.cbClsExtra = 0;
|
|||
|
wcex.cbWndExtra = 0;
|
|||
|
wcex.hInstance = g_my_inst;
|
|||
|
wcex.hIcon = NULL;
|
|||
|
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
|
|||
|
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
|
|||
|
wcex.lpszMenuName = NULL;
|
|||
|
wcex.lpszClassName = dlg_indicator::indicator_class_name.c_str();
|
|||
|
wcex.hIconSm = NULL;
|
|||
|
|
|||
|
return RegisterClassExW(&wcex);
|
|||
|
}
|
|||
|
|
|||
|
void dlg_indicator::create(HANDLE wait)
|
|||
|
{
|
|||
|
MSG msg = { 0 };
|
|||
|
BOOL ret = TRUE;
|
|||
|
|
|||
|
hwnd_ = CreateDialogParamW(g_my_inst, MAKEINTRESOURCE(IDD_INDICATOR), NULL, &dlg_indicator::dlg_indicator_proc, (LPARAM)this);
|
|||
|
SetWindowLongW(GetDlgItem(hwnd_, IDC_STATIC_ERR), GWL_STYLE, GetWindowLong(GetDlgItem(hwnd_, IDC_STATIC_ERR), GWL_STYLE) | SS_OWNERDRAW);
|
|||
|
SetEvent(wait);
|
|||
|
|
|||
|
#ifdef USE_SOLE_WIN_THREAD
|
|||
|
while ((ret = GetMessage(&msg, NULL, 0, 0)))
|
|||
|
{
|
|||
|
if (ret == -1)
|
|||
|
break;
|
|||
|
|
|||
|
TranslateMessage(&msg);
|
|||
|
DispatchMessage(&msg);
|
|||
|
}
|
|||
|
DestroyWindow(hwnd_);
|
|||
|
#endif
|
|||
|
}
|
|||
|
BOOL dlg_indicator::handle_msg(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);
|
|||
|
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(HWND parent)
|
|||
|
{
|
|||
|
RECT rp, r;
|
|||
|
|
|||
|
if (IsWindow(parent))
|
|||
|
GetWindowRect(parent, &rp);
|
|||
|
else
|
|||
|
GetWindowRect(GetDesktopWindow(), &rp);
|
|||
|
GetWindowRect(hwnd_, &r);
|
|||
|
|
|||
|
parent_ = parent;
|
|||
|
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 消息处理程序
|