waitingsong / node-win32-api

win32 api
MIT License
430 stars 55 forks source link

FindWindow handles and SetParent? #42

Open ranmacar opened 1 year ago

ranmacar commented 1 year ago

Hi!

I wanted to convert this example to js using the API, but it doesn't seem to work.

https://github.com/tauri-apps/tauri/issues/4261

Is there a way to access the Progman process or spawn the window as mentioned above?

Thanks!

ranmacar commented 1 year ago

This is how far I got. Looking for the SHELLDLL_DefView doesn't seem to work.

And there is still no SetParent?

https://github.com/waitingsong/node-win32-api/issues/26

import { User32 } from "win32-api/promise";
import { DTypes as W } from "win32-api";

import ref from "ref-napi";
import ffi from "ffi-napi";
import StructDi from "ref-struct-di";

const user32 = User32.load();

function getWindow(name, handle = 0) {
    return user32.FindWindowExW(0, 0, null, Buffer.from(name + "\0", "ucs2"));
}
async function getWorker() {
    const progman = await getWindow("Program Manager");
    console.log("progman: ", progman);

    await user32.SendMessageW(progman, 0x052c, 0, 0);
    await user32.SendMessageW(progman, 0x052c, 0, 1);

    let worker = null;
    let handles = [];

    const Struct = StructDi(ref);
    const EnumParams = Struct({
        uiMsg: W.UINT,
        wParam: W.WPARAM,
        lParam: W.LPARAM,
    });

    var EP = new EnumParams();

    const WndEnumProc = ffi.Callback(W.BOOL, [W.HWND, W.LPARAM], handle => {
        handles.push(handle);
        return 1;
    });

    await user32.EnumWindows(WndEnumProc, EP.ref().address());

    while (!worker && handles.length) {
        const handle = handles.shift();
        worker = await getWindow("SHELLDLL_DefView", handle);
    }

    return worker;
}

async function attach(name) {
    user32.SetParentW(await getWindow(name), await getWorker());
}

async function detach(name) {
    user32.SetParentW(await getWindow(name), 0);
}

console.log("worker:", await getWorker());
waitingsong commented 2 months ago

SetParent() add at v22.1.0