mirror of
https://github.com/HIllya51/LunaTranslator.git
synced 2025-01-15 08:53:53 +08:00
.
This commit is contained in:
parent
08c2649427
commit
e39a4f82e1
@ -1,7 +1,6 @@
|
|||||||
#ifndef WINXP
|
|
||||||
#include <wil/com.h>
|
|
||||||
#include <uiautomation.h>
|
#include <uiautomation.h>
|
||||||
#endif
|
|
||||||
DECLARE_API void showintab(HWND hwnd, bool show, bool tool)
|
DECLARE_API void showintab(HWND hwnd, bool show, bool tool)
|
||||||
{
|
{
|
||||||
// WS_EX_TOOLWINDOW可以立即生效,WS_EX_APPWINDOW必须切换焦点才生效。但是WS_EX_TOOLWINDOW会改变窗口样式,因此只对无边框窗口使用。
|
// WS_EX_TOOLWINDOW可以立即生效,WS_EX_APPWINDOW必须切换焦点才生效。但是WS_EX_TOOLWINDOW会改变窗口样式,因此只对无边框窗口使用。
|
||||||
@ -132,59 +131,57 @@ DECLARE_API bool check_window_viewable(HWND hwnd)
|
|||||||
|
|
||||||
DECLARE_API void GetSelectedText(void (*cb)(const wchar_t *))
|
DECLARE_API void GetSelectedText(void (*cb)(const wchar_t *))
|
||||||
{
|
{
|
||||||
#ifndef WINXP
|
|
||||||
CO_INIT co;
|
CO_INIT co;
|
||||||
CHECK_FAILURE_NORET(co);
|
CHECK_FAILURE_NORET(co);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// 初始化 COM
|
// 初始化 COM
|
||||||
wil::com_ptr<IUIAutomation> automation;
|
CComPtr<IUIAutomation> automation;
|
||||||
if (FAILED(CoCreateInstance(CLSID_CUIAutomation, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&automation))) || !automation)
|
if (FAILED(CoCreateInstance(CLSID_CUIAutomation, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&automation))) || !automation)
|
||||||
{
|
{
|
||||||
throw std::runtime_error("无法初始化 UI Automation.");
|
throw std::runtime_error("无法初始化 UI Automation.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取焦点元素
|
// 获取焦点元素
|
||||||
wil::com_ptr<IUIAutomationElement> focusedElement;
|
CComPtr<IUIAutomationElement> focusedElement;
|
||||||
if (FAILED(automation->GetFocusedElement(&focusedElement)) || !focusedElement)
|
if (FAILED(automation->GetFocusedElement(&focusedElement)) || !focusedElement)
|
||||||
{
|
{
|
||||||
throw std::runtime_error("无法获取当前焦点元素.");
|
throw std::runtime_error("无法获取当前焦点元素.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否支持 TextPattern
|
// 检查是否支持 TextPattern
|
||||||
wil::com_ptr<IUIAutomationTextPattern> textPattern;
|
CComPtr<IUIAutomationTextPattern> textPattern;
|
||||||
if (FAILED(focusedElement->GetCurrentPatternAs(UIA_TextPatternId, IID_PPV_ARGS(&textPattern))) || !textPattern)
|
if (FAILED(focusedElement->GetCurrentPatternAs(UIA_TextPatternId, IID_PPV_ARGS(&textPattern))) || !textPattern)
|
||||||
{
|
{
|
||||||
throw std::runtime_error("当前元素不支持 TextPattern.");
|
throw std::runtime_error("当前元素不支持 TextPattern.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取选定的文本范围
|
// 获取选定的文本范围
|
||||||
wil::com_ptr<IUIAutomationTextRangeArray> selection;
|
CComPtr<IUIAutomationTextRangeArray> selection;
|
||||||
if (FAILED(textPattern->GetSelection(&selection)) || !selection)
|
if (FAILED(textPattern->GetSelection(&selection)) || !selection)
|
||||||
{
|
{
|
||||||
throw std::runtime_error("无法获取选定的文本范围.");
|
throw std::runtime_error("无法获取选定的文本范围.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取第一个选定范围
|
// 获取第一个选定范围
|
||||||
wil::com_ptr<IUIAutomationTextRange> range;
|
CComPtr<IUIAutomationTextRange> range;
|
||||||
if (FAILED(selection->GetElement(0, &range)) || !range)
|
if (FAILED(selection->GetElement(0, &range)) || !range)
|
||||||
{
|
{
|
||||||
throw std::runtime_error("没有选定文本.");
|
throw std::runtime_error("没有选定文本.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 提取文本
|
// 提取文本
|
||||||
wil::unique_bstr text;
|
CComBSTR text;
|
||||||
if (FAILED(range->GetText(-1, &text)) || !text)
|
if (FAILED(range->GetText(-1, &text)) || !text)
|
||||||
{
|
{
|
||||||
throw std::runtime_error("无法提取选定的文本.");
|
throw std::runtime_error("无法提取选定的文本.");
|
||||||
}
|
}
|
||||||
cb(text.get());
|
cb(text);
|
||||||
}
|
}
|
||||||
catch (std::exception &e)
|
catch (std::exception &e)
|
||||||
{
|
{
|
||||||
printf(e.what());
|
printf(e.what());
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DECLARE_API void *get_allAccess_ptr()
|
DECLARE_API void *get_allAccess_ptr()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user