electron.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const { app, BrowserWindow, BrowserView, Tray, Menu, nativeImage } = require('electron')
  2. const { resolve } = require('path')
  3. let tray
  4. function createWindow () {
  5. const mainWindow = new BrowserWindow({
  6. width: 1000,
  7. height: 600, // 设置打开的窗口大小
  8. useContentSize: true,
  9. enableLargerThanScreen: true,
  10. autoHideMenuBar: true,
  11. icon: 'logo.png',
  12. webPreferences: {
  13. // contextIsolation: false,
  14. // worldSafeExecuteJavaScript:a flse,
  15. // webSecurity: false,
  16. nodeIntegration: true // 是否集成node.js,解决require is not defined问题
  17. // nodeIntegrationInWorker: true,
  18. // webviewTag: true, // 解决webview无法显示问题
  19. // enableRemoteModule: true,
  20. }
  21. })
  22. // mainWindow.setIcon('https://agent-crm.luojigou.vip/crm-api//20210323/logo_icon.png')
  23. // console.log(resolve(process.cwd(), 'dist/index.html'))
  24. // mainWindow.loadFile(resolve(process.cwd(), 'dist/index.html'))
  25. const view = new BrowserView() // new出对象
  26. mainWindow.setBrowserView(view) // 在主窗口中设置view可用
  27. view.setBounds({ x: 0, y: 0, width: 1000, height: 600 }) // 定义view的具体样式和位置
  28. view.setAutoResize({ width: true, height: true })
  29. view.useContentSize = true
  30. view.webContents.loadURL('http://cloudlink.jiaolongcloud.com') // wiew载入的页面
  31. }
  32. app.whenReady().then(() => {
  33. const icon = nativeImage.createFromPath('./logo.png')
  34. tray = new Tray(icon)
  35. const contextMenu = Menu.buildFromTemplate([])
  36. tray.setContextMenu(contextMenu)
  37. tray.setToolTip('This is my application')
  38. tray.setTitle('This is my title')
  39. createWindow()
  40. })
  41. app.on('window-all-closed', () => {
  42. if (process.platform !== 'darwin') {
  43. app.quit()
  44. }
  45. })
  46. app.on('activate', () => {
  47. if (BrowserWindow.getAllWindows().length === 0) {
  48. createWindow()
  49. }
  50. })