【问题标题】:changing desktop background in windows 10 via nodejs通过nodejs更改Windows 10中的桌面背景
【发布时间】:2022-11-22 05:39:33
【问题描述】:

我有一个 2 天后更换墙纸的小项目,我使用的是 nodejs 14 和 ff-nappi。

我有这段代码:

const ffi = require("ffi-napi");
const ref = require("ref-napi");
const wchar_t = require("ref-wchar-napi");

var wchar_string = wchar_t.string;

const lib = ffi.Library("user32", {
  SystemParametersInfoW: ["int", ["uint", "uint", wchar_string, "uint"]],
});

const SPI_SETDESKWALLPAPER = 20;
const SPIF_UPDATEINIFILE = 0x01;
const SPIF_SENDWININICHANGE = 0x02;

export function setWallpaper(pathString: string) {
  const path = ref.alloc(wchar_string, pathString);
  lib.SystemParametersInfoW(
    SPI_SETDESKWALLPAPER,
    0,
    path,
    SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE
  );
}

运行此程序后,我的墙纸变黑了。

【问题讨论】:

  • 壁纸的有效完整路径? bmp 还是 jpg?
  • 是的,我验证了墙纸的路径,我尝试了 jpg 和 png
  • 传入 0 或空字符串作为路径可能会删除墙纸。

标签: node.js windows


【解决方案1】:

ffi-napi 没有将路径字符串转换为 wchar_string,因此使用 ref-napi 进行的转换是多余的。

对我有用的整个 sn-p(我也跳过了传递标志):

const ffi = require("ffi-napi");
const wchar_t = require("ref-wchar-napi");

var wchar_string = wchar_t.string;

const lib = ffi.Library("user32", {
  SystemParametersInfoW: ["int", ["uint", "uint", wchar_string, "uint"]],
});

const SPI_SETDESKWALLPAPER = 20;

export function setWallpaper(pathString: string) {
  lib.SystemParametersInfoW(
    SPI_SETDESKWALLPAPER,
    0,
    pathString,
    0
  );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 2013-08-11
    • 1970-01-01
    相关资源
    最近更新 更多