使用 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
// @ts-check
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,
// 开启node
nodeIntegration: true,
contextIsolation: false,
},
});
}
/**
* 打开窗口
*/
async function openWindow() {
const win = createWindow();
// ffi - napi;
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();
})()