以下是一个基本的示例代码,使用Node.js和C++调用WinAPI来监视Ctrl键的按下,并获取鼠标选中的文件以及判断文件类型并打印出来。

C++代码:

#include <windows.h>
#include <shlwapi.h>
#include <iostream>

#pragma comment(lib, "shlwapi.lib")

HHOOK hook;
HWND hWnd;

LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam) {
    if (nCode == HC_ACTION) {
        if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) && (wParam == WM_LBUTTONUP)) {
            POINT cursorPos;
            GetCursorPos(&cursorPos);

            char fileName[MAX_PATH] = { 0 };
            SHFILEINFO fileInfo = { 0 };

            SHGetFileInfo((LPCTSTR)fileName, FILE_ATTRIBUTE_NORMAL, &fileInfo, sizeof(fileInfo), SHGFI_TYPENAME | SHGFI_USEFILEATTRIBUTES | SHGFI_DISPLAYNAME);

            std::cout << "Selected file: " << fileInfo.szDisplayName << std::endl;
            std::cout << "File type: " << fileInfo.szTypeName << std::endl;
        }
    }
    return CallNextHookEx(hook, nCode, wParam, lParam);
}

extern "C" __declspec(dllexport) void SetHook(HWND hwnd) {
    hWnd = hwnd;
    hook = SetWindowsHookEx(WH_MOUSE_LL, HookProc, NULL, 0);
}

extern "C" __declspec(dllexport) void Unhook() {
    UnhookWindowsHookEx(hook);
}

Node.js代码:

const ffi = require('ffi-napi');
const ref = require('ref-napi');
const int = ref.types.int;
const HWND = ref.refType(int);
const voidPtr = ref.refType(ref.types.void);

const user32 = ffi.Library('user32', {
    'SetWindowsHookExA': ['pointer', ['int', 'pointer', 'pointer', 'int']],
    'UnhookWindowsHookEx': ['bool', ['pointer']],
    'CallNextHookEx': ['pointer', ['pointer', 'int', 'pointer', 'pointer']],
    'GetCurrentThreadId': ['int', []],
    'GetLastError': ['uint32', []],
    'GetModuleHandleW': ['pointer', ['pointer']],
    'PostMessageA': ['bool', ['pointer', 'uint32', 'uint32', 'uint32']],
});

const kernel32 = ffi.Library('kernel32', {
    'GetLastError': ['uint32', []],
});

const setHook = (hwnd) => {
    const hookProc = ffi.Callback('pointer', ['int', 'uint32', 'pointer'], function (nCode, wParam, lParam) {
        if (nCode === 0) {
            const ctrlPressed = user32.GetAsyncKeyState(0x11) & 0x8000;
            const leftButtonUp = wParam === 0x202;
            if (ctrlPressed && leftButtonUp) {
                const point = ref.alloc('int', lParam.dword >> 0x10);
                const fileName = Buffer.alloc(0x200);
                user32.PostMessageA(hwnd, 0x112, 0xf100, point.deref());
                user32.SHGetFileInfoW(fileName, 0x80, fileInfo.ref(), fileInfo.length, 0x100 | 0x400 | 0x200);
                console.log('Selected file:', ref.readCString(fileInfo.display_name.buffer));
                console.log('File type:', ref.readCString(fileInfo.type_name.buffer));
            }
        }
        return user32.CallNextHookEx(hook, nCode, wParam, lParam);
    });

    const threadId = user32.GetCurrentThreadId();
    const hModule = user32.GetModuleHandleW(null);
    const hook = user32.SetWindowsHookExA(0x0D, hookProc, hModule, threadId);
    if (hook.isNull()) {
        throw new Error('Failed to set hook: ' + kernel32.GetLastError());
    }
};

const unhook = () => {
    const success = user32.UnhookWindowsHookEx(hook);
    if (!success) {
        throw new Error('Failed to unhook: ' + kernel32.GetLastError());
    }
};

const fileInfo = ref.types.Struct({
    icon: 'pointer',
    icon_index: 'int',
    attributes: 'uint32',
    display_name: ref.types.CString,
    type_name: ref.types.CString,
});

setHook(user32.GetActiveWindow());
``
nodejs c++使用win api 监听当按下ctrl时获取鼠标选中的文件以及判断文件的类型并打印出来

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

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