main.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const {
  2. app, BrowserWindow, Menu, ipcMain
  3. } = require('electron')// 引入electron
  4. let win; let
  5. loadingWin
  6. Menu.setApplicationMenu(null) // 去掉菜单栏
  7. app.commandLine.appendSwitch('wm-window-animations-disabled') // 拖动闪屏
  8. // 创建loading 窗口
  9. const showLoading = () => {
  10. loadingWin = new BrowserWindow({
  11. frame: false, // 无边框(窗口、工具栏等),只包含网页内容
  12. width: 200,
  13. height: 200,
  14. resizable: false,
  15. center: true,
  16. alwaysOnTop: true,
  17. transparent: true // 窗口是否支持透明,如果想做高级效果最好为true
  18. })
  19. loadingWin.loadFile('loading/index.html')
  20. loadingWin.on('close', () => {
  21. loadingWin = null
  22. })
  23. }
  24. // 创建主程序 窗口
  25. const createWindow = () => {
  26. win = new BrowserWindow({
  27. maxWidth: 1920,
  28. maxHeight: 1080,
  29. minWidth: 1620,
  30. minHeight: 900,
  31. webPreferences: {
  32. contextIsolation: false,
  33. nodeIntegration: true,
  34. webSecurity: false // 去掉跨越
  35. },
  36. show: false
  37. })// 创建一个窗口
  38. // 在窗口内要展示的内容index.html 就是打包生成的index.html
  39. win.loadFile('dist/index.html')
  40. // 开启调试工具
  41. // win.webContents.openDevTools();
  42. // 事件监听
  43. win.on('close', () => {
  44. // 回收BrowserWindow对象
  45. win = null
  46. })
  47. }
  48. // 事件监听
  49. app.on('ready', async () => {
  50. showLoading()
  51. createWindow()
  52. // 监听渲染进行
  53. ipcMain.once('close-loading', () => {
  54. loadingWin.close()
  55. win.show()
  56. })
  57. })
  58. app.on('window-all-closed', () => {
  59. app.quit()
  60. })
  61. app.on('activate', () => {
  62. if (win == null) {
  63. createWindow()
  64. }
  65. })