From 80b19567567338eac9b88bf4228a335abb25c507 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <1173718158@qq.com> Date: Wed, 25 Dec 2024 22:59:54 +0800 Subject: [PATCH] . --- cpp/winsharedutils/hwnd.cpp | 61 ++++++- docs/en/fastkeys.md | 3 + docs/ja/fastkeys.md | 3 + docs/zh/fastkeys.md | 3 + py/LunaTranslator/LunaTranslator.py | 1 - py/LunaTranslator/cishu/jisho.py | 6 +- py/LunaTranslator/gui/dialog_savedgame.py | 68 ++++---- py/LunaTranslator/gui/dialog_savedgame_v3.py | 18 ++- py/LunaTranslator/gui/setting.py | 2 + py/LunaTranslator/gui/setting_cishu.py | 6 +- py/LunaTranslator/gui/setting_display_text.py | 3 +- py/LunaTranslator/gui/setting_hotkey.py | 5 +- py/LunaTranslator/gui/showword.py | 70 +++++--- py/LunaTranslator/gui/usefulwidget.py | 151 ++++++++---------- py/LunaTranslator/winsharedutils.py | 10 ++ py/files/defaultconfig/config.json | 5 + 16 files changed, 247 insertions(+), 168 deletions(-) diff --git a/cpp/winsharedutils/hwnd.cpp b/cpp/winsharedutils/hwnd.cpp index 6db6eecc..be530f8f 100644 --- a/cpp/winsharedutils/hwnd.cpp +++ b/cpp/winsharedutils/hwnd.cpp @@ -1,4 +1,6 @@  +#include +#include DECLARE_API void showintab(HWND hwnd, bool show, bool tool) { // WS_EX_TOOLWINDOW可以立即生效,WS_EX_APPWINDOW必须切换焦点才生效。但是WS_EX_TOOLWINDOW会改变窗口样式,因此只对无边框窗口使用。 @@ -154,4 +156,61 @@ DECLARE_API bool check_window_viewable(HWND hwnd) return false; RECT _; return IntersectRect(&_, &windowRect, &monitorInfo.rcWork); -} \ No newline at end of file +} + +DECLARE_API void GetSelectedText(void (*cb)(const wchar_t *)) +{ + CoInitialize(nullptr); + try + { + // 初始化 COM + wil::com_ptr automation; + if (FAILED(CoCreateInstance(CLSID_CUIAutomation, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&automation))) || !automation) + { + throw std::runtime_error("无法初始化 UI Automation."); + } + + // 获取焦点元素 + wil::com_ptr focusedElement; + if (FAILED(automation->GetFocusedElement(&focusedElement)) || !focusedElement) + { + throw std::runtime_error("无法获取当前焦点元素."); + } + + // 检查是否支持 TextPattern + wil::com_ptr textPattern; + if (FAILED(focusedElement->GetCurrentPatternAs(UIA_TextPatternId, IID_PPV_ARGS(&textPattern))) || !textPattern) + { + throw std::runtime_error("当前元素不支持 TextPattern."); + } + + // 获取选定的文本范围 + wil::com_ptr selection; + if (FAILED(textPattern->GetSelection(&selection)) || !selection) + { + throw std::runtime_error("无法获取选定的文本范围."); + } + + // 获取第一个选定范围 + wil::com_ptr range; + if (FAILED(selection->GetElement(0, &range)) || !range) + { + throw std::runtime_error("没有选定文本."); + } + + // 提取文本 + BSTR text; + if (FAILED(range->GetText(-1, &text)) || !text) + { + throw std::runtime_error("无法提取选定的文本."); + } + std::wstring selectedText(text, SysStringLen(text)); + SysFreeString(text); + cb(selectedText.c_str()); + } + catch (std::exception &e) + { + printf(e.what()); + } + CoUninitialize(); +} diff --git a/docs/en/fastkeys.md b/docs/en/fastkeys.md index cb823d81..986fe586 100644 --- a/docs/en/fastkeys.md +++ b/docs/en/fastkeys.md @@ -130,6 +130,9 @@ ### **Dictionary Lookup** +1. #### Search for keywords + Search for words in the text currently selected by the mouse + 1. #### Anki Recording Shortcut key for the recording function in the Anki add interface in the dictionary lookup window. diff --git a/docs/ja/fastkeys.md b/docs/ja/fastkeys.md index 6ef47c18..caede8e8 100644 --- a/docs/ja/fastkeys.md +++ b/docs/ja/fastkeys.md @@ -130,6 +130,9 @@ ### **辞書検索** +1. #### 単語を調べる + 現在のマウスで選択されているテキストの単語検索 + 1. #### Anki録音 辞書検索ウィンドウのAnki追加インターフェースの録音機能のショートカットキー。 diff --git a/docs/zh/fastkeys.md b/docs/zh/fastkeys.md index 94ec8257..7d083f52 100644 --- a/docs/zh/fastkeys.md +++ b/docs/zh/fastkeys.md @@ -131,6 +131,9 @@ ### **查词** +1. #### 查词 + 对当前鼠标选取到的文本查词 + 1. #### Anki 录音 查词窗口中的Anki添加界面中的录音功能的快捷键 diff --git a/py/LunaTranslator/LunaTranslator.py b/py/LunaTranslator/LunaTranslator.py index c8364be1..99518e57 100644 --- a/py/LunaTranslator/LunaTranslator.py +++ b/py/LunaTranslator/LunaTranslator.py @@ -387,7 +387,6 @@ class MAINUI: maybehaspremt = {} skip_other_on_success = False fix_rank = globalconfig["fix_translate_rank_rank"].copy() - print(is_auto_run , globalconfig["fanyi"]["rengong"].get("manual", False)) if ("rengong" in self.translators) and ( not (is_auto_run and globalconfig["fanyi"]["rengong"].get("manual", False)) ): diff --git a/py/LunaTranslator/cishu/jisho.py b/py/LunaTranslator/cishu/jisho.py index 8bf902a3..4471432a 100644 --- a/py/LunaTranslator/cishu/jisho.py +++ b/py/LunaTranslator/cishu/jisho.py @@ -12,17 +12,17 @@ class jisho(cishubase): contents = [] idx = 0 for title, res in allres: - idx += 1 btns.append( """""".format( - idx=idx, title=title, klass='' if idx==0 else ' active' + idx=idx, title=title, klass='' if idx!=0 else ' active' ) ) contents.append( """
{res}
""".format( - idx=idx, res=res,klass='' if idx==0 else ' active' + idx=idx, res=res,klass='' if idx!=0 else ' active' ) ) + idx += 1 commonstyle = """