main.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const { app, BrowserWindow, Menu, ipcMain, globalShortcut } = require('electron');// 引入electron
  2. const path = require('path');
  3. let win, loadingWin;
  4. Menu.setApplicationMenu(null) // 去掉菜单栏
  5. app.commandLine.appendSwitch('wm-window-animations-disabled') // 拖动闪屏
  6. // 创建loading 窗口
  7. const showLoading = () => {
  8. loadingWin = new BrowserWindow({
  9. frame: false, // 无边框(窗口、工具栏等),只包含网页内容
  10. width: 200,
  11. height: 200,
  12. resizable: false,
  13. center: true,
  14. alwaysOnTop: true,
  15. transparent: true // 窗口是否支持透明,如果想做高级效果最好为true
  16. })
  17. loadingWin.loadFile('loading.html')
  18. loadingWin.on('close', () => {
  19. loadingWin = null
  20. })
  21. }
  22. // 创建主程序 窗口
  23. const createWindow = () => {
  24. win = new BrowserWindow({
  25. minWidth: 1300,
  26. minHeight: 760,
  27. width: 1300,
  28. height: 760,
  29. webPreferences: {
  30. contextIsolation: true,
  31. nodeIntegration: true,
  32. webSecurity: false, // 去掉跨越
  33. preload: path.join(__dirname, './preload.js')
  34. },
  35. show: false
  36. })// 创建一个窗口
  37. // 不同环境加载不同文件
  38. if (app.isPackaged) {
  39. win.loadFile('dist/index.html')
  40. } else {
  41. win.loadURL('http://localhost:6547/')
  42. }
  43. // 事件监听
  44. win.on('close', () => {
  45. // 回收BrowserWindow对象
  46. win = null
  47. })
  48. }
  49. // 事件监听
  50. app.on('ready', async () => {
  51. showLoading()
  52. createWindow()
  53. // 监听渲染进行
  54. ipcMain.once('close-loading', () => {
  55. loadingWin?.close()
  56. win?.show()
  57. })
  58. // 在BrowserWindow创建完成后,注册全局快捷键
  59. globalShortcut.register('Control+F12', () => {
  60. win?.webContents.toggleDevTools()
  61. })
  62. })
  63. app.on('window-all-closed', () => {
  64. app.quit()
  65. })
  66. app.on('activate', () => {
  67. if (win == null) {
  68. createWindow()
  69. }
  70. })