Node.js C++ 获取鼠标选中数据 (非复制内容)
在 C++ 中实现获取鼠标选中的数据,需要使用以下步骤:
- 获取当前活动窗口的句柄,可以通过调用
GetForegroundWindowAPI 实现。 - 通过调用
GetWindowThreadProcessIdAPI 获取当前窗口的线程 ID。 - 如果当前窗口的线程 ID 不是当前线程,可以调用
AttachThreadInputAPI 将其附加到当前线程。 - 调用
GetFocusAPI 获取焦点控件的句柄。 - 通过调用
SendMessageWAPI 发送WM_GETTEXT消息获取焦点控件的文本。
下面是一个示例代码:
#include <Windows.h>
char* GetSelectedText() {
HWND hWnd = GetForegroundWindow();
DWORD threadId = GetWindowThreadProcessId(hWnd, NULL);
DWORD currentThreadId = GetCurrentThreadId();
if (threadId != currentThreadId) {
AttachThreadInput(threadId, currentThreadId, TRUE);
}
HWND hFocusWnd = GetFocus();
if (threadId != currentThreadId) {
AttachThreadInput(threadId, currentThreadId, FALSE);
}
const int bufferSize = 1024;
wchar_t buffer[bufferSize];
SendMessageW(hFocusWnd, WM_GETTEXT, bufferSize, (LPARAM)buffer);
char* result = new char[bufferSize];
WideCharToMultiByte(CP_UTF8, 0, buffer, -1, result, bufferSize, NULL, NULL);
return result;
}
该函数获取当前活动窗口的句柄,然后获取当前窗口的线程 ID,如果当前窗口的线程 ID 不是当前线程,将其附加到当前线程。接下来,获取焦点控件的句柄,发送 WM_GETTEXT 消息获取焦点控件的文本,并将结果转换为 UTF-8 编码。最后,返回结果字符串。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/ousB 著作权归作者所有。请勿转载和采集!