const { app, BrowserWindow, Menu, ipcMain } = require('electron')// 引入electron let win; let loadingWin Menu.setApplicationMenu(null) // 去掉菜单栏 app.commandLine.appendSwitch('wm-window-animations-disabled') // 拖动闪屏 // 截图 function screenShot () { return new Promise((res) => { const file = execFile(join(__dirname, './screen/PrintScr.exe')) file.on('exit', (e: Any) => { if (e === 1) { // 剪切板读buffer图片 const clip = clipboard.readImage() if (clip.isEmpty()) { // eslint-disable-next-line prefer-promise-reject-errors res(null) } else { // 转成buffer base64 const png = clipboard.readImage().toPNG() const img = 'data:image/png;base64,' + png.toString('base64') res(img) } } else { // eslint-disable-next-line prefer-promise-reject-errors res(null) } }) }) } // 消息通知 function notifiction (title, options) { const WebNotification = window.Notification || window.mozNotification || window.webkitNotification const isTrue = WebNotification.requestPermission(result => { return (result === 'granted') // granted(允许) || denied(拒绝) }) const noti = new WebNotification(title, options) noti.onclick=()=>{ console.log('关闭') } } // 创建loading 窗口 const showLoading = () => { loadingWin = new BrowserWindow({ frame: false, // 无边框(窗口、工具栏等),只包含网页内容 width: 200, height: 200, resizable: false, center: true, alwaysOnTop: true, transparent: true // 窗口是否支持透明,如果想做高级效果最好为true }) loadingWin.loadFile('loading/index.html') loadingWin.on('close', () => { loadingWin = null }) } // 创建主程序 窗口 const createWindow = () => { win = new BrowserWindow({ maxWidth: 1920, maxHeight: 1080, minWidth: 1620, minHeight: 900, webPreferences: { contextIsolation: false, nodeIntegration: true, webSecurity: false // 去掉跨越 }, show: false })// 创建一个窗口 // 在窗口内要展示的内容index.html 就是打包生成的index.html win.loadFile('dist/index.html') // 开启调试工具 // win.webContents.openDevTools(); // 事件监听 win.on('close', () => { // 回收BrowserWindow对象 win = null }) } // 事件监听 app.on('ready', async () => { showLoading() createWindow() // 监听渲染进行 ipcMain.once('close-loading', () => { loadingWin.close() win.show() }) // 监听截图 ipcMain.once('screen', () => { screenShot().then(res => { if (!res) return // 发送到子进程 win.webContents.send('screenData', res) }) }) }) app.on('window-all-closed', () => { app.quit() }) app.on('activate', () => { if (win == null) { createWindow() } })