electron.js 1.8 KB

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