From 7a5262712b349569bb759ed058781015576f9a23 Mon Sep 17 00:00:00 2001 From: gb <741021719@qq.com> Date: Wed, 8 Mar 2023 16:06:36 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=95=8C=E9=9D=A2=E5=B8=83?= =?UTF-8?q?=E5=B1=80-=E5=AF=B9=E9=BD=90=E7=AC=AC=E4=BA=8C=E5=88=97?= =?UTF-8?q?=EF=BC=9B=E4=BF=9D=E5=AD=98=E4=BA=86API=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E6=A1=88=E5=90=8E=EF=BC=8C=E6=B8=85=E9=99=A4?= =?UTF-8?q?API=E8=AE=BE=E7=BD=AE=E6=A0=87=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sane/DlgPage.cpp | 74 ++++++++++++++++++++++++++++++++++++++---- sane/DlgPage.h | 2 ++ sane/DlgSaveScheme.cpp | 9 ++++- sane/DlgSetting.cpp | 8 +++-- 4 files changed, 84 insertions(+), 9 deletions(-) diff --git a/sane/DlgPage.cpp b/sane/DlgPage.cpp index c48c72e..52ce592 100644 --- a/sane/DlgPage.cpp +++ b/sane/DlgPage.cpp @@ -460,14 +460,18 @@ int dlg_base::get_width_diff_as_text_length(UINT id) return get_string_width(get_item_text(id).c_str()) - RECT_W(r); } -void dlg_base::offset_item(UINT id, int dx, int dy) +void dlg_base::offset_item(HWND wnd, int dx, int dy) { RECT r = { 0 }; - get_item_rect(id, &r, false); + GetWindowRect(wnd, &r); OffsetRect(&r, dx, dy); screen_2_client(&r); - MoveWindow(get_item(id), r.left, r.top, RECT_W(r), RECT_H(r), FALSE); + MoveWindow(wnd, r.left, r.top, RECT_W(r), RECT_H(r), FALSE); +} +void dlg_base::offset_item(UINT id, int dx, int dy) +{ + offset_item(get_item(id), dx, dy); } void dlg_base::expand_item(UINT id, int dx, int dy) { @@ -637,7 +641,7 @@ dlg_page::dlg_page(HWND parent, const wchar_t* name { size_.cx = size_.cy = 0; pos_.x = 12; - pos_.y = 8; + pos_.y = 15; create(); tips_wnd_.create(hwnd()); } @@ -685,6 +689,61 @@ void dlg_page::on_font_changed(void) HFONT font = get_font(); LOGFONTW lf = { 0 }; } +void dlg_page::align_second_control(bool ignore_single) +{ + int pos_2nd = 0, + xoff = 0, + cnt = 0, + prev_pos = 0, + sn = -1; + + // 1st: find the right-most position of the first control + for (auto& v : ctrls_) + { + int id = (int)GetWindowLong(v, GWL_ID); + + if (id != sn) + { + RECT r = { 0 }; + GetWindowRect(v, &r); + + if (ignore_single && cnt == 1) + { + pos_2nd = prev_pos; + } + prev_pos = pos_2nd; + sn = id; + cnt = 1; + if (r.right > pos_2nd) + pos_2nd = r.right; + } + else + cnt++; + } + + // 2nd: align the controls begin from position 2 + sn = -1; + for (auto& v : ctrls_) + { + int id = (int)GetWindowLong(v, GWL_ID); + RECT r = { 0 }; + + if (id != sn) + { + GetWindowRect(v, &r); + sn = id; + xoff = pos_2nd - r.right; + } + else + { + offset_item(v, xoff, 0); + GetWindowRect(v, &r); + screen_2_client(&r); + if (size_.cx < r.right + dlg_page::gap_x) + size_.cx = r.right + dlg_page::gap_x; + } + } +} HWND dlg_page::create_label(int sn, const wchar_t* title, int x, int y, SIZE size) { @@ -721,8 +780,8 @@ HWND dlg_page::create_slider(int sn, int x, int y, double lower, double upper, d size->cx *= 100; if (size->cx < 100) size->cx = 100; - if (size->cx > 300) - size->cx = 300; + if (size->cx > 200) + size->cx = 200; if (upper > lower && size->cx / (upper - lower) < ticks_limit) style |= TBS_NOTICKS; else @@ -1555,6 +1614,7 @@ bool dlg_page::add_control(int sn, const SANE_Option_Descriptor* desc, void* cur std::wstring t(local_trans::lang_trans_between_hz936(CONST_STRING_SET_AREA)); id_custom_area_ = sn; + text.cx += dlg_page::gap_x; wnd = CreateWindowW(WC_BUTTONW, t.c_str(), WS_CHILD | WS_VISIBLE, pos_.x + text.cx, pos_.y - 1, w, text.cy + 3, hwnd(), NULL, g_my_inst, NULL); text.cx += w + dlg_page::gap_x; EnableWindow(wnd, (desc->cap & SANE_CAP_INACTIVE) == 0 && *(SANE_Bool*)cur_val == SANE_TRUE); @@ -1584,6 +1644,7 @@ bool dlg_page::add_control(int sn, const SANE_Option_Descriptor* desc, void* cur std::wstring t(local_trans::lang_trans_between_hz936(CONST_STRING_SET_TONE)); id_custom_gamma_ = sn; + text.cx += dlg_page::gap_x; wnd = CreateWindowW(WC_BUTTONW, t.c_str(), WS_CHILD | WS_VISIBLE, pos_.x + text.cx, pos_.y - 1, w, text.cy + 3, hwnd(), NULL, g_my_inst, NULL); text.cx += w + dlg_page::gap_x; EnableWindow(wnd, (desc->cap& SANE_CAP_INACTIVE) == 0 && *(SANE_Bool*)cur_val == SANE_TRUE); @@ -1608,6 +1669,7 @@ bool dlg_page::add_control(int sn, const SANE_Option_Descriptor* desc, void* cur void dlg_page::add_control_done(void) { done_ = true; + align_second_control(true); } SIZE dlg_page::desired_size(void) { diff --git a/sane/DlgPage.h b/sane/DlgPage.h index 0a2952f..b836d37 100644 --- a/sane/DlgPage.h +++ b/sane/DlgPage.h @@ -92,6 +92,7 @@ public: bool get_item_rect(UINT id, LPRECT r, bool client = true); std::wstring get_item_text(UINT id); int get_width_diff_as_text_length(UINT id); // + void offset_item(HWND wnd, int dx, int dy); void offset_item(UINT id, int dx, int dy); void expand_item(UINT id, int dx, int dy); bool set_item_text(UINT id, const wchar_t* text); @@ -182,6 +183,7 @@ class dlg_page : public dlg_base BOOL handle_message(UINT msg, WPARAM wp, LPARAM lp) override; void on_font_changed(void) override; + void align_second_control(bool ignore_single = false); // align the second control of a option controls HWND create_label(int sn, const wchar_t* title, int x, int y, SIZE size); HWND create_slider(int sn, int x, int y, double lower, double upper, double step, double pos, LPSIZE size, bool is_double); diff --git a/sane/DlgSaveScheme.cpp b/sane/DlgSaveScheme.cpp index 4465116..87f4899 100644 --- a/sane/DlgSaveScheme.cpp +++ b/sane/DlgSaveScheme.cpp @@ -19,6 +19,7 @@ dlg_save_scheme::dlg_save_scheme(HWND parent) : dlg_base(parent, IDD_SAVE_SCHEME std::wstring title(local_trans::lang_trans_between_hz936(CONST_STRING_SAVE_TITLE)); int dif = 0; + RECT rc = { 0 }; SetWindowTextW(hwnd(), title.c_str()); @@ -34,7 +35,9 @@ dlg_save_scheme::dlg_save_scheme(HWND parent) : dlg_base(parent, IDD_SAVE_SCHEME set_item_text(IDC_RADIO_NEW, title.c_str()); dif = set_item_fit_to_text(IDC_RADIO_NEW); offset_item(IDC_NAME, dif, 0); - // expand_item(IDC_NAME, dif, 0); + GetWindowRect(get_item(IDC_NAME), &rc); + if(dif < RECT_W(rc) / 2) + expand_item(IDC_NAME, -dif, 0); { RECT r = { 0 }, rc = { 0 }; @@ -43,8 +46,10 @@ dlg_save_scheme::dlg_save_scheme(HWND parent) : dlg_base(parent, IDD_SAVE_SCHEME GetWindowRect(get_item(IDC_NAME), &rc); if (rc.right + WIDTH_MARGINS > r.right) { + dif = rc.right + WIDTH_MARGINS - r.right; r.right = rc.right + WIDTH_MARGINS; MoveWindow(hwnd(), r.left, r.top, RECT_W(r), RECT_H(r), FALSE); + offset_item(IDOK, dif / 2, 0); } } @@ -194,8 +199,10 @@ void dlg_save_scheme::set_info(const wchar_t* name, std::vector& e GetWindowRect(hwnd(), &r); if (RECT_W(r) < RECT_W(rc) + WIDTH_MARGINS) { + int diff = r.left + RECT_W(rc) + WIDTH_MARGINS - r.right; r.right = r.left + RECT_W(rc) + WIDTH_MARGINS; MoveWindow(hwnd(), r.left, r.top, RECT_W(r), RECT_H(r), FALSE); + offset_item(IDOK, diff / 2, 0); } } diff --git a/sane/DlgSetting.cpp b/sane/DlgSetting.cpp index 1a8039f..23ceadd 100644 --- a/sane/DlgSetting.cpp +++ b/sane/DlgSetting.cpp @@ -289,8 +289,8 @@ void dlg_setting::on_init_dialog(void) { if (page) { - dlg_base::get_max_size(size, page->desired_size()); page->add_control_done(); + dlg_base::get_max_size(size, page->desired_size()); } page = add_tab(desc->title); } @@ -321,8 +321,8 @@ void dlg_setting::on_init_dialog(void) } if (page) { - dlg_base::get_max_size(size, page->desired_size()); page->add_control_done(); + dlg_base::get_max_size(size, page->desired_size()); } if (size.cx || size.cy || IsWindow(tab_)) @@ -554,7 +554,11 @@ void dlg_setting::save_changes_to_cur_scheme(int reason) std::string name(local_trans::u2a(dlg.get_name().c_str(), CP_UTF8)); if (cfg_->add_scheme(twain_schm_, name.c_str()) && reason == SAVE_REASON_QUIT_UI) + { cfg_->select_scheme(name.c_str()); + if (twain_set_) + *twain_set_ = false; + } } } twain_schm_->release();