惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
月光博客
月光博客
AI
AI
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
CXSECURITY Database RSS Feed - CXSecurity.com
WordPress大学
WordPress大学
GbyAI
GbyAI
L
Lohrmann on Cybersecurity
O
OpenAI News
Schneier on Security
Schneier on Security
P
Palo Alto Networks Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Troy Hunt's Blog
V2EX - 技术
V2EX - 技术
W
WeLiveSecurity
L
LINUX DO - 最新话题
人人都是产品经理
人人都是产品经理
S
Security Affairs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
Arctic Wolf
Recorded Future
Recorded Future
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
G
GRAHAM CLULEY
N
Netflix TechBlog - Medium
TaoSecurity Blog
TaoSecurity Blog
C
Check Point Blog
Scott Helme
Scott Helme
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Vercel News
Vercel News
The Hacker News
The Hacker News
Y
Y Combinator Blog
Latest news
Latest news
SecWiki News
SecWiki News
Hugging Face - Blog
Hugging Face - Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
Webroot Blog
Webroot Blog
Google DeepMind News
Google DeepMind News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Cisco Blogs
博客园_首页
H
Hackread – Cybersecurity News, Data Breaches, AI and More
宝玉的分享
宝玉的分享
L
LINUX DO - 热门话题

C++

小孩马上高一,但是想学信奥赛 c++,有什么学 c++的书推荐? - V2EX 真是意想不到的操作:有好几个人一起协作向 C++库 fmtlib 加上了 C11 包装接口,确实能用 - V2EX 基于 C++20 协程编写 gRPC 客户端与服务端 似乎在 C 的领域,让一个新程序“为未来准备好”是一件很麻烦的事 - V2EX 请教各位 centos 7.9 通过 devtoolset 启用 c++14/17 时遇到的链接问题 - V2EX 求大佬指点:Windows 上 c++部署最新 Paddleocr,无法通过内存识字 - V2EX 为 c++ 提供模式匹配 - V2EX 分享一下我个人开源的 C++23 协程网络框架 - V2EX 为什么写 C++的人年龄偏大? - V2EX 大型 c++项目,在 ai 帮助下完成 Linux 平台移植,可行性多大? - V2EX 少用 auto - V2EX 再一次感觉到 C++的恶心 - V2EX 分布式存储 - V2EX [求助] Linux 有什么好的引入 c++ 第三方库的方案 - V2EX [求助]请教一个 C++多线程的性能问题 - V2EX 2026 年找 C++的开发工作,应该学习 C++的哪个版本? - V2EX 分布式系统 - V2EX 使用匿名结构体指针作为常量来杜绝魔数,是否合理/值得? - V2EX 有没有什么工具可以统计 C++项目里标识符的使用情况? - V2EX 看到一些 C++ 或者 C#项目 驼峰和下划线一块用,为啥泥? - V2EX [求助] Linux 系统下动态库卸载后全局变量未重置的问题 - V2EX 交叉编译 asop android adb 最新版的问题 - V2EX 小白问个 vcpkg 相关的问题 - V2EX 记录一次踩坑过程(clion + cmake + vcpkg) - V2EX 用智能指针管理 ffmpeg 中的数据结构是有必要的吗? - V2EX 定位重载的插件或者 IDE - V2EX 想系统的学习 Modern C++,麻烦大佬们推荐一些书籍 - V2EX 困扰几天的问题,这是被 gcc 优化了吗? - V2EX 好的 c++代码是什么样的 - V2EX 为什么 C/C++ 语言的标准库不做成 Java 那样可安装的运行时? - V2EX CLion 提供非商业免费使用了 - V2EX MFC c++对话框项目使用 httplib 创建静态资源服务, Debug 时每次访问都会被断下是什么原因 - V2EX c++调试遇到奇怪问题 - V2EX 求推荐 C++ 高性能的 web 框架, curd 用 - V2EX 一个项目下有多个 cpp 程序的源文件以及多个程序共用的源文件与头文件, vscode 的 clangd 插件与 compile_commands.json 该如何配置 - V2EX C++库脚手架项目及思考 - V2EX 有人遇到过 vs2022 c++代码高亮不生效的问题吗? - V2EX 使用 C++20 协程与 ASIO 库写了个 Socks5 Server 的跨平台 Demo 程序,几乎全功能,单文件源码少于一千行 - V2EX 小众论坛问个好奇的问题关于 c++ - V2EX 市面上大部分 c++程序员都在 c++98 水平吧? - V2EX cpp 一个隐式转换奇怪的现象 - V2EX C++入门书籍,我是不是被坑了? - V2EX mac clion 调试怎么才能看到 stl 容器的值 - V2EX 有没有合适开源的 C++项目可以快速实现一些功能 - V2EX 用 c++实现 bluez 对于 31 个字节蓝牙广播限制的问题 - V2EX 寻找一个在线运行 c++的方案 - V2EX 这种情况如何消除几百个 if/else - V2EX cmake + googletest 的目录结构最佳实践是? - V2EX 为什么打印模板元编程计算阶乘结果,比打印 for 循环计算阶乘结果更耗时 - V2EX Asio 异步模型 - V2EX 求教, mac 下 cmake c++工程编译后 elf 里面还有很多路径字符串 - V2EX C 怎么进阶学 C++呀
[有偿] 小白, Windows UI Automation TextPattern 检测问题求助 - V2EX
ck125s · 2025-09-04 · via C++

