使用 ffi-napi 调用底层 dll , 实现模拟 vscode 无边框启动。
参考文献: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowlonga https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowpos https://stackoverflow.com/questions/71502147/electron-has-blue-outline-frame-when-launch/71506959#71506959
const { BrowserWindow, app } = require("electron");
const path = require("path");
const { Library } = require("ffi-napi");
app.disableHardwareAcceleration();
function createWindow() {
return new BrowserWindow({
minHeight: 400,
minWidth: 600,
center: true,
frame: false,
show: true,
backgroundColor: "#fff",
webPreferences: {
spellcheck: false,
webSecurity: true,
nodeIntegration: true,
contextIsolation: false,
},
});
}
async function openWindow() {
const win = createWindow();
var ffi = Library("user32.dll", {
SetWindowLongA: ["int", ["int", "int", "long"]],
SetWindowPos: ["int", ["int", "int", "int", "int", "int", "int", "int"]],
});
const WS_THICKFRAME = 0x00040000;
const SWP_SHOWWINDOW = 0x0040;
ffi.SetWindowLongA(win.getNativeWindowHandle().readUint32LE(), -16, WS_THICKFRAME);
ffi.SetWindowPos(win.getNativeWindowHandle().readUint32LE(), 0, 0, 0, 0, 0, SWP_SHOWWINDOW);
win.center();
if (app.isPackaged) {
await win.loadFile("public/index.html");
} else {
await win.loadURL("http://localhost:3000");
}
return win;
}
直接调用即可:
(async ()=>{
await app.whenReady();
window = await openWindow();
})()
|