electron+vue——屏蔽顶部标题栏最大化按钮
js:
import { BrowserWindow } from "electron";
function createWindow() {
// 创建浏览器窗口
mainWindow = new BrowserWindow({
width: 1000,
height: 600,
// maximizable: false, // windows下有效,禁用最大化按钮
resizable: false, // windows下禁用最大化按钮,linux下隐藏最大化按钮
...
});
}
自定义标题栏也可以实现(win/linux兼容),但要考虑窗口大小,不然会出现滚动条
function createWindow() { // 创建浏览器窗口 mainWindow = new BrowserWindow({ width: 1000, height: 630, // 内容高度600+标题栏高度30 titleBarStyle: "hidden", // 隐藏默认标题栏 frame: false, // 隐藏边框 ... }); }
App.vue:
<template>
<div id="app">
<!-- 自定义标题栏 -->
<TitleBar
:app-name="appName"
@window-control="handleWindowControl"
></TitleBar>
<router-view />
</div>
</template>
<script>
import TitleBar from "./views/components/titleBar.vue";
export default {
name: "App",
components: { TitleBar },
data() {
return { appName: "测试标题" };
},
methods: {
// 处理窗口控制
handleWindowControl(action) {
switch (action) {
case "minimize":
window.desktopHandler.minimizeWindow();
break;
case "close":
window.desktopHandler.confirmClose();
break;
}
},
},
};
</script>
titleBar.vue:
<template>
<div class="title-bar">
<!-- 左侧应用信息 -->
<div class="left-section">
<div class="app-info">
<span class="app-name">{{ appName }}</span>
</div>
</div>
<!-- 右侧窗口控制按钮 -->
<div class="window-controls">
<button
class="control-btn"
@click="controlWindow('minimize')"
title="最小化"
>
—
</button>
<button
class="control-btn close"
@click="controlWindow('close')"
title="关闭"
>
X
</button>
</div>
</div>
</template>
<script>
export default {
props: {
appName: {
type: String,
default: "",
},
},
methods: {
controlWindow(action) {
this.$emit("window-control", action);
},
},
};
</script>
<style scoped>
.title-bar {
width: 100%;
-webkit-app-region: drag;
height: 30px;
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(20px);
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
display: flex;
align-items: center;
justify-content: space-between;
position: fixed;
z-index: 1000;
transition: all 0.3s ease;
user-select: none;
}
/* 左侧区域 */
.left-section {
display: flex;
align-items: center;
gap: 12px;
}
.app-info {
display: flex;
flex-direction: column;
gap: 2px;
margin-left: 20px;
}
.app-name {
font-size: 12px;
}
/* 窗口控制按钮 */
.window-controls {
-webkit-app-region: no-drag;
display: flex;
gap: 8px;
}
.control-btn {
width: 32px;
height: 32px;
border: none;
border-radius: 6px;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: all 0.3s ease;
background: rgba(255, 255, 255, 0);
position: relative;
overflow: hidden;
}
.control-btn::before {
content: "";
position: absolute;
top: 0;
left: -100%;
width: 100%;
height: 100%;
background: linear-gradient(
90deg,
transparent,
rgba(255, 255, 255, 0.2),
transparent
);
transition: left 0.5s;
}
.control-btn:hover::before {
left: 100%;
}
.control-btn:hover {
background: rgba(255, 255, 255, 0.2);
transform: translateY(-1px);
}
.control-btn.close:hover {
background: #ff4757;
}
</style>
注意窗口内容需要相对定位:
.container {
width: 1000px;
height: 600px;
position: absolute;
top: 30px;
}
preload.js:
import { contextBridge, ipcRenderer } from "electron";
contextBridge.exposeInMainWorld("desktopHandler", {
confirmClose: () => ipcRenderer.send("confirm-window-close"),
minimizeWindow: () => ipcRenderer.send("minimize-window"),
});

浙公网安备 33010602011771号