main.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // 消息通知
  9. function notifiction (title, options) {
  10. const WebNotification = window.Notification || window.mozNotification || window.webkitNotification
  11. const isTrue = WebNotification.requestPermission(result => {
  12. return (result === 'granted') // granted(允许) || denied(拒绝)
  13. })
  14. const noti = new WebNotification(title, options)
  15. noti.onclick=()=>{
  16. console.log('关闭')
  17. }
  18. }
  19. // 创建loading 窗口
  20. const showLoading = () => {
  21. loadingWin = new BrowserWindow({
  22. frame: false, // 无边框(窗口、工具栏等),只包含网页内容
  23. width: 200,
  24. height: 200,
  25. resizable: false,
  26. center: true,
  27. alwaysOnTop: true,
  28. transparent: true // 窗口是否支持透明,如果想做高级效果最好为true
  29. })
  30. loadingWin.loadFile('loading.html')
  31. loadingWin.on('close', () => {
  32. loadingWin = null
  33. })
  34. }
  35. // 创建主程序 窗口
  36. const createWindow = () => {
  37. win = new BrowserWindow({
  38. maxWidth: 1920,
  39. maxHeight: 1080,
  40. minWidth: 1620,
  41. minHeight: 900,
  42. webPreferences: {
  43. contextIsolation: false,
  44. nodeIntegration: true,
  45. webSecurity: false // 去掉跨越
  46. },
  47. show: false
  48. })// 创建一个窗口
  49. // 在窗口内要展示的内容index.html 就是打包生成的index.html
  50. win.loadFile('dist/index.html')
  51. // 开启调试工具
  52. // win.webContents.openDevTools();
  53. // 事件监听
  54. win.on('close', () => {
  55. // 回收BrowserWindow对象
  56. win = null
  57. })
  58. }
  59. // 事件监听
  60. app.on('ready', async () => {
  61. showLoading()
  62. createWindow()
  63. // 监听渲染进行
  64. ipcMain.once('close-loading', () => {
  65. loadingWin.close()
  66. win.show()
  67. })
  68. })
  69. app.on('window-all-closed', () => {
  70. app.quit()
  71. })
  72. app.on('activate', () => {
  73. if (win == null) {
  74. createWindow()
  75. }
  76. })