production-taskbar-client / src / main / utils / winapi / setWindowLong.js
setWindowLong.js
Raw
/* eslint-disable no-bitwise */
import os from "os";
import ffi from "@inigolabs/ffi-napi";
import ref from "@inigolabs/ref-napi";

const is64bit = os.arch() === "x64";

const LWA_ALPHA = 0x2;
const GWL_EXSTYLE = -20;
const WS_EX_LAYERED = 0x00080000;

const user32 = ffi.Library("user32", {
  SetLayeredWindowAttributes: [
    "bool",
    ["long", "uint32", ref.types.byte, "uint32"],
  ],
  [is64bit ? "GetWindowLongPtrA" : "GetWindowLongA"]: ["long", ["long", "int"]],
  [is64bit ? "SetWindowLongPtrA" : "SetWindowLongA"]: [
    "long",
    ["long", "int", "long"],
  ],
});

export default function setWindowTransparency(hwnd, value) {
  if (is64bit) {
    const ex = user32.GetWindowLongPtrA(hwnd, GWL_EXSTYLE);
    user32.SetWindowLongPtrA(hwnd, GWL_EXSTYLE, ex | WS_EX_LAYERED);
  } else {
    const ex = user32.GetWindowLongA(hwnd, GWL_EXSTYLE);
    user32.SetWindowLongA(hwnd, GWL_EXSTYLE, ex | WS_EX_LAYERED);
  }

  return user32.SetLayeredWindowAttributes(hwnd, 0, value, LWA_ALPHA);
}