自动保存配置到user/appdata/local/product/config下

This commit is contained in:
gb 2022-09-23 14:17:41 +08:00
parent fc8ca14ec5
commit ffa101ae6b
8 changed files with 142 additions and 3 deletions

View File

@ -68,6 +68,20 @@ BOOL CALLBACK dlg_base::dlg_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
return ret;
}
void dlg_base::screen_2_client(HWND wnd, LPRECT r)
{
POINT pt = { r->left, r->top };
ScreenToClient(wnd, &pt);
OffsetRect(r, pt.x - r->left, pt.y - r->top);
}
void dlg_base::client_2_screen(HWND wnd, LPRECT r)
{
POINT pt = { r->left, r->top };
ClientToScreen(wnd, &pt);
OffsetRect(r, pt.x - r->left, pt.y - r->top);
}
bool dlg_base::get_max_size(SIZE& dst, const SIZE& src)
{
bool changed = false;
@ -120,6 +134,39 @@ int dlg_base::select_combo_text(HWND combo, const wchar_t* text)
return ret;
}
HWND dlg_base::create_tooltip_for(HWND ctrl, const wchar_t* tips)
{
HWND tipwnd = CreateWindowExW(NULL, TOOLTIPS_CLASSW, NULL,
WS_POPUP |TTS_ALWAYSTIP | TTS_BALLOON,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
ctrl, NULL,
GetModuleHandle(NULL), NULL),
parent = GetParent(ctrl);
if (tipwnd)
{
TTTOOLINFOW toolInfo = { 0 };
SetWindowPos(tipwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
toolInfo.cbSize = sizeof(toolInfo);
toolInfo.hwnd = parent;
toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
toolInfo.uId = (UINT_PTR)ctrl;
toolInfo.lpszText = (wchar_t*)(DWORD_PTR)tips; // LPSTR_TEXTCALLBACK - WM_NOTIFY.TTN_GETDISPINFO
toolInfo.hinst = GetModuleHandle(NULL);
GetWindowRect(ctrl, &toolInfo.rect);
dlg_base::screen_2_client(parent, &toolInfo.rect);
if (!SendMessageW(tipwnd, TTM_ADDTOOL, 0, (LPARAM)&toolInfo))
{
DWORD err = GetLastError();
DestroyWindow(tipwnd);
tipwnd = NULL;
}
}
return tipwnd;
}
BOOL dlg_base::handle_message(UINT msg, WPARAM wp, LPARAM lp)
{
@ -131,6 +178,7 @@ void dlg_base::on_font_changed(void)
}
void dlg_base::create(void)
{
// InitCommonControls();
hwnd_ = CreateDialogParamW(g_my_inst, MAKEINTRESOURCE(idd_), parent_, (DLGPROC)&dlg_base::dlg_proc, (LPARAM)this);
}
void dlg_base::notify_ui_event(int ev)
@ -266,12 +314,25 @@ void dlg_base::show_scroll_bar(int bar, bool show)
SetWindowLong(hwnd(), GWL_STYLE, style);
}
}
bool dlg_base::track_mouse_hover(void)
{
return true;
TRACKMOUSEEVENT tme = { 0 };
tme.cbSize = sizeof(tme);
tme.dwFlags = TME_HOVER;
tme.dwHoverTime = HOVER_DEFAULT;
tme.hwndTrack = hwnd_;
return TrackMouseEvent(&tme);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// dlg_page 对话框
std::wstring dlg_page::property_type = L"option_type";
std::wstring dlg_page::property_host = L"option_host_wnd";
std::wstring dlg_page::property_size = L"option_size";
std::wstring dlg_page::property_tooltip_wnd = L"option_tooltips_wnd";
UINT dlg_page::dyn_id_base = 3000;
int dlg_page::gap_x = 20;
int dlg_page::gap_y = 15;
@ -294,8 +355,13 @@ dlg_page::dlg_page(HWND parent, const wchar_t* name
dlg_page::~dlg_page()
{
for (auto& v : ctrls_)
{
HWND tooltip = (HWND)GetPropW(v, dlg_page::property_tooltip_wnd.c_str());
if (tooltip)
DestroyWindow(tooltip);
DestroyWindow(v);
}
}
BOOL dlg_page::handle_message(UINT msg, WPARAM wp, LPARAM lp)
{
@ -320,6 +386,10 @@ BOOL dlg_page::handle_message(UINT msg, WPARAM wp, LPARAM lp)
if(hsb_)
on_hscroll(HIWORD(wp), LOWORD(wp));
break;
case WM_MOUSEHOVER:
track_mouse_hover();
on_mouse_hover(LOWORD(lp), HIWORD(lp), wp);
break;
default:
ret = FALSE;
}
@ -711,6 +781,7 @@ BOOL dlg_page::on_notify(int ctrl_id, LPNMHDR pnmh)
{
wchar_t cls[128] = { 0 };
pnmh->code == NM_TOOLTIPSCREATED;
GetClassNameW((HWND)pnmh->hwndFrom, cls, _countof(cls) - 1);
if (IS_TRACKBAR(cls))
{
@ -1072,6 +1143,28 @@ void dlg_page::on_hscroll(int pos, int sb_ev)
SetScrollPos(hwnd(), SB_HORZ, hsb_pos_, TRUE);
}
}
void dlg_page::on_mouse_hover(int x, int y, int flag)
{
POINT pt = { x, y };
HWND ctrl = NULL;
ClientToScreen(hwnd_, &pt);
ctrl = WindowFromPoint(pt);
if (IsWindow(ctrl))
{
int id = GetWindowLong(ctrl, GWL_ID);
if (id > dlg_page::dyn_id_base)
{
id -= dlg_page::dyn_id_base;
const SANE_Option_Descriptor* desc = sane_.sane_get_option_descriptor_api(dev_, id);
if (desc)
{
std::wstring tips(local_trans::a2u(desc->title, CP_UTF8));
// MessageBoxW(NULL, tips.c_str(), L"Tips", MB_OK);
}
}
}
}
bool dlg_page::add_control(int sn, const SANE_Option_Descriptor* desc, void* cur_val)
{
@ -1113,6 +1206,12 @@ bool dlg_page::add_control(int sn, const SANE_Option_Descriptor* desc, void* cur
ret = IsWindow(wnd);
if (ret)
{
// if (desc->desc && *desc->desc)
// {
// HWND tooltip = dlg_base::create_tooltip_for(wnd, local_trans::a2u(desc->desc, CP_UTF8).c_str());
// if (tooltip)
// SetPropW(wnd, dlg_page::property_tooltip_wnd.c_str(), tooltip);
// }
SetPropW(wnd, dlg_page::property_type.c_str(), (HANDLE)creat[i].sane_type);
SetPropW(wnd, dlg_page::property_size.c_str(), (HANDLE)desc->size);
if (desc->cap & SANE_CAP_INACTIVE)

View File

@ -41,9 +41,12 @@ public:
dlg_base(HWND parent, UINT idd);
virtual ~dlg_base();
static void screen_2_client(HWND wnd, LPRECT r);
static void client_2_screen(HWND wnd, LPRECT r);
static bool get_max_size(SIZE& dst, const SIZE& src); // return whether changed dst
static bool get_max_size(SIZE& dst, int cx, int cy); // return whether changed dst
static int select_combo_text(HWND combo, const wchar_t* text);
static HWND create_tooltip_for(HWND ctrl, const wchar_t* tips);
public:
void set_ui_event_notify(void(__stdcall* notify)(int, void*, void*), void* param);
@ -57,6 +60,7 @@ public:
HFONT get_font(void);
int get_string_width(const wchar_t* str);
void show_scroll_bar(int bar = SB_VERT, bool show = true);
bool track_mouse_hover(void);
};
class dlg_page : public dlg_base
@ -85,6 +89,7 @@ class dlg_page : public dlg_base
static std::wstring property_type;
static std::wstring property_host;
static std::wstring property_size;
static std::wstring property_tooltip_wnd;
static UINT dyn_id_base;
static int gap_x;
static int gap_y;
@ -123,6 +128,7 @@ class dlg_page : public dlg_base
BOOL on_mouse_wheel(WORD vkey, short delta, short x, short y);
void on_vscroll(int pos, int sb_ev);
void on_hscroll(int pos, int sb_ev);
void on_mouse_hover(int x, int y, int flag);
public:
dlg_page(HWND parent, const wchar_t* name, LPSANEAPI api, SANE_Handle dev);

View File

@ -74,6 +74,9 @@ void dlg_setting::handle_command(WORD code, WORD id, HANDLE ctrl)
{
if (id == IDOK)
{
if (cfg_)
cfg_->save_to(NULL);
notify_over();
}
else if (id == IDC_BUTTON_HELP)

View File

@ -833,6 +833,7 @@ namespace gb
if (bkp_)
delete bkp_;
bkp_ = NULL;
file_ = L"";
}
std::string sane_config::to_hex_letter(const char* data, size_t bytes)
{
@ -901,7 +902,10 @@ namespace gb
{
jsn_ = new gb::json();
if (jsn_->attach_text(&raw[0]))
{
ret = true;
file_ = file;
}
else
{
delete jsn_;
@ -934,6 +938,9 @@ namespace gb
bool ret = false;
std::string encode(to_text_stream());
if (!file || *file == 0)
file = file_.c_str();
if (encode.length())
{
FILE* dst = _wfopen(file, L"wb");

View File

@ -81,6 +81,7 @@ namespace gb
class sane_config
{
std::wstring file_;
json* jsn_;
json* bkp_;
json* def_val_;

View File

@ -33,7 +33,7 @@
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 108
#define _APS_NEXT_RESOURCE_VALUE 109
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1020
#define _APS_NEXT_SYMED_VALUE 101

View File

@ -125,6 +125,16 @@ scanner::scanner(SCANNERID id) : handle_(NULL), id_(id), ex_id_(EXTENSION_ID_BAS
{
cfg_ = new gb::sane_config();
tmp_path_ = local_trans::a2u(hg_sane_middleware::sane_path().c_str());
{
char* tmp = getenv("LOCALAPPDATA");
if (tmp)
{
tmp_path_ = local_trans::a2u(tmp) + L"\\";
tmp_path_ += local_trans::a2u(PRODUCT_VENDOR) + L"Scan\\";
CreateDirectoryW(tmp_path_.c_str(), NULL);
}
}
cfg_path_ = tmp_path_ + L"config";
CreateDirectoryW(cfg_path_.c_str(), NULL);
cfg_path_ += L"\\";
@ -138,6 +148,7 @@ scanner::scanner(SCANNERID id) : handle_(NULL), id_(id), ex_id_(EXTENSION_ID_BAS
}
scanner::~scanner()
{
close();
if (cfg_)
delete cfg_;
}
@ -290,8 +301,8 @@ int scanner::open(void)
callback::reg_callback(handle_, this);
scanner_name_ = local_trans::a2u(name.c_str(), CP_UTF8);
ret = init_options_id();
load_config((cfg_path_ + scanner_name_).c_str());
// apply_config();
load_config((cfg_path_ + scanner_name_ + L".cfg").c_str());
apply_config();
}
else
{
@ -305,7 +316,10 @@ int scanner::close(void)
{
callback::unreg_callback(this);
if (handle_)
{
hg_sane_middleware::instance()->close_device(handle_);
// save_config((cfg_path_ + scanner_name_ + L".cfg").c_str());
}
handle_ = NULL;
ex_id_ = EXTENSION_ID_BASE;

View File

@ -1225,6 +1225,15 @@ Twpp::Result huagao_ds::imageFileXferGet(const Twpp::Identity& origin)
conv.dst.data_len = target.length();
while (!MoveFileA(target.c_str(), file.c_str()))
{
if (cv_e == 0)
{
std::wstring info(L"[convert image format] Move '");
wchar_t buf[80] = { 0 };
swprintf_s(buf, _countof(buf) - 1, L"%d\r\n", GetLastError());
info += load_sane_util::ansi2unic(target.c_str()) + L"' to '" + load_sane_util::ansi2unic(file.c_str()) + L"' failed: ";
load_sane_util::log_info((info + buf).c_str(), 0);
}
if (++cv_e >= 9)
break;
Sleep(30);