让 AI 写的,好像一次过

```
#include <iostream>
#include <windows.h>
#include <comdef.h>
#include <UIAutomation.h>
#include <atlbase.h>
#include <thread>
#include <chrono>

#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")

HWND consoleWindow = nullptr;
std::wstring lastSelectedText;

HRESULT GetTextFromActiveWindow(IUIAutomation* automation, std::wstring& selectedText) {
HWND foregroundWindow = GetForegroundWindow();
if (!foregroundWindow || foregroundWindow == consoleWindow) {
return E_FAIL;
}

CComPtr<IUIAutomationElement> windowElement;
HRESULT hr = automation->ElementFromHandle(foregroundWindow, &windowElement);
if (FAILED(hr) || !windowElement) {
return hr;
}

CComPtr<IUIAutomationTextPattern> textPattern;
hr = windowElement->GetCurrentPatternAs(UIA_TextPatternId, IID_PPV_ARGS(&textPattern));
if (FAILED(hr) || !textPattern) {
CComPtr<IUIAutomationElement> focusedElement;
hr = automation->GetFocusedElement(&focusedElement);
if (FAILED(hr) || !focusedElement) {
return hr;
}
hr = focusedElement->GetCurrentPatternAs(UIA_TextPatternId, IID_PPV_ARGS(&textPattern));
if (FAILED(hr) || !textPattern) {
return hr;
}
}

CComPtr<IUIAutomationTextRangeArray> textRangeArray;
hr = textPattern->GetSelection(&textRangeArray);
if (FAILED(hr) || !textRangeArray) {
return hr;
}

int length = 0;
hr = textRangeArray->get_Length(&length);
if (FAILED(hr) || length == 0) {
return hr;
}

selectedText.clear();
for (int i = 0; i < length; i++) {
CComPtr<IUIAutomationTextRange> textRange;
hr = textRangeArray->GetElement(i, &textRange);
if (SUCCEEDED(hr) && textRange) {
BSTR text = nullptr;
hr = textRange->GetText(-1, &text);
if (SUCCEEDED(hr) && text) {
selectedText += text;
SysFreeString(text);
}
}
}
return S_OK;
}

void MonitorSelectedText(IUIAutomation* automation) {
while (true) {
std::wstring currentSelectedText;
HRESULT hr = GetTextFromActiveWindow(automation, currentSelectedText);

if (SUCCEEDED(hr) && !currentSelectedText.empty() && currentSelectedText != lastSelectedText) {
lastSelectedText = currentSelectedText;

HWND foregroundWindow = GetForegroundWindow();
wchar_t windowTitle[256];
GetWindowTextW(foregroundWindow, windowTitle, 256);

std::wcout << L"\n=== 检测到新的选中文本 ===" << std::endl;
std::wcout << L"窗口: " << windowTitle << std::endl;
std::wcout << L"选中内容: " << currentSelectedText << std::endl;
std::wcout << L"========================\n" << std::endl;
}

std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
}

int main() {
CoInitializeEx(NULL, COINIT_MULTITHREADED);

consoleWindow = GetConsoleWindow();

CComPtr<IUIAutomation> automation;
HRESULT hr = CoCreateInstance(CLSID_CUIAutomation, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&automation));

if (FAILED(hr)) {
std::wcout << L"无法创建 UI Automation 对象" << std::endl;
CoUninitialize();
return -1;
}

std::wcout << L"文本选择监控程序已启动..." << std::endl;
std::wcout << L"程序将在后台监控其他窗口中的文本选择" << std::endl;
std::wcout << L"按 Ctrl+C 退出程序\n" << std::endl;

std::thread monitorThread(MonitorSelectedText, automation.p);
monitorThread.detach();

std::wcin.get();

CoUninitialize();
return 0;
}

```