electron自定义标题栏

20pzqm
• 阅读 3177

上贴传送门

【electron】ipc模块使用

electron 自定义标题栏

官方资料

隐藏默认标题栏

const { BrowserWindow } = require('electron')
const win = new BrowserWindow({ frame: false })// frame设置为false

支持拖拽

默认情况下, 无边框窗口是不可拖拽的。 应用程序需要在 CSS 中指定 -webkit-app-region: drag 来告诉 Electron 哪些区域是可拖拽的(如操作系统的标准标题栏). 在可拖拽区域内部使用 -webkit-app-region: no-drag 则可以将其中部分区域排除。 可拖拽

body {
  -webkit-app-region: drag;
}

不可拖拽

body {
  -webkit-app-region: drag;
}

示例

目标

  • 制作自定义标题栏,支持拖拽
  • 制作自定义的最大\最小\关闭按钮

    前提

    基于上一帖的代码
    https://github.com/NightsLight-hub/vcped-test
    tag:1-ipc

    引入ant-design-vue

    本人非常喜欢ant-design-vue框架, 本节主要以其作为基础组件库 在vcpeb-test 根目录使用如下命令安装ant-desing-vue
    yarn add ant-design-vue@next
    在main.js中引入antd
    import { createApp } from 'vue';
    import App from './App.vue';
    import router from './router';
    import store from './store';
    import Antd from 'ant-design-vue';
    import 'ant-design-vue/dist/antd.css';
    

createApp(App).use(Antd).use(store).use(router).mount('#app');

## 修改`App.vue`,只保留一个`router-view`即可
```html
<template>
  <router-view/>
</template>

<style lang="less">
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
</style>

修改background.js的frame=false

const browserWindow = new BrowserWindow({
    width: 800,
    height: 600,
    frame: false, // 关闭默认标题栏
    webPreferences: {
      // Use pluginOptions.nodeIntegration, leave this alone
      // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
      nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
      contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,
      preload: path.join(__dirname, 'preload.js')
    }
  });

修改Home.vue

<template>
<!--  使用antd的header 布局,作为标题栏-->
  <a-layout-header>

  </a-layout-header>
  <a-layout-content class="layout-content">
    <div class="home">
      <span>{{ msg }}</span>
    </div>
  </a-layout-content>

</template>

<script>
// @ is an alias to /src

export default {
  name: 'Home',
  data () {
    return {
      msg: ''
    };
  },
  mounted () {
    // eslint-disable-next-line no-debugger
    debugger;
    window.ipcRenderer.receive('mainMsg', (event, ...args) => {
      console.log('get mainMsg');
      this.msg = args[0];
    });
  }
};
</script>
<style scoped>
.ant-layout-header{
  width: 100%;
  height: 65px;
  /* 标题栏设置个便于区分的颜色,可以拖拽用 */
  background-color: #2c3e50;
  /* 设置标题栏可以拖拽 */
  -webkit-app-region: drag;

}
.layout-content{
  height: calc(100vh - 100px);
  width: 100%;
}
</style>

调试

yarn electron:serve

深色部分可以按住左键拖拽窗口

增加最大\最小\关闭按钮

增加三个按钮的组件 关闭按钮组件

<template>
  <a-button id="closeButton" type="text" title="close" @click="closeWindow">
    <template #icon><CloseOutlined /></template>
  </a-button>
</template>

<script>
import { CloseOutlined } from '@ant-design/icons-vue';
export default {
  name: 'closeButton',
  components: {
    CloseOutlined
  },
  methods: {
    closeWindow () {
      const ipcRenderer = window.ipcRenderer;
      ipcRenderer.send('control', 'close');
    }
  }
};
</script>

<style>
#closeButton {
  position: absolute;
  width: 30px;
  height: 30px;
  top: 5px;
  right: 20px;
  margin: auto 0;
  -webkit-app-region: no-drag;
}
</style>

最大化窗口按钮

<template>
  <a-button id="maxButton" type="text" @click="maxWindow">
    <template #icon><FullscreenOutlined /></template>
  </a-button>
</template>

<script>
import { FullscreenOutlined } from '@ant-design/icons-vue';
export default {
  name: 'maxButton',
  components: {
    FullscreenOutlined
  },
  methods: {
    maxWindow () {
      const ipcRenderer = window.ipcRenderer;
      ipcRenderer.send('control', 'max');
    }
  }
};
</script>

<style>
#maxButton {
  position: absolute;
  width: 30px;
  height: 30px;
  top: 5px;
  right: 60px;
  margin: auto 0;
  -webkit-app-region: no-drag;
}
</style>

最小化按钮

<template>
  <a-button id="minButton" type="text" @click="minWindow">
    <template #icon>
      <FullscreenExitOutlined />
    </template>
  </a-button>
</template>

<script>
import { FullscreenExitOutlined } from '@ant-design/icons-vue';
export default {
  name: 'MinButton',
  components: {
    FullscreenExitOutlined
  },
  methods: {
    minWindow () {
      const ipcRenderer = window.ipcRenderer;
      ipcRenderer.send('control', 'min');
    }
  }
};
</script>

<style>
#minButton {
  position: absolute;
  width: 30px;
  height: 30px;
  top: 5px;
  right: 100px;
  margin: auto 0;
  -webkit-app-region: no-drag;
}
</style>

三个按钮的点击事件利用ipcRender ,通过control通道发送响应控制消息给主进程 比如最小化按钮

minWindow () {
  const ipcRenderer = window.ipcRenderer;
  ipcRenderer.send('control', 'min');
}

主进程增加对control通道的响应

ipcMain.on('control', (event, ...args) => {
    if (args[0] === 'min') {
      browserWindow.minimize();
    } else if (args[0] === 'max') {
      if (browserWindow.isMaximized()) {
        browserWindow.unmaximize();
      } else {
        browserWindow.maximize();
      }
    } else if (args[0] === 'close') {
      console.log('close event');
      browserWindow.close();
    }
  });

Home.vue 把三个按钮放到标题栏

<a-layout-header>
    <close-button></close-button>
    <max-button></max-button>
    <min-button></min-button>
  </a-layout-header>

适当调整了 标题栏背景色

.ant-layout-header{
  width: 100%;
  height: 65px;
  /* 标题栏设置个便于区分的颜色,可以拖拽用 */
  background-color: #18bae5;
  /* 设置标题栏可以拖拽 */
  -webkit-app-region: drag;

}

调试

electron自定义标题栏

获取本章代码

https://github.com/NightsLight-hub/vcped-test
tag: 2-customTitleBar
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
Java获得今日零时零分零秒的时间(Date型)
publicDatezeroTime()throwsParseException{    DatetimenewDate();    SimpleDateFormatsimpnewSimpleDateFormat("yyyyMMdd00:00:00");    SimpleDateFormatsimp2newS
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
2年前
JOptionPane修改图标
1.在Linux平台下.JOptionPane会显示Java默认的图标,在window平台不显示图标,如何替换这个图标了?2JOptionPane.setIcon(Icon)修改的是内容区域的icon,而不是左上角的Icon.所以需要通过修改Jdialog/Frame的图标来达到修改默认图标的问题.3.代码:if(JOptio
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这