const {app, BrowserWindow, ipcMain} = require('electron') const path = require('path') const fs = require('fs') function writeFile(event, data){ console.log(data) fs.writeFileSync('E:/hello.txt', data) } function readFile(){ const res = fs.readFileSync('E:/hello.txt').toString() console.log('###', res) return res } //创建窗口函数 function createWindow(){ //创建窗口 const win = new BrowserWindow({ //指定窗口大小 width: 1000, height: 800, //指定窗口位置 // x: 0, y: 0, // alwaysOnTop: true, //加载窗口选项 webPreferences:{ //预加载进程,必须传入绝对路径,所以使用path.resolve preload:path.resolve(__dirname, './preload.js') }, //隐藏菜单栏 autoHideMenuBar: true }) ipcMain.on('file-save', writeFile) ipcMain.handle('file-read', readFile) //加载主页面 win.loadFile('./pages/index.html') } //启动调用 app.on('ready', ()=>{ createWindow() //针对mac用户的无窗口时继续运行,点击图标创建窗口有 app.on('activate', ()=>{ if (BrowserWindow.getAllWindows().length === 0) createWindow() }) console.log('启动完毕') }) //对于非mac用户,无窗口时立即退出程序。 app.on('window-all-closed', ()=>{ if (process.platform !== 'darwin') app.quit() })