[亂說]Electron 無邊框視窗最大化最小化關閉功能
阿新 • • 發佈:2019-01-25
Electron 無邊框視窗最大化最小化關閉功能
目的
- 實現無邊框視窗,並新增最大化最小化和關閉功能
前提
操作流程
- 先在介面上放三個按鈕
<body style="-webkit-app-region: drag">
<section style="-webkit-app-region: drag">
<!--html程式碼-->
<h1>Hello World!</h1>
</section>
<section style="-webkit-app-region: no-drag">
<button type="button" id="maxbt">max</button>
<button type="button" id="minbt">>min</button>
<button type="button" id="closebt">>close</button>
</section>
<!-- All of the Node.js APIs are available in this renderer process. -->
We are using Node.js <script>document.write(process.versions.node)</script>,
Chromium <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
<script>
// require('electron').ipcRenderer;
// You can also require other files to run in this process
require('./renderer.js')
</script>
</body>
```
**-webkit-app-region: drag是可拖動的樣式**
2. 新增事件把要做的操作傳送給主程序
``` javascript
var ipc = require('electron').ipcRenderer;
document.getElementById('maxbt').addEventListener('click', () => {
console.log('hello vscode!')
ipc.send('window-max');
})
document.getElementById('minbt').addEventListener('click', () => {
console.log('hello vscode!')
ipc.send('window-min');
})
document.getElementById('closebt').addEventListener('click', () => {
console.log('hello vscode!')
ipc.send('window-close');
})
<div class="se-preview-section-delimiter"></div>
- 在主程序中調相應的方法
const electron = require('electron')
const ipc = electron.ipcMain
//登入視窗最小化
ipc.on('window-min',function(){
mainWindow.minimize();
})
//登入視窗最大化
ipc.on('window-max',function(){
if(mainWindow.isMaximized()){
mainWindow.restore();
}else{
mainWindow.maximize();
}
})
ipc.on('window-close',function(){
mainWindow.close();
})
提示
基礎不穩寫出來的程式碼也不是很好,開源的學習成本其實是很高的,特別是對於新手,自己學習一定要把握好度。
