Skip to main content

自定义窗口样式

🌐 Custom Window Styles

无框窗口

🌐 Frameless windows

Frameless Window

无边框窗口会移除操作系统施加的所有 谷歌浏览器,包括窗口控件。

🌐 A frameless window removes all chrome applied by the OS, including window controls.

要创建无边框窗口,请在 BrowserWindow 构造函数中将 BaseWindowContructorOptionsframe 参数设置为 false

🌐 To create a frameless window, set the BaseWindowContructorOptions frame param in the BrowserWindow constructor to false.

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

透明窗口 macOS 任务控制中的透明窗口

要创建一个完全透明的窗口,请在 BrowserWindow 构造函数中将 BaseWindowContructorOptionstransparent 参数设置为 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 transparent window and CSS styling to create the illusion of a circular window.

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()
})

局限性

🌐 Limitations

  • 你无法点击透明区域。详情请参见 #1335
  • 透明窗口不可调整大小。将 resizable 设置为 true 可能会导致透明窗口在某些平台上无法正常工作。
  • CSS blur() 滤镜仅适用于窗口的网页内容,因此无法对窗口下方的内容(例如用户系统中打开的其他应用)应用模糊效果。
  • 打开 DevTools 时,窗口不会是透明的。
  • Windows 上:
    • 透明窗口不能通过 Windows 系统菜单或双击标题栏来最大化。这一限制的原因可以在 PR #28207 中看到。
  • macOS 上:
    • 原生窗口阴影不会显示在透明窗口上。