main.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 screenShot () {
  10. return new Promise((res) => {
  11. const file = execFile(join(__dirname, './screen/PrintScr.exe'))
  12. file.on('exit', (e: Any) => {
  13. if (e === 1) {
  14. // 剪切板读buffer图片
  15. const clip = clipboard.readImage()
  16. if (clip.isEmpty()) {
  17. // eslint-disable-next-line prefer-promise-reject-errors
  18. res(null)
  19. } else {
  20. // 转成buffer base64
  21. const png = clipboard.readImage().toPNG()
  22. const img = 'data:image/png;base64,' + png.toString('base64')
  23. res(img)
  24. }
  25. } else {
  26. // eslint-disable-next-line prefer-promise-reject-errors
  27. res(null)
  28. }
  29. })
  30. })
  31. }
  32. // 消息通知
  33. function notifiction (title, options) {
  34. const WebNotification = window.Notification || window.mozNotification || window.webkitNotification
  35. const isTrue = WebNotification.requestPermission(result => {
  36. return (result === 'granted') // granted(允许) || denied(拒绝)
  37. })
  38. const noti = new WebNotification(title, options)
  39. noti.onclick=()=>{
  40. console.log('关闭')
  41. }
  42. }
  43. // 创建loading 窗口
  44. const showLoading = () => {
  45. loadingWin = new BrowserWindow({
  46. frame: false, // 无边框(窗口、工具栏等),只包含网页内容
  47. width: 200,
  48. height: 200,
  49. resizable: false,
  50. center: true,
  51. alwaysOnTop: true,
  52. transparent: true // 窗口是否支持透明,如果想做高级效果最好为true
  53. })
  54. loadingWin.loadFile('loading/index.html')
  55. loadingWin.on('close', () => {
  56. loadingWin = null
  57. })
  58. }
  59. // 创建主程序 窗口
  60. const createWindow = () => {
  61. win = new BrowserWindow({
  62. maxWidth: 1920,
  63. maxHeight: 1080,
  64. minWidth: 1620,
  65. minHeight: 900,
  66. webPreferences: {
  67. contextIsolation: false,
  68. nodeIntegration: true,
  69. webSecurity: false // 去掉跨越
  70. },
  71. show: false
  72. })// 创建一个窗口
  73. // 在窗口内要展示的内容index.html 就是打包生成的index.html
  74. win.loadFile('dist/index.html')
  75. // 开启调试工具
  76. // win.webContents.openDevTools();
  77. // 事件监听
  78. win.on('close', () => {
  79. // 回收BrowserWindow对象
  80. win = null
  81. })
  82. }
  83. // 事件监听
  84. app.on('ready', async () => {
  85. showLoading()
  86. createWindow()
  87. // 监听渲染进行
  88. ipcMain.once('close-loading', () => {
  89. loadingWin.close()
  90. win.show()
  91. })
  92. // 监听截图
  93. ipcMain.once('screen', () => {
  94. screenShot().then(res => {
  95. if (!res) return
  96. // 发送到子进程
  97. win.webContents.send('screenData', res)
  98. })
  99. })
  100. })
  101. app.on('window-all-closed', () => {
  102. app.quit()
  103. })
  104. app.on('activate', () => {
  105. if (win == null) {
  106. createWindow()
  107. }
  108. })