electron 打开新窗口时不进入聚焦
您可以使用new BrowserWindow来创建新窗口,并使用focusable选项禁用窗口聚焦。
以下是示例代码:
const { app, BrowserWindow } = require('electron')
let mainWindow
function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
mainWindow.loadFile('index.html')
mainWindow.on('closed', function () {
mainWindow = null
})
}
app.on('ready', function () {
createWindow()
// 创建新窗口
let newWindow = new BrowserWindow({
width: 400,
height: 300,
focusable: false, // 禁用聚焦
webPreferences: {
nodeIntegration: true
}
})
newWindow.loadURL('https://www.google.com')
newWindow.on('closed', function () {
newWindow = null
})
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
if (mainWindow === null) {
createWindow()
}
})
在上面的代码中,我们创建了一个新窗口,并使用focusable选项禁用了窗口聚焦。注意,这只适用于新窗口,而不是主窗口
原文地址: https://www.cveoy.top/t/topic/f4z0 著作权归作者所有。请勿转载和采集!