上一篇:electron+vue制做桌面应用--自定义标题栏中咱们介绍了如何使用electon制做自定义样式的标题栏
接下来,咱们介绍一下标题栏上的最大化、最小化和关闭按钮如何实现vue
首先查阅electron文档BrowserWindow部分,咱们发现electron已经准备好win.close()、win.maximize()和win.minimize()三个方法供咱们调用了。web
那么问题来了,个人渲染进程获取到了点击事件,如何到主进程中调用对应的方法呢?segmentfault
这里有两种方法,一种是使用ipc,另外一种是使用romate
这里先介绍下ipc
新建组件Titlebtn,'src\renderer\components\mytitle\Titlebtn.vue'api
<template> <div class="titlebtn" v-bind:style="style" v-on:click="click"/> </template> <script> const {ipcRenderer: ipc} = require('electron'); const style = { min: { backgroundColor: 'green', right: '100px' }, max: { backgroundColor: 'yellow', right: '60px' }, close: { backgroundColor: 'black', right: '20px' } }; export default { name: 'Titlebtn', props: ['type'], computed: { style: function () { return style[this.type]; } }, methods: { click: function () { ipc.send(this.type); } } } </script> <style> .titlebtn { position: absolute; width: 20px; height: 20px; top: 0; bottom: 0; margin: auto 0; -webkit-app-region: no-drag; } </style>
这里须要注意一下,由于以前咱们设置标题栏样式-webkit-app-region: drag,这里按钮必须设置样式-webkit-app-region: no-drag,否则按钮将没法选中或点击
鼠标点击按钮后,经过ipcRenderer向主进程发送消息
而后修改咱们的Titlebtn组件,以下app
<template> <div id="mytitle"> <Titlebtn type="min" /> <Titlebtn type="max" /> <Titlebtn type="close" /> </div> </template> <script> import Titlebtn from './Titlebtn.vue'; export default { name: 'Mytitle', components: { Titlebtn } } </script> <style> #mytitle { position: absolute; width: 100%; height: 52px; background-color: rgb(198, 47, 47); -webkit-app-region: drag; } </style>
修改主进程index.js,监听渲染进程的消息,并根据消息执行相应的动做electron
import { ipcMain } from 'electron' ipcMain.on('min', e=> mainWindow.minimize()); ipcMain.on('max', e=> { if (mainWindow.isMaximized()) { mainWindow.unmaximize() } else { mainWindow.maximize() } }); ipcMain.on('close', e=> mainWindow.close());