const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
app.quit()
} else {
app.on('second-instance', (event, commandLine, workingDirectory) => {
if (process.platform === "win32") {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore()
}
if (mainWindow.isVisible) {
mainWindow.focus()
} else {
mainWindow.show()
}
}
}
})
1. 定义模板
let tmp = [{
label: "关于",
click: () {
shell.openExternal('https://www.baidu.com')
}
}, {
label: "打开",
click(){
BrowserWindow.getFocusedWindow.webContents.send('openUrl')
}
}]
2. 生成模板
let menu = Menu.buildFromTemplate(tmp)
3. 挂载
Menu.setApplicationMenu(menu)
let option = {
title: '拉钩教育',
body: '互联网的实战大学, 大前端',
icon: './img.png'
}
let myNotification = new window.Notification(option.title, option)
myNotification.onclick = function () {
console.log("")
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/54967cf70b8f42f0b11d6be0e2ff187d.png)
app.on('ready', () => {
let ret = globalShortcut.register('ctrl + q', () => {
console.log('快捷键注册成功')
})
if (!ret) {
console.log('注册失败')
}
})
app.on("will-quit", () => {
globalShortcut.unregister('ctrl + q')
globalShortcut.unregisterAll()
})
const { clipboard } = require('electron')
let ret = clipboard.writeText("1231231")
clipboard.readText(ret)
let oImage = nativeImage.createFromPath('./msg.png')
clipboard.writeImage(oImage)
let oImg = clipboard.readImage()
let oImgDom = new Image()
oImgDom.src = oImg.toDataURL()
document.body.appendChild(oImgDom)
"script": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon --watch main.js --exec npm run serve",
"serve": "electron ."
}
function createWindow () {
let mainWin = new BrowserWindow({
x: 100,
y: 100,
show: false,
width: 800,
height: 400,
maxWidth: 1200,
maxHeight: 600,
minWidth: 400,
minHeight: 200,
resizable:false,
frame: true,
autoHideMenuBar: true,
icon: 'logo.png',
title: "标题",
webPreferences: {
webSecurity:false,
nodeIntegration: true,
enableRemoteModule: true
}
})
mainWin.loadFile('index.html')
mainWin.on('ready-to-show', () => {
mainWin.show()
})
mainWin.on('close', () => {
mainWin = null
})
}
const { remote } = require('electron')
let newWin = new remote.BrowserWindow({
width: 200,
height: 200
})
newWin.loadFile('list.html')
newWin.on('close', () => {
newWin = null
})