C++ 获取鼠标选中文本 - Node.js 可用
#include <Windows.h>
#include
char* GetFocusedControlText() { HWND focusedWnd = GetForegroundWindow(); DWORD threadId = GetWindowThreadProcessId(focusedWnd, NULL); DWORD currentThreadId = GetCurrentThreadId(); if (threadId != currentThreadId) { AttachThreadInput(threadId, currentThreadId, TRUE); } HWND focusedCtrl = GetFocus(); int textLength = SendMessageW(focusedCtrl, WM_GETTEXTLENGTH, 0, 0); wchar_t* buffer = new wchar_t[textLength + 1]; SendMessageW(focusedCtrl, WM_GETTEXT, textLength + 1, (LPARAM)buffer); std::wstring wstr(buffer); delete[] buffer; int utf8Length = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL); char* utf8Buffer = new char[utf8Length]; WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, utf8Buffer, utf8Length, NULL, NULL); return utf8Buffer; }
AI 优化版
原文地址:https://www.cveoy.top/t/topic/ourB 著作权归作者所有。请勿转载和采集!