nodejs 在windows10中设置动态(视频)壁纸

main.js

const fs = require("fs");
const path = require("path");
const ffi = require("@saleae/ffi");
const child_process = require("child_process");

// Import user32
const W32 = new ffi.Library("user32", {
  // 检索顶级窗口的句柄,该顶级窗口的类名和窗口名与指定的字符串匹配。此功能不搜索子窗口。此功能不执行区分大小写的搜索。
  FindWindowW: ["int32", ["string", "string"]],

  // 将指定的消息发送到一个或多个窗口
  SendMessageTimeoutW: [
    "int32",
    ["int32", "int32", "int32", "int32", "int32", "int32", "int32"],
  ],

  // 通过将句柄传递给每个窗口,依次传递到应用程序定义的回调函数,可以枚举屏幕上所有的顶级窗口
  EnumWindows: ["bool", ["pointer", "int32"]],

  // 检索其类名和窗口名与指定字符串匹配的窗口的句柄。该功能搜索子窗口,从指定子窗口之后的子窗口开始。此功能不执行区分大小写的搜索。
  FindWindowExW: ["int32", ["int32", "int32", "string", "int32"]],

  // 更改指定子窗口的父窗口。
  // HWND SetParent(HWND hWndChild, HWND hWndNewParent);
  SetParent: ["int32", ["int32", "int32"]],
});

const argv = process.argv.slice(2);

if (!argv || !argv.length) {
  process.exit(1);
}

const filePath = path.normalize(argv[0]);

if (!fs.existsSync(filePath)) {
  process.exit(1);
}
const play = child_process.fork("./play.js");
const runFFplayCommand = `ffplay "${filePath}" -loop 0`;
play.send(runFFplayCommand);
play.on("message", () => {
  setTimeout(() => {
    // ffplay句柄
    const ffplayw = W32.FindWindowW(TEXT("SDL_app"), null);
    if (ffplayw == 0) {
      console.error("获取fpplay句柄失败");
      process.exit(1);
    }
    setDynamicWallpaper(ffplayw);
  }, 2000);
});

function setDynamicWallpaper(ffplayw) {

  // 要触发在桌面图标和墙纸之间创建WorkerW窗口,我们必须向程序管理器发送一条消息。 
  // 该消息是未记录的消息,因此没有专用的Windows API名称,除了0x052C
  W32.SendMessageTimeoutW(
    W32.FindWindowW(TEXT("Progman"), null),
    0x052c,
    0,
    0,
    0x0000,
    1000,
    0
  );

  // 我们枚举所有Windows
  W32.EnumWindows(
    ffi.Callback("bool", ["int32", "int32"], (tophandle, topparamhandle) => {
      // 找到一个具有SHELLDLL_DefView的Windows
      const SHELLDLL_DefView = W32.FindWindowExW(
        tophandle,
        0,
        TEXT("SHELLDLL_DefView"),
        0
      );
      if (SHELLDLL_DefView !== 0) {
        // 将其下一个同级分配给workerw。
        const workerw = W32.FindWindowExW(0, tophandle, TEXT("WorkerW"), 0);
        W32.SetParent(ffplayw, workerw);
      }
      return true;
    }),
    0
  );
}
function TEXT(text) {
  return Buffer.from(`${text}\0`, "ucs2");
}

play.js

const child_process = require("child_process");

process.on("message", (runFFplayCommand) => {
  process.send(true);
  child_process.execSync(runFFplayCommand);
});

运行

>node main.js "D:\dynamic wallpaper\Nightcore-We-Wish-You-A-Merry-Christmas-Live-Wallpaper.mp4"

参考连接

猜你喜欢

转载自www.cnblogs.com/ajanuw/p/12790326.html