const { app, BrowserWindow, Menu, ipcMain, globalShortcut, dialog, screen, Tray } = require('electron'); const { join } = require('path'); const { fork } = require('child_process'); const { platform } = require('process'); class MainSerivce { constructor() { Menu.setApplicationMenu(null) // 去掉菜单栏 app.commandLine.appendSwitch('wm-window-animations-disabled') // 拖动闪屏 this.loadingWin = null this.mainWin = null this.icon = join(__dirname, './icon/playGame.png') app.on('ready', this.onRead.bind(this)) app.on('activate', this.createWindow.bind(this)) app.on('window-all-closed', app.quit) } createLoading() { this.loadingWin = new BrowserWindow({ frame: false, // 无边框(窗口、工具栏等),只包含网页内容 width: 200, height: 200, resizable: false, center: true, alwaysOnTop: true, transparent: true // 窗口是否支持透明,如果想做高级效果最好为true }) this.loadingWin.loadFile('loading.html') this.loadingWin.on('close', () => { this.loadingWin = null }) } createWindow() { if (!this.mainWin) { this.mainWin = new BrowserWindow({ minWidth: 1300, minHeight: 760, width: 1300, height: 760, frame: false, transparent: true, icon: this.icon, webPreferences: { contextIsolation: true, nodeIntegration: true, webSecurity: false, // 去掉跨越 nodeIntegrationInWorker: true, preload: join(__dirname, './preload.js') }, show: false })// 创建一个窗口 // 不同环境加载不同文件 if (app.isPackaged) { this.mainWin.loadFile('dist/index.html') } else { this.mainWin.loadURL('http://localhost:6547/') } // 事件监听 this.mainWin.on('close', () => { this.mainWin = null }) } } onRead() { this.createLoading() this.createWindow() // 图标 const tray = new Tray(this.icon) const contextMenu = Menu.buildFromTemplate([ { label: '退出', click: () => { this.mainWin.close() app.quit() } } ]) tray.setContextMenu(contextMenu) tray.setToolTip('控制端') tray.on('click', () => { this.mainWin.show() }) // 注册调试模式 globalShortcut.register('Control+F12', () => { this.mainWin.webContents.toggleDevTools() }) // 禁用右键 if(platform === 'win32') this.mainWin.hookWindowMessage(278, () => { this.mainWin.setEnabled(false);//窗口禁用 setTimeout(() => { this.mainWin.setEnabled(true); }, 100) //延时太快会立刻启动,太慢会妨碍窗口其他操作,可自行测试最佳时间 return true }) // 通信 ipcMain.on('signal', (_, evt, data) => { if (evt === 'close-loading') { if (this.loadingWin) this.loadingWin.close() this.mainWin.show() if(this.mainWin.isVisible()) this.connectLogi() } else if (evt === 'minWin') { this.mainWin.minimize() } else if (evt === 'closeWin') { this.mainWin.close() } else if (evt === 'maxWin') { const { width, height } = screen.getPrimaryDisplay().size if (data) { this.mainWin.setBounds({ x: 0, y: 0, width, height }) } else { this.mainWin.setBounds({ width: 1300, height: 760 }) this.mainWin.center() } } }) } connectLogi() { const signal = fork(join(__dirname, './logiControl.js')) signal.on('message',msg=>{ if(msg.type === 'err'){ dialog.showMessageBox(this.mainWin, { message: '连接方向盘失败', type: 'error', title: '连接错误', detail: '请检查方向盘是否连接' }).then(({ response }) => { if (!response) { this.mainWin.close() app.quit() } }) }else{ console.log(msg.data); if (this.mainWin && !this.mainWin.isDestroyed()) this.mainWin?.webContents.send('contrlData', msg.data) } }) } }; new MainSerivce()