main.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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, rej) => {
  11. const file = execFile(`${path.resolve(__dirname, 'screen/PrintScr.exe')}`)
  12. file.on('exit', (e) => {
  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. rej(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. rej(null)
  28. }
  29. })
  30. })
  31. }
  32. // 创建loading 窗口
  33. const showLoading = () => {
  34. loadingWin = new BrowserWindow({
  35. frame: false, // 无边框(窗口、工具栏等),只包含网页内容
  36. width: 200,
  37. height: 200,
  38. resizable: false,
  39. center: true,
  40. alwaysOnTop: true,
  41. transparent: true // 窗口是否支持透明,如果想做高级效果最好为true
  42. })
  43. loadingWin.loadFile('loading/index.html')
  44. loadingWin.on('close', () => {
  45. loadingWin = null
  46. })
  47. }
  48. // 创建主程序 窗口
  49. const createWindow = () => {
  50. win = new BrowserWindow({
  51. maxWidth: 1920,
  52. maxHeight: 1080,
  53. minWidth: 1620,
  54. minHeight: 900,
  55. webPreferences: {
  56. contextIsolation: false,
  57. nodeIntegration: true,
  58. webSecurity: false // 去掉跨越
  59. },
  60. show: false
  61. })// 创建一个窗口
  62. // 在窗口内要展示的内容index.html 就是打包生成的index.html
  63. win.loadFile('dist/index.html')
  64. // 开启调试工具
  65. // win.webContents.openDevTools();
  66. // 事件监听
  67. win.on('close', () => {
  68. // 回收BrowserWindow对象
  69. win = null
  70. })
  71. }
  72. // 事件监听
  73. app.on('ready', async () => {
  74. showLoading()
  75. createWindow()
  76. // 监听渲染进行
  77. ipcMain.once('close-loading', () => {
  78. loadingWin.close()
  79. win.show()
  80. })
  81. // 监听截图
  82. ipcMain.once('screen', () => {
  83. screenShot().then(res => {
  84. if (!res) return
  85. // 发送到子进程
  86. win.webContents.send('screenData', res)
  87. })
  88. })
  89. })
  90. app.on('window-all-closed', () => {
  91. app.quit()
  92. })
  93. app.on('activate', () => {
  94. if (win == null) {
  95. createWindow()
  96. }
  97. })