让 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;
}
```