main.js 1.6 KB

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