// DlgIndicator.cpp: 实现文件 // #include "DlgPage.h" #include "resource.h" #include "scanned_img.h" // for local_trans ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // dlg_base 对话框 extern HMODULE g_my_inst; std::wstring dlg_base::prop_name = L"dlg_base_object_prop_name"; dlg_base::dlg_base(HWND parent, UINT idd) : parent_(parent), hwnd_(NULL), idd_(idd) { } dlg_base::~dlg_base() { if(IsWindow(hwnd_)) DestroyWindow(hwnd_); } BOOL CALLBACK dlg_base::dlg_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { if (msg == WM_INITDIALOG) { dlg_base* obj = (dlg_base*)lp; SetPropW(hwnd, dlg_base::prop_name.c_str(), (HANDLE)obj); obj->hwnd_ = hwnd; } dlg_base *obj = (dlg_base*)GetPropW(hwnd, dlg_base::prop_name.c_str()); BOOL ret = FALSE; if (obj) ret = obj->handle_message(msg, wp, lp); return ret; } bool dlg_base::get_max_size(SIZE& dst, const SIZE& src) { bool changed = false; if (dst.cx < src.cx) { dst.cx = src.cx; changed = true; } if (dst.cy < src.cy) { dst.cy = src.cy; changed = true; } return changed; } bool dlg_base::get_max_size(SIZE& dst, int cx, int cy) { bool changed = false; if (dst.cx < cx) { dst.cx = cx; changed = true; } if (dst.cy < cy) { dst.cy = cy; changed = true; } return changed; } BOOL dlg_base::handle_message(UINT msg, WPARAM wp, LPARAM lp) { return FALSE; } void dlg_base::on_font_changed(void) { } void dlg_base::create(void) { hwnd_ = CreateDialogParamW(g_my_inst, MAKEINTRESOURCE(idd_), parent_, &dlg_base::dlg_proc, (LPARAM)this); } HWND dlg_base::hwnd(void) { return hwnd_; } void dlg_base::screen_2_client(LPRECT r) { POINT pt = { r->left, r->top }; ScreenToClient(hwnd_, &pt); OffsetRect(r, pt.x - r->left, pt.y - r->top); } void dlg_base::client_2_screen(LPRECT r) { POINT pt = { r->left, r->top }; ClientToScreen(hwnd_, &pt); OffsetRect(r, pt.x - r->left, pt.y - r->top); } HWND dlg_base::get_item(UINT id) { return GetDlgItem(hwnd_, id); } BOOL dlg_base::set_font(HFONT font) { BOOL ret = SendMessage(hwnd_, WM_SETFONT, (WPARAM)font, 1) == 0; if (ret) on_font_changed(); return ret; } HFONT dlg_base::get_font(void) { return (HFONT)SendMessage(hwnd_, WM_GETFONT, 0, 0); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // dlg_page 对话框 UINT dlg_page::dyn_id_base = 3000; int dlg_page::gap_x = 20; int dlg_page::gap_y = 12; dlg_page::dlg_page(HWND parent, const wchar_t* name) : dlg_base(parent, IDD_PAGE), name_(name ? name : L""), ctrl_id_(0) { size_.cx = size_.cy = 0; create(); refresh_font(); } dlg_page::~dlg_page() {} BOOL dlg_page::handle_message(UINT msg, WPARAM wp, LPARAM lp) { return FALSE; } void dlg_page::on_font_changed(void) { refresh_font(); } void dlg_page::refresh_font(void) { HFONT font = get_font(); LOGFONTW lf = { 0 }; GetObjectW(font, sizeof(lf), &lf); font_size_.cx = lf.lfWidth; font_size_.cy = lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight; } bool dlg_page::create_control_bool(int sn, const SANE_Option_Descriptor* desc, void* cur_val) { std::wstring title(local_trans::a2u(desc->title, CP_UTF8)); bool ret = true; bool now = *(SANE_Bool*)cur_val == SANE_TRUE ? true : false; int w = title.length() * font_size_.cx + 18, h = font_size_.cy; HWND wnd = CreateWindowW(L"Button", L"check", WS_CHILD | WS_VISIBLE | WS_MAXIMIZEBOX, pos_.x, pos_.y, w, h, hwnd(), NULL, g_my_inst, NULL); pos_.y += h + dlg_page::gap_y; if (size_.cx < w) size_.cx = w; if (size_.cy < pos_.y) size_.cy = pos_.y; if (desc->cap & SANE_CAP_INACTIVE) EnableWindow(wnd, FALSE); ShowWindow(wnd, SW_SHOW); if (now) SendMessage(wnd, BM_SETCHECK, (WPARAM)BST_CHECKED, 0); return ret; } bool dlg_page::create_control_int(int sn, const SANE_Option_Descriptor* desc, void* cur_val) { bool ret = true; return ret; } bool dlg_page::create_control_float(int sn, const SANE_Option_Descriptor* desc, void* cur_val) { bool ret = true; return ret; } bool dlg_page::create_control_string(int sn, const SANE_Option_Descriptor* desc, void* cur_val) { bool ret = true; return ret; } bool dlg_page::create_control_button(int sn, const SANE_Option_Descriptor* desc, void* cur_val) { bool ret = true; return ret; } bool dlg_page::add_control(int sn, const SANE_Option_Descriptor* desc, void* cur_val) { bool ret = false; struct { int sane_type; bool(dlg_page::* func)(int, const SANE_Option_Descriptor*, void*); }creat[] = { {SANE_TYPE_BOOL, &dlg_page::create_control_bool} , {SANE_TYPE_INT, &dlg_page::create_control_int} , {SANE_TYPE_FIXED, &dlg_page::create_control_float} , {SANE_TYPE_STRING, &dlg_page::create_control_string} , {SANE_TYPE_BUTTON, &dlg_page::create_control_button} }; for (int i = 0; i < _countof(creat); ++i) { if (creat[i].sane_type == desc->type) { ret = (this->*creat[i].func)(sn, desc, cur_val); break; } } return ret; } SIZE dlg_page::desired_size(void) { return size_; } void dlg_page::show(void) { ShowWindow(hwnd_, SW_SHOW); } void dlg_page::hide(void) { ShowWindow(hwnd_, SW_HIDE); } const wchar_t* dlg_page::name(void) { return name_.c_str(); }