main.js 2.2 KB

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