在Windows平台,可以使用以下代码实现获取鼠标选中的数据:

#include <Windows.h>
#include <string>

std::string GetSelectedText()
{
    HWND hWnd = GetForegroundWindow();
    if (hWnd == NULL)
    {
        return "";
    }

    DWORD processId;
    GetWindowThreadProcessId(hWnd, &processId);
    if (processId == GetCurrentProcessId())
    {
        return "";
    }

    if (!AttachThreadInput(GetCurrentThreadId(), processId, TRUE))
    {
        return "";
    }

    HWND focusWnd = GetFocus();
    if (focusWnd == NULL)
    {
        return "";
    }

    const int MAX_TEXT_LEN = 1024;
    wchar_t text[MAX_TEXT_LEN] = { 0 };
    SendMessageW(focusWnd, WM_GETTEXT, MAX_TEXT_LEN, (LPARAM)text);

    std::wstring wstr(text);
    std::string str;
    int len = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
    if (len > 0)
    {
        char* buffer = new char[len];
        WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, buffer, len, NULL, NULL);
        str = buffer;
        delete[] buffer;
    }

    AttachThreadInput(GetCurrentThreadId(), processId, FALSE);

    return str;
}

该方法返回一个字符串,该字符串为选中的文本内容,编码为UTF-8。可以在Node.js中使用node-ffi模块来调用该方法并包装成Node.js模块

nodejs c++ 获取鼠标选中的数据注意不是复制的内容而是选中的内容 一下是官方的描述请根据这个思路用C++实现并包装成一个方法返回值是char注意用utf8编码This code help you to get focused control text in focused window i hope that helps Gain focus control You could get t

原文地址: http://www.cveoy.top/t/topic/gfV3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录