自定义窗口样式
¥Custom Window Styles
无框窗口
¥Frameless windows
无框架窗口会删除操作系统应用的所有 chrome,包括窗口控件。
¥A frameless window removes all chrome applied by the OS, including window controls.
要创建无框窗口,请将 BrowserWindow
构造函数中的 BaseWindowContructorOptions
frame
参数设置为 false
。
¥To create a frameless window, set the BaseWindowContructorOptions
frame
param in the BrowserWindow
constructor to false
.
- main.js
const { app, BrowserWindow } = require('electron')
function createWindow () {
const win = new BrowserWindow({
width: 300,
height: 200,
frame: false
})
win.loadURL('https://example.com')
}
app.whenReady().then(() => {
createWindow()
})
透明窗口
¥Transparent windows
要创建完全透明的窗口,请将 BrowserWindow
构造函数中的 BaseWindowContructorOptions
transparent
参数设置为 true
。
¥To create a fully transparent window, set the BaseWindowContructorOptions
transparent
param in the BrowserWindow
constructor to true
.
以下小提琴利用透明窗口和 CSS 样式来创建圆形窗口的幻觉。
¥The following fiddle takes advantage of a tranparent window and CSS styling to create the illusion of a circular window.
- main.js
- index.html
- styles.css
const { app, BrowserWindow } = require('electron')
function createWindow () {
const win = new BrowserWindow({
width: 100,
height: 100,
resizable: false,
frame: false,
transparent: true
})
win.loadFile('index.html')
}
app.whenReady().then(() => {
createWindow()
})
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'">
<link href="./styles.css" rel="stylesheet">
<title>Transparent Hello World</title>
</head>
<body>
<div class="white-circle">
<div>Hello World!</div>
</div>
</body>
</html>
body {
margin: 0;
padding: 0;
background-color: rgba(0, 0, 0, 0); /* Transparent background */
}
.white-circle {
width: 100px;
height: 100px;
background-color: white;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
app-region: drag;
user-select: none;
}
局限性
¥Limitations
-
你无法单击透明区域。详情请参见 #1335。
¥You cannot click through the transparent area. See #1335 for details.
-
透明窗口不可调整大小。将
resizable
设置为true
可能会使透明窗口在某些平台上停止工作。¥Transparent windows are not resizable. Setting
resizable
totrue
may make a transparent window stop working on some platforms. -
CSS
blur()
过滤器仅适用于窗口的网页内容,因此无法对窗口下方的内容(即用户系统上打开的其他应用)应用模糊效果。¥The CSS
blur()
filter only applies to the window's web contents, so there is no way to apply blur effect to the content below the window (i.e. other applications open on the user's system). -
打开 DevTools 时,窗口不会是透明的。
¥The window will not be transparent when DevTools is opened.
-
在 Windows 上:
¥On Windows:
-
在 macOS 上:
¥On macOS:
-
原生窗口阴影不会显示在透明窗口上。
¥The native window shadow will not be shown on a transparent window.
-