Skip to main content

BaseWindow

创建和控制窗口。

¥Create and control windows.

进程:主进程

¥Process: Main

注意 BaseWindow 提供了一种在单个窗口中组合多个 Web 视图的灵活方法。对于只有一个全尺寸 Web 视图的窗口,BrowserWindow 类可能是一个更简单的选择。

¥BaseWindow provides a flexible way to compose multiple web views in a single window. For windows with only a single, full-size web view, the BrowserWindow class may be a simpler option.

app 模块的 ready 事件发出之前,该模块无法使用。

¥This module cannot be used until the ready event of the app module is emitted.

// In the main process.
const { BaseWindow, WebContentsView } = require('electron')

const win = new BaseWindow({ width: 800, height: 600 })

const leftView = new WebContentsView()
leftView.webContents.loadURL('https://electron.nodejs.cn')
win.contentView.addChildView(leftView)

const rightView = new WebContentsView()
rightView.webContents.loadURL('https://github.com/electron/electron')
win.contentView.addChildView(rightView)

leftView.setBounds({ x: 0, y: 0, width: 400, height: 600 })
rightView.setBounds({ x: 400, y: 0, width: 400, height: 600 })

父窗口和子窗口

¥Parent and child windows

通过使用 parent 选项,你可以创建子窗口:

¥By using parent option, you can create child windows:

const { BaseWindow } = require('electron')

const parent = new BaseWindow()
const child = new BaseWindow({ parent })

child 窗口将始终显示在 parent 窗口的顶部。

¥The child window will always show on top of the parent window.

¥Modal windows

模态窗口是禁用父窗口的子窗口。要创建模态窗口,你必须设置 parentmodal 选项:

¥A modal window is a child window that disables parent window. To create a modal window, you have to set both the parent and modal options:

const { BaseWindow } = require('electron')

const parent = new BaseWindow()
const child = new BaseWindow({ parent, modal: true })

平台须知

¥Platform notices

  • 在 macOS 上,模式窗口将显示为附加到父窗口的工作表。

    ¥On macOS modal windows will be displayed as sheets attached to the parent window.

  • 在 macOS 上,当父窗口移动时,子窗口将保持与父窗口的相对位置,而在 Windows 和 Linux 上,子窗口不会移动。

    ¥On macOS the child windows will keep the relative position to parent window when parent window moves, while on Windows and Linux child windows will not move.

  • 在 Linux 上,模式窗口的类型将更改为 dialog

    ¥On Linux the type of modal windows will be changed to dialog.

  • 在 Linux 上,许多桌面环境不支持隐藏模式窗口。

    ¥On Linux many desktop environments do not support hiding a modal window.

类:BaseWindow

¥Class: BaseWindow

创建和控制窗口。

¥Create and control windows.

进程:主进程

¥Process: Main

BaseWindowEventEmitter

¥BaseWindow is an EventEmitter.

它创建一个新的 BaseWindow,其原生属性由 options 设置。

¥It creates a new BaseWindow with native properties as set by the options.

new BaseWindow([options])

  • options BaseWindowConstructorOptions(可选)

    ¥options BaseWindowConstructorOptions (optional)

    • width 整数(可选) - 窗口的宽度(以像素为单位)。默认为 800

      ¥width Integer (optional) - Window's width in pixels. Default is 800.

    • height 整数(可选) - 窗口的高度(以像素为单位)。默认为 600

      ¥height Integer (optional) - Window's height in pixels. Default is 600.

    • x 整数(可选) - (如果使用 y,则为必需)窗口相对于屏幕的左侧偏移量。默认是将窗口居中。

      ¥x Integer (optional) - (required if y is used) Window's left offset from screen. Default is to center the window.

    • y 整数(可选) - (如果使用 x,则为必需)窗口顶部距屏幕的偏移量。默认是将窗口居中。

      ¥y Integer (optional) - (required if x is used) Window's top offset from screen. Default is to center the window.

    • useContentSize 布尔值(可选) - widthheight 将用作网页的尺寸,这意味着实际窗口的尺寸将包括窗框的尺寸并稍大一些。默认为 false

      ¥useContentSize boolean (optional) - The width and height would be used as web page's size, which means the actual window's size will include window frame's size and be slightly larger. Default is false.

    • center 布尔值(可选) - 在屏幕中央显示窗口。默认为 false

      ¥center boolean (optional) - Show window in the center of the screen. Default is false.

    • minWidth 整数(可选) - 窗口的最小宽度。默认为 0

      ¥minWidth Integer (optional) - Window's minimum width. Default is 0.

    • minHeight 整数(可选) - 窗口的最小高度。默认为 0

      ¥minHeight Integer (optional) - Window's minimum height. Default is 0.

    • maxWidth 整数(可选) - 窗口的最大宽度。默认没有限制。

      ¥maxWidth Integer (optional) - Window's maximum width. Default is no limit.

    • maxHeight 整数(可选) - 窗口的最大高度。默认没有限制。

      ¥maxHeight Integer (optional) - Window's maximum height. Default is no limit.

    • resizable 布尔值(可选) - 窗口是否可调整大小。默认为 true

      ¥resizable boolean (optional) - Whether window is resizable. Default is true.

    • movable 布尔值(可选) macOS Windows - 窗户是否可移动。这在 Linux 上没有实现。默认为 true

      ¥movable boolean (optional) macOS Windows - Whether window is movable. This is not implemented on Linux. Default is true.

    • minimizable 布尔值(可选) macOS Windows - 窗口是否可最小化。这在 Linux 上没有实现。默认为 true

      ¥minimizable boolean (optional) macOS Windows - Whether window is minimizable. This is not implemented on Linux. Default is true.

    • maximizable 布尔值(可选) macOS Windows - 窗口是否可最大化。这在 Linux 上没有实现。默认为 true

      ¥maximizable boolean (optional) macOS Windows - Whether window is maximizable. This is not implemented on Linux. Default is true.

    • closable 布尔值(可选) macOS Windows - 窗口是否可关闭。这在 Linux 上没有实现。默认为 true

      ¥closable boolean (optional) macOS Windows - Whether window is closable. This is not implemented on Linux. Default is true.

    • focusable 布尔值(可选) - 窗口是否可以聚焦。默认为 true。在 Windows 上,设置 focusable: false 也意味着设置 skipTaskbar: true。在 Linux 上,设置 focusable: false 使窗口停止与 wm 交互,因此窗口将始终位于所有工作区的顶部。

      ¥focusable boolean (optional) - Whether the window can be focused. Default is true. On Windows setting focusable: false also implies setting skipTaskbar: true. On Linux setting focusable: false makes the window stop interacting with wm, so the window will always stay on top in all workspaces.

    • alwaysOnTop 布尔值(可选) - 窗口是否应始终位于其他窗口之上。默认为 false

      ¥alwaysOnTop boolean (optional) - Whether the window should always stay on top of other windows. Default is false.

    • fullscreen 布尔值(可选) - 窗口是否应全屏显示。当明确设置为 false 时,全屏按钮将在 macOS 上隐藏或禁用。默认为 false

      ¥fullscreen boolean (optional) - Whether the window should show in fullscreen. When explicitly set to false the fullscreen button will be hidden or disabled on macOS. Default is false.

    • fullscreenable 布尔值(可选) - 窗口是否可以进入全屏模式。在 macOS 上,还包括最大化/缩放按钮是否应切换全屏模式或最大化窗口。默认为 true

      ¥fullscreenable boolean (optional) - Whether the window can be put into fullscreen mode. On macOS, also whether the maximize/zoom button should toggle full screen mode or maximize window. Default is true.

    • simpleFullscreen 布尔值(可选)macOS - 在 macOS 上使用 Lion 之前的全屏。默认为 false

      ¥simpleFullscreen boolean (optional) macOS - Use pre-Lion fullscreen on macOS. Default is false.

    • skipTaskbar 布尔值(可选) macOS Windows - 是否在任务栏中显示窗口。默认为 false

      ¥skipTaskbar boolean (optional) macOS Windows - Whether to show the window in taskbar. Default is false.

    • hiddenInMissionControl 布尔值(可选)macOS - 当用户切换到任务控制时是否应隐藏窗口。

      ¥hiddenInMissionControl boolean (optional) macOS - Whether window should be hidden when the user toggles into mission control.

    • kiosk 布尔值(可选) - 窗口是否处于 kiosk 模式。默认为 false

      ¥kiosk boolean (optional) - Whether the window is in kiosk mode. Default is false.

    • title 字符串(可选) - 默认窗口标题。默认为 "Electron"。如果 loadURL() 加载的 HTML 文件中定义了 HTML 标签 <title>,则该属性将被忽略。

      ¥title string (optional) - Default window title. Default is "Electron". If the HTML tag <title> is defined in the HTML file loaded by loadURL(), this property will be ignored.

    • iconNativeImage | 字符串)(可选) - 窗口图标。在 Windows 上,建议使用 ICO 图标以获得最佳视觉效果,你也可以将其保留为未定义,以便使用可执行文件的图标。

      ¥icon (NativeImage | string) (optional) - The window icon. On Windows it is recommended to use ICO icons to get best visual effects, you can also leave it undefined so the executable's icon will be used.

    • show 布尔值(可选) - 创建时是否应显示窗口。默认为 true

      ¥show boolean (optional) - Whether window should be shown when created. Default is true.

    • frame 布尔值(可选) - 指定 false 创建 无框窗。默认为 true

      ¥frame boolean (optional) - Specify false to create a frameless window. Default is true.

    • parent 基本窗口(可选) - 指定父窗口。默认为 null

      ¥parent BaseWindow (optional) - Specify parent window. Default is null.

    • modal 布尔值(可选) - 这是否是一个模态窗口。仅当窗口是子窗口时才有效。默认为 false

      ¥modal boolean (optional) - Whether this is a modal window. This only works when the window is a child window. Default is false.

    • acceptFirstMouse 布尔值(可选)macOS - 单击非活动窗口是否也会单击进入 Web 内容。macOS 上的默认值为 false。此选项在其他平台上不可配置。

      ¥acceptFirstMouse boolean (optional) macOS - Whether clicking an inactive window will also click through to the web contents. Default is false on macOS. This option is not configurable on other platforms.

    • disableAutoHideCursor 布尔值(可选) - 打字时是否隐藏光标。默认为 false

      ¥disableAutoHideCursor boolean (optional) - Whether to hide cursor when typing. Default is false.

    • autoHideMenuBar 布尔值(可选) - 除非按下 Alt 键,否则自动隐藏菜单栏。默认为 false

      ¥autoHideMenuBar boolean (optional) - Auto hide the menu bar unless the Alt key is pressed. Default is false.

    • enableLargerThanScreen 布尔值(可选)macOS - 使窗口大小能够调整为大于屏幕。仅与 macOS 相关,因为其他操作系统默认允许大于屏幕的窗口。默认为 false

      ¥enableLargerThanScreen boolean (optional) macOS - Enable the window to be resized larger than screen. Only relevant for macOS, as other OSes allow larger-than-screen windows by default. Default is false.

    • backgroundColor 字符串(可选) - 窗口的背景颜色采用 Hex、RGB、RGBA、HSL、HSLA 或命名的 CSS 颜色格式。如果 transparent 设置为 true,则支持 #AARRGGBB 格式的 Alpha。默认为 #FFF(白色)。请参阅 win.setBackgroundColor 了解更多信息。

      ¥backgroundColor string (optional) - The window's background color in Hex, RGB, RGBA, HSL, HSLA or named CSS color format. Alpha in #AARRGGBB format is supported if transparent is set to true. Default is #FFF (white). See win.setBackgroundColor for more information.

    • hasShadow 布尔值(可选) - 窗口是否应该有阴影。默认为 true

      ¥hasShadow boolean (optional) - Whether window should have a shadow. Default is true.

    • opacity 数字(可选) macOS Windows - 设置窗口的初始不透明度,介于 0.0(完全透明)和 1.0(完全不透明)之间。这仅在 Windows 和 macOS 上实现。

      ¥opacity number (optional) macOS Windows - Set the initial opacity of the window, between 0.0 (fully transparent) and 1.0 (fully opaque). This is only implemented on Windows and macOS.

    • darkTheme 布尔值(可选) - 强制窗口使用深色主题,仅适用于某些 GTK+3 桌面环境。默认为 false

      ¥darkTheme boolean (optional) - Forces using dark theme for the window, only works on some GTK+3 desktop environments. Default is false.

    • transparent 布尔值(可选) - 制作窗口 transparent。默认为 false。在 Windows 上,除非窗口是无框的,否则不起作用。

      ¥transparent boolean (optional) - Makes the window transparent. Default is false. On Windows, does not work unless the window is frameless.

    • type 字符串(可选) - 窗口类型,默认为普通窗口。请参阅下文了解更多相关信息。

      ¥type string (optional) - The type of window, default is normal window. See more about this below.

    • visualEffectState 字符串(可选)macOS - 指定材质外观应如何反映 macOS 上的窗口活动状态。必须与 vibrancy 属性一起使用。可能的值为:

      ¥visualEffectState string (optional) macOS - Specify how the material appearance should reflect window activity state on macOS. Must be used with the vibrancy property. Possible values are:

      • followWindow - 当窗口处于活动状态时,背景应自动显示为活动状态,而当窗口处于非活动状态时,背景应自动显示为非活动状态。这是默认设置。

        ¥followWindow - The backdrop should automatically appear active when the window is active, and inactive when it is not. This is the default.

      • active - 背景应该始终显示为活动状态。

        ¥active - The backdrop should always appear active.

      • inactive - 背景应该始终显示为非活动状态。

        ¥inactive - The backdrop should always appear inactive.

    • titleBarStyle 字符串(可选) - 窗口标题栏的样式。默认为 default。可能的值为:

      ¥titleBarStyle string (optional) - The style of window title bar. Default is default. Possible values are:

      • default - 分别产生 macOS 或 Windows 的标准标题栏。

        ¥default - Results in the standard title bar for macOS or Windows respectively.

      • hidden - 结果是隐藏的标题栏和全尺寸的内容窗口。在 macOS 上,窗口的左上角仍然有标准窗口控件(“红绿灯”)。在 Windows 和 Linux 上,当与 titleBarOverlay: true 结合使用时,它将激活窗口控件覆盖(有关更多信息,请参阅 titleBarOverlay),否则不会显示任何窗口控件。

        ¥hidden - Results in a hidden title bar and a full size content window. On macOS, the window still has the standard window controls (“traffic lights”) in the top left. On Windows and Linux, when combined with titleBarOverlay: true it will activate the Window Controls Overlay (see titleBarOverlay for more information), otherwise no window controls will be shown.

      • hiddenInset macOS - 结果会出现一个隐藏的标题栏,但红绿灯按钮会稍微偏离窗口边缘。

        ¥hiddenInset macOS - Results in a hidden title bar with an alternative look where the traffic light buttons are slightly more inset from the window edge.

      • customButtonsOnHover macOS - 结果会出现一个隐藏的标题栏和一个全尺寸的内容窗口,当将鼠标悬停在窗口左上角时,红绿灯按钮将显示出来。注意:此选项目前处于实验阶段。

        ¥customButtonsOnHover macOS - Results in a hidden title bar and a full size content window, the traffic light buttons will display when being hovered over in the top left of the window. Note: This option is currently experimental.

    • titleBarOverlay Object | Boolean (optional) - 当将无框窗口与 macOS 上的 win.setWindowButtonVisibility(true) 结合使用或使用 titleBarStyle 以使标准窗口控件(macOS 上的 "红绿灯")可见时,此属性将启用窗口控件覆盖 JavaScript APICSS 环境变量。指定 true 将导致使用默认系统颜色进行叠加。默认为 false

      ¥titleBarOverlay Object | Boolean (optional) - When using a frameless window in conjunction with win.setWindowButtonVisibility(true) on macOS or using a titleBarStyle so that the standard window controls ("traffic lights" on macOS) are visible, this property enables the Window Controls Overlay JavaScript APIs and CSS Environment Variables. Specifying true will result in an overlay with default system colors. Default is false.

      • color 字符串(可选)Windows Linux - 启用时窗口控件叠加层的 CSS 颜色。默认为系统颜色。

        ¥color String (optional) Windows Linux - The CSS color of the Window Controls Overlay when enabled. Default is the system color.

      • symbolColor 字符串(可选) Windows - 启用后窗口控件叠加上符号的 CSS 颜色。默认为系统颜色。

        ¥symbolColor String (optional) Windows - The CSS color of the symbols on the Window Controls Overlay when enabled. Default is the system color.

      • height 整数(可选) - 标题栏和窗口控件叠加的高度(以像素为单位)。默认为系统高度。

        ¥height Integer (optional) - The height of the title bar and Window Controls Overlay in pixels. Default is system height.

    • trafficLightPosition 观点(可选)macOS - 在无框窗户中设置交通灯按钮的自定义位置。

      ¥trafficLightPosition Point (optional) macOS - Set a custom position for the traffic light buttons in frameless windows.

    • roundedCorners 布尔值(可选)macOS - 无框窗口在 macOS 上是否应具有圆角。默认为 true。将此属性设置为 false 将阻止窗口全屏显示。

      ¥roundedCorners boolean (optional) macOS - Whether frameless window should have rounded corners on macOS. Default is true. Setting this property to false will prevent the window from being fullscreenable.

    • thickFrame 布尔值(可选) - 在 Windows 上对无框窗户使用 WS_THICKFRAME 样式,这会添加标准窗框。将其设置为 false 将删除窗口阴影和窗口动画。默认为 true

      ¥thickFrame boolean (optional) - Use WS_THICKFRAME style for frameless windows on Windows, which adds standard window frame. Setting it to false will remove window shadow and window animations. Default is true.

    • vibrancy 字符串(可选)macOS - 向窗口添加一种活力效果,仅限 macOS。可以是 appearance-basedtitlebarselectionmenupopoversidebarheadersheetwindowhudfullscreen-uitooltipcontentunder-windowunder-page

      ¥vibrancy string (optional) macOS - Add a type of vibrancy effect to the window, only on macOS. Can be appearance-based, titlebar, selection, menu, popover, sidebar, header, sheet, window, hud, fullscreen-ui, tooltip, content, under-window, or under-page.

    • backgroundMaterial 字符串(可选) Windows - 设置窗口的系统绘制背景材质,包括非客户区后面。可以是 autononemicaacrylictabbed。请参阅 win.setBackgroundMaterial 了解更多信息。

      ¥backgroundMaterial string (optional) Windows - Set the window's system-drawn background material, including behind the non-client area. Can be auto, none, mica, acrylic or tabbed. See win.setBackgroundMaterial for more information.

    • zoomToPageWidth 布尔值(可选)macOS - 控制 macOS 上按住 Option 键单击工具栏上的绿色红绿灯按钮或单击“窗口”>“缩放”菜单项时的行为。如果是 true,则窗口在缩放时将增长到网页的首选宽度,false 将使其缩放到屏幕的宽度。这也会影响直接调用 maximize() 时的行为。默认为 false

      ¥zoomToPageWidth boolean (optional) macOS - Controls the behavior on macOS when option-clicking the green stoplight button on the toolbar or by clicking the Window > Zoom menu item. If true, the window will grow to the preferred width of the web page when zoomed, false will cause it to zoom to the width of the screen. This will also affect the behavior when calling maximize() directly. Default is false.

    • tabbingIdentifier 字符串(可选)macOS - 选项卡组名称,允许将窗口作为原生选项卡打开。具有相同选项卡标识符的窗口将被分组在一起。这还会向窗口的选项卡栏添加一个原生新选项卡按钮,并允许 app 和窗口接收 new-window-for-tab 事件。

      ¥tabbingIdentifier string (optional) macOS - Tab group name, allows opening the window as a native tab. Windows with the same tabbing identifier will be grouped together. This also adds a native new tab button to your window's tab bar and allows your app and window to receive the new-window-for-tab event.

当使用 minWidth/maxWidth/minHeight/maxHeight 设置最小或最大窗口大小时,它仅限制用户。它不会阻止你将不遵循大小限制的大小传递给 setBounds/setSizeBrowserWindow 的构造函数。

¥When setting minimum or maximum window size with minWidth/maxWidth/ minHeight/maxHeight, it only constrains the users. It won't prevent you from passing a size that does not follow size constraints to setBounds/setSize or to the constructor of BrowserWindow.

type 选项的可能值和行为取决于平台。可能的值为:

¥The possible values and behaviors of the type option are platform dependent. Possible values are:

  • 在 Linux 上,可能的类型为 desktopdocktoolbarsplashnotification

    ¥On Linux, possible types are desktop, dock, toolbar, splash, notification.

    • desktop 类型将窗口放置在桌面背景窗口级别 (kCGDesktopWindowLevel - 1)。但请注意,桌面窗口不会接收焦点、键盘或鼠标事件。你仍然可以使用 globalShortcut 来谨慎地接收输入。

      ¥The desktop type places the window at the desktop background window level (kCGDesktopWindowLevel - 1). However, note that a desktop window will not receive focus, keyboard, or mouse events. You can still use globalShortcut to receive input sparingly.

    • dock 类型创建类似停靠的窗口行为。

      ¥The dock type creates a dock-like window behavior.

    • toolbar 类型创建一个带有工具栏外观的窗口。

      ¥The toolbar type creates a window with a toolbar appearance.

    • splash 类型以特定方式运行。即使窗口主体的 CSS 样式包含 -webkit-app-region,它也是不可拖动的:拖。这种类型通常用于启动画面。

      ¥The splash type behaves in a specific way. It is not draggable, even if the CSS styling of the window's body contains -webkit-app-region: drag. This type is commonly used for splash screens.

    • notification 类型创建一个行为类似于系统通知的窗口。

      ¥The notification type creates a window that behaves like a system notification.

  • 在 macOS 上,可能的类型为 desktoptexturedpanel

    ¥On macOS, possible types are desktop, textured, panel.

    • textured 型增加了金属渐变外观(NSWindowStyleMaskTexturedBackground)。

      ¥The textured type adds metal gradient appearance (NSWindowStyleMaskTexturedBackground).

    • desktop 类型将窗口放置在桌面背景窗口级别 (kCGDesktopWindowLevel - 1)。请注意,桌面窗口不会接收焦点、键盘或鼠标事件,但你可以使用 globalShortcut 来少量接收输入。

      ¥The desktop type places the window at the desktop background window level (kCGDesktopWindowLevel - 1). Note that desktop window will not receive focus, keyboard or mouse events, but you can use globalShortcut to receive input sparingly.

    • panel 类型通过在运行时添加 NSWindowStyleMaskNonactivatingPanel 样式掩码(通常为 NSPanel 保留),使窗口能够浮动在全屏应用的顶部。此外,该窗口将出现在所有空间(桌面)上。

      ¥The panel type enables the window to float on top of full-screened apps by adding the NSWindowStyleMaskNonactivatingPanel style mask,normally reserved for NSPanel, at runtime. Also, the window will appear on all spaces (desktops).

  • 在 Windows 上,可能的类型是 toolbar

    ¥On Windows, possible type is toolbar.

实例事件

¥Instance Events

使用 new BaseWindow 创建的对象会发出以下事件:

¥Objects created with new BaseWindow emit the following events:

注意:某些事件仅在特定操作系统上可用并如此标记。

¥Note: Some events are only available on specific operating systems and are labeled as such.

事件:'close'

¥Event: 'close'

返回:

¥Returns:

  • event 事件

    ¥event Event

当窗口将要关闭时发出。它在 DOM 的 beforeunloadunload 事件之前发出。调用 event.preventDefault() 将取消关闭。

¥Emitted when the window is going to be closed. It's emitted before the beforeunload and unload event of the DOM. Calling event.preventDefault() will cancel the close.

通常你会希望使用 beforeunload 处理程序来决定是否应关闭窗口,重新加载窗口时也会调用该处理程序。在 Electron 中,返回 undefined 以外的任何值都会取消关闭。例如:

¥Usually you would want to use the beforeunload handler to decide whether the window should be closed, which will also be called when the window is reloaded. In Electron, returning any value other than undefined would cancel the close. For example:

window.onbeforeunload = (e) => {
console.log('I do not want to be closed')

// Unlike usual browsers that a message box will be prompted to users, returning
// a non-void value will silently cancel the close.
// It is recommended to use the dialog API to let the user confirm closing the
// application.
e.returnValue = false
}

注意:window.onbeforeunload = handlerwindow.addEventListener('beforeunload', handler) 的行为之间存在细微差别。建议始终显式设置 event.returnValue,而不是仅返回一个值,因为前者在 Electron 中工作得更加一致。

¥Note: There is a subtle difference between the behaviors of window.onbeforeunload = handler and window.addEventListener('beforeunload', handler). It is recommended to always set the event.returnValue explicitly, instead of only returning a value, as the former works more consistently within Electron.

事件:'closed'

¥Event: 'closed'

当窗口关闭时发出。收到此事件后,你应该删除对该窗口的引用并避免再使用它。

¥Emitted when the window is closed. After you have received this event you should remove the reference to the window and avoid using it any more.

事件:'session-end' Windows

¥Event: 'session-end' Windows

当窗口会话由于强制关闭或计算机重新启动或会话注销而即将结束时发出。

¥Emitted when window session is going to end due to force shutdown or machine restart or session log off.

事件:'blur'

¥Event: 'blur'

当窗口失去焦点时发出。

¥Emitted when the window loses focus.

事件:'focus'

¥Event: 'focus'

当窗口获得焦点时发出。

¥Emitted when the window gains focus.

事件:'show'

¥Event: 'show'

显示窗口时发出。

¥Emitted when the window is shown.

事件:'hide'

¥Event: 'hide'

当窗口隐藏时发出。

¥Emitted when the window is hidden.

事件:'maximize'

¥Event: 'maximize'

当窗口最大化时发出。

¥Emitted when window is maximized.

事件:'unmaximize'

¥Event: 'unmaximize'

当窗口从最大化状态退出时发出。

¥Emitted when the window exits from a maximized state.

事件:'minimize'

¥Event: 'minimize'

当窗口最小化时发出。

¥Emitted when the window is minimized.

事件:'restore'

¥Event: 'restore'

当窗口从最小化状态恢复时发出。

¥Emitted when the window is restored from a minimized state.

事件:'will-resize' macOS Windows

¥Event: 'will-resize' macOS Windows

返回:

¥Returns:

  • event 事件

    ¥event Event

  • newBounds 长方形 - 正在调整窗口大小。

    ¥newBounds Rectangle - Size the window is being resized to.

  • details 对象

    ¥details Object

    • edge(string) - 拖动窗口的边缘来调整大小。可以是 bottomleftrighttop-lefttop-rightbottom-leftbottom-right

      ¥edge (string) - The edge of the window being dragged for resizing. Can be bottom, left, right, top-left, top-right, bottom-left or bottom-right.

在调整窗口大小之前发出。调用 event.preventDefault() 将阻止调整窗口大小。

¥Emitted before the window is resized. Calling event.preventDefault() will prevent the window from being resized.

请注意,只有在手动调整窗口大小时才会发出此消息。使用 setBounds/setSize 调整窗口大小不会发出此事件。

¥Note that this is only emitted when the window is being resized manually. Resizing the window with setBounds/setSize will not emit this event.

edge 选项的可能值和行为取决于平台。可能的值为:

¥The possible values and behaviors of the edge option are platform dependent. Possible values are:

  • 在 Windows 上,可能的值为 bottomtopleftrighttop-lefttop-rightbottom-leftbottom-right

    ¥On Windows, possible values are bottom, top, left, right, top-left, top-right, bottom-left, bottom-right.

  • 在 macOS 上,可能的值为 bottomright

    ¥On macOS, possible values are bottom and right.

    • bottom 用于表示垂直调整大小。

      ¥The value bottom is used to denote vertical resizing.

    • right 用于表示水平调整大小。

      ¥The value right is used to denote horizontal resizing.

事件:'resize'

¥Event: 'resize'

调整窗口大小后发出。

¥Emitted after the window has been resized.

事件:'resized' macOS Windows

¥Event: 'resized' macOS Windows

当窗口完成调整大小时发出一次。

¥Emitted once when the window has finished being resized.

这通常在手动调整窗口大小时发出。在 macOS 上,使用 setBounds/setSize 调整窗口大小并将 animate 参数设置为 true 也会在调整大小完成后发出此事件。

¥This is usually emitted when the window has been resized manually. On macOS, resizing the window with setBounds/setSize and setting the animate parameter to true will also emit this event once resizing has finished.

事件:'will-move' macOS Windows

¥Event: 'will-move' macOS Windows

返回:

¥Returns:

  • event 事件

    ¥event Event

  • newBounds 长方形 - 窗口移动到的位置。

    ¥newBounds Rectangle - Location the window is being moved to.

在移动窗口之前发出。在 Windows 上,调用 event.preventDefault() 将阻止窗口移动。

¥Emitted before the window is moved. On Windows, calling event.preventDefault() will prevent the window from being moved.

请注意,只有在手动移动窗口时才会发出此信号。使用 setPosition/setBounds/center 移动窗口不会发出此事件。

¥Note that this is only emitted when the window is being moved manually. Moving the window with setPosition/setBounds/center will not emit this event.

事件:'move'

¥Event: 'move'

当窗口移动到新位置时发出。

¥Emitted when the window is being moved to a new position.

事件:'moved' macOS Windows

¥Event: 'moved' macOS Windows

当窗口移动到新位置时发出一次。

¥Emitted once when the window is moved to a new position.

注意:在 macOS 上,此事件是 move 的别名。

¥Note: On macOS this event is an alias of move.

事件:'enter-full-screen'

¥Event: 'enter-full-screen'

当窗口进入全屏状态时发出。

¥Emitted when the window enters a full-screen state.

事件:'leave-full-screen'

¥Event: 'leave-full-screen'

当窗口离开全屏状态时发出。

¥Emitted when the window leaves a full-screen state.

事件:'always-on-top-changed'

¥Event: 'always-on-top-changed'

返回:

¥Returns:

  • event 事件

    ¥event Event

  • isAlwaysOnTop 布尔值

    ¥isAlwaysOnTop boolean

当窗口被设置或取消设置为始终显示在其他窗口之上时发出。

¥Emitted when the window is set or unset to show always on top of other windows.

事件:'app-command' Windows Linux

¥Event: 'app-command' Windows Linux

返回:

¥Returns:

  • event 事件

    ¥event Event

  • command 字符串

    ¥command string

当调用 应用命令 时发出。这些通常与键盘媒体键或浏览器命令以及 Windows 上某些鼠标中内置的 "后退" 按钮相关。

¥Emitted when an App Command is invoked. These are typically related to keyboard media keys or browser commands, as well as the "Back" button built into some mice on Windows.

命令变为小写,下划线替换为连字符,并且去掉 APPCOMMAND_ 前缀。例如 APPCOMMAND_BROWSER_BACKWARD 作为 browser-backward 发出。

¥Commands are lowercased, underscores are replaced with hyphens, and the APPCOMMAND_ prefix is stripped off. e.g. APPCOMMAND_BROWSER_BACKWARD is emitted as browser-backward.

const { BaseWindow } = require('electron')
const win = new BaseWindow()
win.on('app-command', (e, cmd) => {
// Navigate the window back when the user hits their mouse back button
if (cmd === 'browser-backward') {
// Find the appropriate WebContents to navigate.
}
})

Linux 上明确支持以下应用命令:

¥The following app commands are explicitly supported on Linux:

  • browser-backward

  • browser-forward

事件:'swipe' macOS

¥Event: 'swipe' macOS

返回:

¥Returns:

  • event 事件

    ¥event Event

  • direction 字符串

    ¥direction string

三指滑动时发出。可能的方向是 uprightdownleft

¥Emitted on 3-finger swipe. Possible directions are up, right, down, left.

此事件背后的方法是为了处理旧版 macOS 风格的触控板滑动而构建的,其中屏幕上的内容不会随着滑动而移动。大多数 macOS 触控板不再配置为允许这种滑动,因此为了使其正确发出,System Preferences > Trackpad > More Gestures 中的 '在页面之间滑动' 首选项必须设置为 '用两根或三根手指滑动'。

¥The method underlying this event is built to handle older macOS-style trackpad swiping, where the content on the screen doesn't move with the swipe. Most macOS trackpads are not configured to allow this kind of swiping anymore, so in order for it to emit properly the 'Swipe between pages' preference in System Preferences > Trackpad > More Gestures must be set to 'Swipe with two or three fingers'.

事件:'rotate-gesture' macOS

¥Event: 'rotate-gesture' macOS

返回:

¥Returns:

  • event 事件

    ¥event Event

  • rotation 漂浮

    ¥rotation Float

在触控板旋转手势上发出。持续发出直到旋转手势结束。每次触发的 rotation 值是自上次触发以来旋转的角度(以度为单位)。旋转手势时最后发出的事件的值始终为 0。逆时针旋转值为正,顺时针旋转值为负。

¥Emitted on trackpad rotation gesture. Continually emitted until rotation gesture is ended. The rotation value on each emission is the angle in degrees rotated since the last emission. The last emitted event upon a rotation gesture will always be of value 0. Counter-clockwise rotation values are positive, while clockwise ones are negative.

事件:'sheet-begin' macOS

¥Event: 'sheet-begin' macOS

当窗口打开一张工作表时发出。

¥Emitted when the window opens a sheet.

事件:'sheet-end' macOS

¥Event: 'sheet-end' macOS

当窗口关闭工作表时发出。

¥Emitted when the window has closed a sheet.

事件:'new-window-for-tab' macOS

¥Event: 'new-window-for-tab' macOS

单击原生新选项卡按钮时发出。

¥Emitted when the native new tab button is clicked.

事件:'system-context-menu' Windows

¥Event: 'system-context-menu' Windows

返回:

¥Returns:

  • event 事件

    ¥event Event

  • point 观点 - 触发上下文菜单的屏幕坐标

    ¥point Point - The screen coordinates the context menu was triggered at

当在窗口上触发系统上下文菜单时发出,通常仅当用户右键单击窗口的非客户区域时才会触发。这是窗口标题栏或你在无框窗口中声明为 -webkit-app-region: drag 的任何区域。

¥Emitted when the system context menu is triggered on the window, this is normally only triggered when the user right clicks on the non-client area of your window. This is the window titlebar or any area you have declared as -webkit-app-region: drag in a frameless window.

调用 event.preventDefault() 将阻止显示菜单。

¥Calling event.preventDefault() will prevent the menu from being displayed.

静态方法

¥Static Methods

BaseWindow 类具有以下静态方法:

¥The BaseWindow class has the following static methods:

BaseWindow.getAllWindows()

返回 BaseWindow[] - 所有打开的浏览器窗口的数组。

¥Returns BaseWindow[] - An array of all opened browser windows.

BaseWindow.getFocusedWindow()

返回 BaseWindow | null - 该应用中聚焦的窗口,否则返回 null

¥Returns BaseWindow | null - The window that is focused in this application, otherwise returns null.

BaseWindow.fromId(id)

  • id 整数

    ¥id Integer

返回 BaseWindow | null - 具有给定 id 的窗口。

¥Returns BaseWindow | null - The window with the given id.

实例属性

¥Instance Properties

使用 new BaseWindow 创建的对象具有以下属性:

¥Objects created with new BaseWindow have the following properties:

const { BaseWindow } = require('electron')
// In this example `win` is our instance
const win = new BaseWindow({ width: 800, height: 600 })

win.id 只读

¥win.id Readonly

代表窗口的唯一 ID 的 Integer 属性。每个 ID 在整个 Electron 应用的所有 BaseWindow 实例中都是唯一的。

¥A Integer property representing the unique ID of the window. Each ID is unique among all BaseWindow instances of the entire Electron application.

win.contentView

窗口内容视图的 View 属性。

¥A View property for the content view of the window.

win.tabbingIdentifier macOS 只读

¥win.tabbingIdentifier macOS Readonly

string(可选)属性,等于传递给 BrowserWindow 构造函数的 tabbingIdentifierundefined(如果未设置)。

¥A string (optional) property that is equal to the tabbingIdentifier passed to the BrowserWindow constructor or undefined if none was set.

win.autoHideMenuBar

一个 boolean 属性,用于确定窗口菜单栏是否应自动隐藏。设置后,只有当用户按单个 Alt 键时才会显示菜单栏。

¥A boolean property that determines whether the window menu bar should hide itself automatically. Once set, the menu bar will only show when users press the single Alt key.

如果菜单栏已经可见,将此属性设置为 true 不会立即隐藏它。

¥If the menu bar is already visible, setting this property to true won't hide it immediately.

win.simpleFullScreen

一个 boolean 属性,用于确定窗口是否处于简单(Lion 之前)全屏模式。

¥A boolean property that determines whether the window is in simple (pre-Lion) fullscreen mode.

win.fullScreen

确定窗口是否处于全屏模式的 boolean 属性。

¥A boolean property that determines whether the window is in fullscreen mode.

win.focusable Windows macOS

确定窗口是否可聚焦的 boolean 属性。

¥A boolean property that determines whether the window is focusable.

win.visibleOnAllWorkspaces macOS Linux

boolean 属性确定窗口是否在所有工作区上可见。

¥A boolean property that determines whether the window is visible on all workspaces.

注意:在 Windows 上始终返回 false。

¥Note: Always returns false on Windows.

win.shadow

确定窗口是否有阴影的 boolean 属性。

¥A boolean property that determines whether the window has a shadow.

win.menuBarVisible Windows Linux

确定菜单栏是否可见的 boolean 属性。

¥A boolean property that determines whether the menu bar should be visible.

注意:如果菜单栏自动隐藏,用户仍然可以通过按单个 Alt 键调出菜单栏。

¥Note: If the menu bar is auto-hide, users can still bring up the menu bar by pressing the single Alt key.

win.kiosk

确定窗口是否处于 kiosk 模式的 boolean 属性。

¥A boolean property that determines whether the window is in kiosk mode.

win.documentEdited macOS

一个 boolean 属性,指定窗口的文档是否已被编辑。

¥A boolean property that specifies whether the window’s document has been edited.

当设置为 true 时,标题栏中的图标将变为灰色。

¥The icon in title bar will become gray when set to true.

win.representedFilename macOS

string 属性确定窗口所代表的文件的路径名,并且文件的图标将显示在窗口的标题栏中。

¥A string property that determines the pathname of the file the window represents, and the icon of the file will show in window's title bar.

win.title

确定原生窗口标题的 string 属性。

¥A string property that determines the title of the native window.

注意:网页的标题可以与原生窗口的标题不同。

¥Note: The title of the web page can be different from the title of the native window.

win.minimizable macOS Windows

boolean 属性确定用户是否可以手动最小化窗口。

¥A boolean property that determines whether the window can be manually minimized by user.

在 Linux 上,虽然 getter 返回 true,但 setter 是无操作的。

¥On Linux the setter is a no-op, although the getter returns true.

win.maximizable macOS Windows

一个 boolean 属性,用于确定用户是否可以手动最大化窗口。

¥A boolean property that determines whether the window can be manually maximized by user.

在 Linux 上,虽然 getter 返回 true,但 setter 是无操作的。

¥On Linux the setter is a no-op, although the getter returns true.

win.fullScreenable

boolean 属性,用于确定最大化/缩放窗口按钮是否切换全屏模式或最大化窗口。

¥A boolean property that determines whether the maximize/zoom window button toggles fullscreen mode or maximizes the window.

win.resizable

boolean 属性确定用户是否可以手动调整窗口大小。

¥A boolean property that determines whether the window can be manually resized by user.

win.closable macOS Windows

boolean 属性,确定用户是否可以手动关闭窗口。

¥A boolean property that determines whether the window can be manually closed by user.

在 Linux 上,虽然 getter 返回 true,但 setter 是无操作的。

¥On Linux the setter is a no-op, although the getter returns true.

win.movable macOS Windows

boolean 属性确定用户是否可以移动窗口。

¥A boolean property that determines Whether the window can be moved by user.

在 Linux 上,虽然 getter 返回 true,但 setter 是无操作的。

¥On Linux the setter is a no-op, although the getter returns true.

win.excludedFromShownWindowsMenu macOS

boolean 属性,用于确定窗口是否从应用的 Windows 菜单中排除。默认为 false

¥A boolean property that determines whether the window is excluded from the application’s Windows menu. false by default.

const { Menu, BaseWindow } = require('electron')
const win = new BaseWindow({ height: 600, width: 600 })

const template = [
{
role: 'windowmenu'
}
]

win.excludedFromShownWindowsMenu = true

const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)

win.accessibleTitle

string 属性,定义仅提供给屏幕阅读器等辅助工具的替代标题。用户不能直接看到该字符串。

¥A string property that defines an alternative title provided only to accessibility tools such as screen readers. This string is not directly visible to users.

实例方法

¥Instance Methods

使用 new BaseWindow 创建的对象具有以下实例方法:

¥Objects created with new BaseWindow have the following instance methods:

注意:有些方法仅在特定操作系统上可用,并如此标记。

¥Note: Some methods are only available on specific operating systems and are labeled as such.

win.setContentView(view)

设置窗口的内容视图。

¥Sets the content view of the window.

win.getContentView()

返回 View - 窗口的内容视图。

¥Returns View - The content view of the window.

win.destroy()

强制关闭窗口,网页不会发出 unloadbeforeunload 事件,该窗口也不会发出 close 事件,但保证会发出 closed 事件。

¥Force closing the window, the unload and beforeunload event won't be emitted for the web page, and close event will also not be emitted for this window, but it guarantees the closed event will be emitted.

win.close()

尝试关闭窗口。这与用户手动单击窗口的关闭按钮具有相同的效果。但网页可能会取消关闭。参见 关闭事件

¥Try to close the window. This has the same effect as a user manually clicking the close button of the window. The web page may cancel the close though. See the close event.

win.focus()

焦点集中在窗户上。

¥Focuses on the window.

win.blur()

从窗口中移除焦点。

¥Removes focus from the window.

win.isFocused()

返回 boolean - 窗口是否获得焦点。

¥Returns boolean - Whether the window is focused.

win.isDestroyed()

返回 boolean - 窗户是否被破坏。

¥Returns boolean - Whether the window is destroyed.

win.show()

显示窗口并给予窗口焦点。

¥Shows and gives focus to the window.

win.showInactive()

显示窗口但不关注它。

¥Shows the window but doesn't focus on it.

win.hide()

隐藏窗户。

¥Hides the window.

win.isVisible()

返回 boolean - 用户在应用的前台是否可以看到该窗口。

¥Returns boolean - Whether the window is visible to the user in the foreground of the app.

win.isModal()

返回 boolean - 当前窗口是否为模态窗口。

¥Returns boolean - Whether current window is a modal window.

win.maximize()

最大化窗口。如果窗口尚未显示,这也将显示(但不聚焦)窗口。

¥Maximizes the window. This will also show (but not focus) the window if it isn't being displayed already.

win.unmaximize()

取消最大化窗口。

¥Unmaximizes the window.

win.isMaximized()

返回 boolean - 窗口是否最大化。

¥Returns boolean - Whether the window is maximized.

win.minimize()

最小化窗口。在某些平台上,最小化的窗口将显示在 Dock 中。

¥Minimizes the window. On some platforms the minimized window will be shown in the Dock.

win.restore()

将窗口从最小化状态恢复到之前的状态。

¥Restores the window from minimized state to its previous state.

win.isMinimized()

返回 boolean - 窗口是否最小化。

¥Returns boolean - Whether the window is minimized.

win.setFullScreen(flag)

  • flag 布尔值

    ¥flag boolean

设置窗口是否应处于全屏模式。

¥Sets whether the window should be in fullscreen mode.

注意:在 macOS 上,全屏转换是异步发生的。如果进一步的操作取决于全屏状态,请使用 'enter-full-screen''leave-full-screen' 事件。

¥Note: On macOS, fullscreen transitions take place asynchronously. If further actions depend on the fullscreen state, use the 'enter-full-screen' or 'leave-full-screen' events.

win.isFullScreen()

返回 boolean - 窗口是否处于全屏模式。

¥Returns boolean - Whether the window is in fullscreen mode.

win.setSimpleFullScreen(flag) macOS

  • flag 布尔值

    ¥flag boolean

进入或离开简单全屏模式。

¥Enters or leaves simple fullscreen mode.

简单全屏模式模拟 Lion (10.7) 之前的 macOS 版本中的原生全屏行为。

¥Simple fullscreen mode emulates the native fullscreen behavior found in versions of macOS prior to Lion (10.7).

win.isSimpleFullScreen() macOS

返回 boolean - 窗口是否处于简单(Lion 之前)全屏模式。

¥Returns boolean - Whether the window is in simple (pre-Lion) fullscreen mode.

win.isNormal()

返回 boolean - 窗口是否处于正常状态(未最大化、未最小化、未全屏模式)。

¥Returns boolean - Whether the window is in normal state (not maximized, not minimized, not in fullscreen mode).

win.setAspectRatio(aspectRatio[, extraSize])

  • aspectRatio 漂浮 - 内容视图某些部分保持的宽高比。

    ¥aspectRatio Float - The aspect ratio to maintain for some portion of the content view.

  • extraSize 尺寸(可选)macOS - 在保持纵横比的同时不包括额外的尺寸。

    ¥extraSize Size (optional) macOS - The extra size not to be included while maintaining the aspect ratio.

这将使窗口保持纵横比。额外的尺寸允许开发者拥有以像素指定的空间,不包括在长宽比计算中。该 API 已经考虑了窗口大小与其内容大小之间的差异。

¥This will make a window maintain an aspect ratio. The extra size allows a developer to have space, specified in pixels, not included within the aspect ratio calculations. This API already takes into account the difference between a window's size and its content size.

考虑一个带有高清视频播放器和相关控件的普通窗口。也许左边缘有 15 个像素的控件,右边缘有 25 个像素的控件,播放器下方有 50 个像素的控件。为了在播放器本身内保持 16:9 的宽高比(HD @1920x1080 的标准宽高比),我们将使用 16/9 和 { 宽度参数调用此函数:40, height:50 }.第二个参数并不关心额外的宽度和高度在内容视图中的位置 - 只关心它们存在。对整个内容视图中的所有额外宽度和高度区域进行求和。

¥Consider a normal window with an HD video player and associated controls. Perhaps there are 15 pixels of controls on the left edge, 25 pixels of controls on the right edge and 50 pixels of controls below the player. In order to maintain a 16:9 aspect ratio (standard aspect ratio for HD @1920x1080) within the player itself we would call this function with arguments of 16/9 and { width: 40, height: 50 }. The second argument doesn't care where the extra width and height are within the content view--only that they exist. Sum any extra width and height areas you have within the overall content view.

当使用 win.setSize 等 API 以编程方式调整窗口大小时,不会考虑宽高比。

¥The aspect ratio is not respected when window is resized programmatically with APIs like win.setSize.

要重置宽高比,请传递 0 作为 aspectRatio 值:win.setAspectRatio(0)

¥To reset an aspect ratio, pass 0 as the aspectRatio value: win.setAspectRatio(0).

win.setBackgroundColor(backgroundColor)

  • backgroundColor 字符串 - 颜色采用 Hex、RGB、RGBA、HSL、HSLA 或命名的 CSS 颜色格式。对于十六进制类型,Alpha 通道是可选的。

    ¥backgroundColor string - Color in Hex, RGB, RGBA, HSL, HSLA or named CSS color format. The alpha channel is optional for the hex type.

有效 backgroundColor 值的示例:

¥Examples of valid backgroundColor values:

  • 十六进制

    ¥Hex

    • #fff (shorthand RGB)

    • #ffff (shorthand ARGB)

    • #ffffff (RGB)

    • #ffffffff (ARGB)

  • RGB

    • rgb\(([\d]+),\s*([\d]+),\s*([\d]+)\)

      • 例如 RGB(255, 255, 255)

        ¥e.g. rgb(255, 255, 255)

  • RGBA

    • rgba\(([\d]+),\s*([\d]+),\s*([\d]+),\s*([\d.]+)\)

      • 例如 RGBA(255, 255, 255, 1.0)

        ¥e.g. rgba(255, 255, 255, 1.0)

  • HSL

    • hsl\((-?[\d.]+),\s*([\d.]+)%,\s*([\d.]+)%\)

      • 例如 HSL(200, 20%, 50%)

        ¥e.g. hsl(200, 20%, 50%)

  • HSLA

    • hsla\((-?[\d.]+),\s*([\d.]+)%,\s*([\d.]+)%,\s*([\d.]+)\)

      • 例如 高斯拉(200, 20%, 50%, 0.5)

        ¥e.g. hsla(200, 20%, 50%, 0.5)

  • 颜色名称

    ¥Color name

    • 选项列于 SkParseColor.cpp

      ¥Options are listed in SkParseColor.cpp

    • 与 CSS Color Module Level 3 关键字类似,但区分大小写。

      ¥Similar to CSS Color Module Level 3 keywords, but case-sensitive.

      • 例如 bluevioletred

        ¥e.g. blueviolet or red

设置窗口的背景颜色。参见 设置 backgroundColor

¥Sets the background color of the window. See Setting backgroundColor.

win.previewFile(path[, displayName]) macOS

  • path 字符串 - 要使用 QuickLook 预览的文件的绝对路径。这很重要,因为“快速查看”使用路径上的文件名和文件扩展名来确定要打开的文件的内容类型。

    ¥path string - The absolute path to the file to preview with QuickLook. This is important as Quick Look uses the file name and file extension on the path to determine the content type of the file to open.

  • displayName 字符串(可选) - 要在“快速查看”模式视图上显示的文件的名称。这纯粹是视觉上的,不会影响文件的内容类型。默认为 path

    ¥displayName string (optional) - The name of the file to display on the Quick Look modal view. This is purely visual and does not affect the content type of the file. Defaults to path.

使用 快速查看 预览给定路径中的文件。

¥Uses Quick Look to preview a file at a given path.

win.closeFilePreview() macOS

关闭当前打开的 快速查看 面板。

¥Closes the currently open Quick Look panel.

win.setBounds(bounds[, animate])

  • bounds 部分<长方形>

    ¥bounds Partial<Rectangle>

  • animate 布尔值(可选)macOS

    ¥animate boolean (optional) macOS

调整窗口大小并将窗口移动到提供的边界。任何未提供的属性将默认为其当前值。

¥Resizes and moves the window to the supplied bounds. Any properties that are not supplied will default to their current values.

const { BaseWindow } = require('electron')
const win = new BaseWindow()

// set all bounds properties
win.setBounds({ x: 440, y: 225, width: 800, height: 600 })

// set a single bounds property
win.setBounds({ width: 100 })

// { x: 440, y: 225, width: 100, height: 600 }
console.log(win.getBounds())

注意:在 macOS 上,y 坐标值不能小于 Tray 高度。托盘高度随着时间的推移而变化,并且取决于操作系统,但在 20-40px 之间。传递低于托盘高度的值将导致窗口与托盘齐平。

¥Note: On macOS, the y-coordinate value cannot be smaller than the Tray height. The tray height has changed over time and depends on the operating system, but is between 20-40px. Passing a value lower than the tray height will result in a window that is flush to the tray.

win.getBounds()

返回 Rectangle - 窗口的 boundsObject

¥Returns Rectangle - The bounds of the window as Object.

注意:在 macOS 上,返回的 y 坐标值至少为 Tray 高度。例如,调用托盘高度为 38 的 win.setBounds({ x: 25, y: 20, width: 800, height: 600 }) 意味着 win.getBounds() 将返回 { x: 25, y: 38, width: 800, height: 600 }

¥Note: On macOS, the y-coordinate value returned will be at minimum the Tray height. For example, calling win.setBounds({ x: 25, y: 20, width: 800, height: 600 }) with a tray height of 38 means that win.getBounds() will return { x: 25, y: 38, width: 800, height: 600 }.

win.getBackgroundColor()

返回 string - 获取十六进制 (#RRGGBB) 格式的窗口背景颜色。

¥Returns string - Gets the background color of the window in Hex (#RRGGBB) format.

参见 设置 backgroundColor

¥See Setting backgroundColor.

注意:Alpha 值不会与红色、绿色和蓝色值一起返回。

¥Note: The alpha value is not returned alongside the red, green, and blue values.

win.setContentBounds(bounds[, animate])

  • bounds 长方形

    ¥bounds Rectangle

  • animate 布尔值(可选)macOS

    ¥animate boolean (optional) macOS

调整窗口的客户区域(例如网页)的大小并将其移动到提供的边界。

¥Resizes and moves the window's client area (e.g. the web page) to the supplied bounds.

win.getContentBounds()

返回 Rectangle - 窗口的客户区的 boundsObject

¥Returns Rectangle - The bounds of the window's client area as Object.

win.getNormalBounds()

返回 Rectangle - 包含正常状态的窗口边界

¥Returns Rectangle - Contains the window bounds of the normal state

注意:无论窗口的当前状态如何:最大化、最小化或全屏,此函数始终返回正常状态下窗口的位置和大小。在正常状态下,getBounds 和 getNormalBounds 返回相同的 Rectangle

¥Note: whatever the current state of the window : maximized, minimized or in fullscreen, this function always returns the position and size of the window in normal state. In normal state, getBounds and getNormalBounds returns the same Rectangle.

win.setEnabled(enable)

  • enable 布尔值

    ¥enable boolean

禁用或启用该窗口。

¥Disable or enable the window.

win.isEnabled()

返回 boolean - 窗口是否启用。

¥Returns boolean - whether the window is enabled.

win.setSize(width, height[, animate])

  • width 整数

    ¥width Integer

  • height 整数

    ¥height Integer

  • animate 布尔值(可选)macOS

    ¥animate boolean (optional) macOS

将窗口大小调整为 widthheight。如果 widthheight 低于任何设置的最小尺寸限制,窗口将捕捉到其最小尺寸。

¥Resizes the window to width and height. If width or height are below any set minimum size constraints the window will snap to its minimum size.

win.getSize()

返回 Integer[] - 包含窗口的宽度和高度。

¥Returns Integer[] - Contains the window's width and height.

win.setContentSize(width, height[, animate])

  • width 整数

    ¥width Integer

  • height 整数

    ¥height Integer

  • animate 布尔值(可选)macOS

    ¥animate boolean (optional) macOS

将窗口客户区(例如网页)的大小调整为 widthheight

¥Resizes the window's client area (e.g. the web page) to width and height.

win.getContentSize()

返回 Integer[] - 包含窗口客户区的宽度和高度。

¥Returns Integer[] - Contains the window's client area's width and height.

win.setMinimumSize(width, height)

  • width 整数

    ¥width Integer

  • height 整数

    ¥height Integer

将窗口的最小尺寸设置为 widthheight

¥Sets the minimum size of window to width and height.

win.getMinimumSize()

返回 Integer[] - 包含窗口的最小宽度和高度。

¥Returns Integer[] - Contains the window's minimum width and height.

win.setMaximumSize(width, height)

  • width 整数

    ¥width Integer

  • height 整数

    ¥height Integer

将窗口的最大尺寸设置为 widthheight

¥Sets the maximum size of window to width and height.

win.getMaximumSize()

返回 Integer[] - 包含窗口的最大宽度和高度。

¥Returns Integer[] - Contains the window's maximum width and height.

win.setResizable(resizable)

  • resizable 布尔值

    ¥resizable boolean

设置用户是否可以手动调整窗口大小。

¥Sets whether the window can be manually resized by the user.

win.isResizable()

返回 boolean - 用户是否可以手动调整窗口大小。

¥Returns boolean - Whether the window can be manually resized by the user.

win.setMovable(movable) macOS Windows

  • movable 布尔值

    ¥movable boolean

设置用户是否可以移动窗口。在 Linux 上什么都不做。

¥Sets whether the window can be moved by user. On Linux does nothing.

win.isMovable() macOS Windows

返回 boolean - 用户是否可以移动窗口。

¥Returns boolean - Whether the window can be moved by user.

在 Linux 上始终返回 true

¥On Linux always returns true.

win.setMinimizable(minimizable) macOS Windows

  • minimizable 布尔值

    ¥minimizable boolean

设置用户是否可以手动最小化窗口。在 Linux 上什么都不做。

¥Sets whether the window can be manually minimized by user. On Linux does nothing.

win.isMinimizable() macOS Windows

返回 boolean - 用户是否可以手动最小化窗口。

¥Returns boolean - Whether the window can be manually minimized by the user.

在 Linux 上始终返回 true

¥On Linux always returns true.

win.setMaximizable(maximizable) macOS Windows

  • maximizable 布尔值

    ¥maximizable boolean

设置用户是否可以手动最大化窗口。在 Linux 上什么都不做。

¥Sets whether the window can be manually maximized by user. On Linux does nothing.

win.isMaximizable() macOS Windows

返回 boolean - 用户是否可以手动最大化窗口。

¥Returns boolean - Whether the window can be manually maximized by user.

在 Linux 上始终返回 true

¥On Linux always returns true.

win.setFullScreenable(fullscreenable)

  • fullscreenable 布尔值

    ¥fullscreenable boolean

设置最大化/缩放窗口按钮是否切换全屏模式或最大化窗口。

¥Sets whether the maximize/zoom window button toggles fullscreen mode or maximizes the window.

win.isFullScreenable()

返回 boolean - 最大化/缩放窗口按钮是否切换全屏模式或最大化窗口。

¥Returns boolean - Whether the maximize/zoom window button toggles fullscreen mode or maximizes the window.

win.setClosable(closable) macOS Windows

  • closable 布尔值

    ¥closable boolean

设置用户是否可以手动关闭窗口。在 Linux 上什么都不做。

¥Sets whether the window can be manually closed by user. On Linux does nothing.

win.isClosable() macOS Windows

返回 boolean - 窗口是否可以由用户手动关闭。

¥Returns boolean - Whether the window can be manually closed by user.

在 Linux 上始终返回 true

¥On Linux always returns true.

win.setHiddenInMissionControl(hidden) macOS

  • hidden 布尔值

    ¥hidden boolean

设置当用户切换到任务控制时是否隐藏窗口。

¥Sets whether the window will be hidden when the user toggles into mission control.

win.isHiddenInMissionControl() macOS

返回 boolean - 当用户切换到任务控制时窗口是否隐藏。

¥Returns boolean - Whether the window will be hidden when the user toggles into mission control.

win.setAlwaysOnTop(flag[, level][, relativeLevel])

  • flag 布尔值

    ¥flag boolean

  • level 字符串(可选) macOS Windows - 值包括 normalfloatingtorn-off-menumodal-panelmain-menustatuspop-up-menuscreen-saverdock(已弃用)。当 flag 为 true 时,默认值为 floating。当标志为假时,level 重置为 normal。请注意,从 floatingstatus,窗口位于 macOS 上的 Dock 下方和 Windows 上的任务栏下方。从 pop-up-menu 到更高版本,它显示在 macOS 上的 Dock 上方和 Windows 上的任务栏上方。有关详细信息,请参阅 macOS 文档

    ¥level string (optional) macOS Windows - Values include normal, floating, torn-off-menu, modal-panel, main-menu, status, pop-up-menu, screen-saver, and dock (Deprecated). The default is floating when flag is true. The level is reset to normal when the flag is false. Note that from floating to status included, the window is placed below the Dock on macOS and below the taskbar on Windows. From pop-up-menu to a higher it is shown above the Dock on macOS and above the taskbar on Windows. See the macOS docs for more details.

  • relativeLevel 整数(可选)macOS - 相对于给定的 level 设置此窗口的更高层数。默认为 0。请注意,Apple 不鼓励在 screen-saver 之上设置高于 1 的级别。

    ¥relativeLevel Integer (optional) macOS - The number of layers higher to set this window relative to the given level. The default is 0. Note that Apple discourages setting levels higher than 1 above screen-saver.

设置窗口是否应始终显示在其他窗口之上。设置完毕后,该窗口仍然是一个普通窗口,而不是一个无法获得焦点的工具箱窗口。

¥Sets whether the window should show always on top of other windows. After setting this, the window is still a normal window, not a toolbox window which can not be focused on.

win.isAlwaysOnTop()

返回 boolean - 窗口是否始终位于其他窗口之上。

¥Returns boolean - Whether the window is always on top of other windows.

win.moveAbove(mediaSourceId)

  • mediaSourceId 字符串 - 窗口 ID,格式为 DesktopCapturerSource 的 id。例如 "窗口:1869:0"。

    ¥mediaSourceId string - Window id in the format of DesktopCapturerSource's id. For example "window:1869:0".

按 z 顺序将窗口移动到源窗口上方。如果 mediaSourceId 不是窗口类型或者窗口不存在,则此方法将引发错误。

¥Moves window above the source window in the sense of z-order. If the mediaSourceId is not of type window or if the window does not exist then this method throws an error.

win.moveTop()

无论焦点如何,将窗口移动到顶部(z 顺序)

¥Moves window to top(z-order) regardless of focus

win.center()

将窗口移动到屏幕中央。

¥Moves window to the center of the screen.

win.setPosition(x, y[, animate])

  • x 整数

    ¥x Integer

  • y 整数

    ¥y Integer

  • animate 布尔值(可选)macOS

    ¥animate boolean (optional) macOS

将窗口移动到 xy

¥Moves window to x and y.

win.getPosition()

返回 Integer[] - 包含窗口的当前位置。

¥Returns Integer[] - Contains the window's current position.

win.setTitle(title)

  • title 字符串

    ¥title string

将原生窗口的标题更改为 title

¥Changes the title of native window to title.

win.getTitle()

返回 string - 原生窗口的标题。

¥Returns string - The title of the native window.

注意:网页的标题可以与原生窗口的标题不同。

¥Note: The title of the web page can be different from the title of the native window.

win.setSheetOffset(offsetY[, offsetX]) macOS

  • offsetY 漂浮

    ¥offsetY Float

  • offsetX 浮动(可选)

    ¥offsetX Float (optional)

更改 macOS 上工作表的附着点。默认情况下,工作表附加在窗口框架的正下方,但你可能希望将它们显示在 HTML 渲染的工具栏下方。例如:

¥Changes the attachment point for sheets on macOS. By default, sheets are attached just below the window frame, but you may want to display them beneath a HTML-rendered toolbar. For example:

const { BaseWindow } = require('electron')
const win = new BaseWindow()

const toolbarRect = document.getElementById('toolbar').getBoundingClientRect()
win.setSheetOffset(toolbarRect.height)

win.flashFrame(flag)

History
  • flag 布尔值

    ¥flag boolean

开始或停止闪烁窗口以吸引用户的注意力。

¥Starts or stops flashing the window to attract user's attention.

win.setSkipTaskbar(skip) macOS Windows

  • skip 布尔值

    ¥skip boolean

使窗口不显示在任务栏中。

¥Makes the window not show in the taskbar.

win.setKiosk(flag)

  • flag 布尔值

    ¥flag boolean

进入或离开信息亭模式。

¥Enters or leaves kiosk mode.

win.isKiosk()

返回 boolean - 窗口是否处于 kiosk 模式。

¥Returns boolean - Whether the window is in kiosk mode.

win.isTabletMode() Windows

返回 boolean - 窗口是否处于 Windows 10 平板电脑模式。

¥Returns boolean - Whether the window is in Windows 10 tablet mode.

由于 Windows 10 用户可以 将他们的电脑用作平板电脑,在此模式下应用可以选择优化平板电脑的 UI,例如放大标题栏和隐藏标题栏按钮。

¥Since Windows 10 users can use their PC as tablet, under this mode apps can choose to optimize their UI for tablets, such as enlarging the titlebar and hiding titlebar buttons.

该 API 返回窗口是否处于平板电脑模式,resize 事件可用于监听平板电脑模式的变化。

¥This API returns whether the window is in tablet mode, and the resize event can be be used to listen to changes to tablet mode.

win.getMediaSourceId()

返回 string - 窗口 ID,格式为 DesktopCapturerSource 的 id。例如 "窗口:1324:0"。

¥Returns string - Window id in the format of DesktopCapturerSource's id. For example "window:1324:0".

更准确地说,格式是 window:id:other_id,其中 id 在 Windows 上是 HWND,在 macOS 上是 CGWindowID (uint64_t),在 Linux 上是 Window (unsigned long)。other_id 用于标识 Web 内容(选项卡),因此位于同一顶层窗口内。

¥More precisely the format is window:id:other_id where id is HWND on Windows, CGWindowID (uint64_t) on macOS and Window (unsigned long) on Linux. other_id is used to identify web contents (tabs) so within the same top level window.

win.getNativeWindowHandle()

返回 Buffer - 窗口的特定于平台的句柄。

¥Returns Buffer - The platform-specific handle of the window.

句柄的原生类型在 Windows 上为 HWND,在 macOS 上为 NSView*,在 Linux 上为 Window (unsigned long)。

¥The native type of the handle is HWND on Windows, NSView* on macOS, and Window (unsigned long) on Linux.

win.hookWindowMessage(message, callback) Windows

  • message 整数

    ¥message Integer

  • callback 函数

    ¥callback Function

    • wParam 缓冲 - 提供给 WndProc 的 wParam

      ¥wParam Buffer - The wParam provided to the WndProc

    • lParam 缓冲 - 提供给 WndProc 的 lParam

      ¥lParam Buffer - The lParam provided to the WndProc

钩子 Windows 消息。当 WndProc 中收到消息时,调用 callback

¥Hooks a windows message. The callback is called when the message is received in the WndProc.

win.isWindowMessageHooked(message) Windows

  • message 整数

    ¥message Integer

返回 boolean - truefalse 取决于消息是否被钩子。

¥Returns boolean - true or false depending on whether the message is hooked.

win.unhookWindowMessage(message) Windows

  • message 整数

    ¥message Integer

取消窗口消息。

¥Unhook the window message.

win.unhookAllWindowMessages() Windows

取消所有窗口消息。

¥Unhooks all of the window messages.

win.setRepresentedFilename(filename) macOS

  • filename 字符串

    ¥filename string

设置窗口代表的文件的路径名,文件的图标将显示在窗口的标题栏中。

¥Sets the pathname of the file the window represents, and the icon of the file will show in window's title bar.

win.getRepresentedFilename() macOS

返回 string - 窗口代表的文件的路径名。

¥Returns string - The pathname of the file the window represents.

win.setDocumentEdited(edited) macOS

  • edited 布尔值

    ¥edited boolean

指定窗口的文档是否已编辑,设置为 true 时标题栏图标将变为灰色。

¥Specifies whether the window’s document has been edited, and the icon in title bar will become gray when set to true.

win.isDocumentEdited() macOS

返回 boolean - 窗口的文档是否已被编辑。

¥Returns boolean - Whether the window's document has been edited.

win.setMenu(menu) Linux Windows

  • menu Menu | null

menu 设置为窗口的菜单栏。

¥Sets the menu as the window's menu bar.

win.removeMenu() Linux Windows

删除窗口的菜单栏。

¥Remove the window's menu bar.

win.setProgressBar(progress[, options])

  • progress 双倍的

    ¥progress Double

  • options 对象(可选)

    ¥options Object (optional)

    • mode 字符串 Windows - 进度条模式。可以是 nonenormalindeterminateerrorpaused

      ¥mode string Windows - Mode for the progress bar. Can be none, normal, indeterminate, error or paused.

设置进度条中的进度值。有效范围为 [0, 1.0]。

¥Sets progress value in progress bar. Valid range is [0, 1.0].

当进度 < 0 时移除进度条;当进度 > 1 时更改为不确定模式。

¥Remove progress bar when progress < 0; Change to indeterminate mode when progress > 1.

在 Linux 平台上,仅支持 Unity 桌面环境,需要将 *.desktop 文件名指定到 package.json 中的 desktopName 字段。默认情况下,它将采用 {app.name}.desktop

¥On Linux platform, only supports Unity desktop environment, you need to specify the *.desktop file name to desktopName field in package.json. By default, it will assume {app.name}.desktop.

在 Windows 上,可以传递模式。接受的值为 nonenormalindeterminateerrorpaused。如果你在未设置模式的情况下调用 setProgressBar(但其值在有效范围内),则将假定为 normal

¥On Windows, a mode can be passed. Accepted values are none, normal, indeterminate, error, and paused. If you call setProgressBar without a mode set (but with a value within the valid range), normal will be assumed.

win.setOverlayIcon(overlay, description) Windows

  • overlay NativeImage | 无效的 - 显示在任务栏图标右下角的图标。如果该参数为 null,则清除叠加

    ¥overlay NativeImage | null - the icon to display on the bottom right corner of the taskbar icon. If this parameter is null, the overlay is cleared

  • description 字符串 - 将提供给辅助功能屏幕阅读器的描述

    ¥description string - a description that will be provided to Accessibility screen readers

在当前任务栏图标上设置 16 x 16 像素的覆盖层,通常用于传达某种应用状态或被动通知用户。

¥Sets a 16 x 16 pixel overlay onto the current taskbar icon, usually used to convey some sort of application status or to passively notify the user.

win.invalidateShadow() macOS

使窗口阴影无效,以便根据当前窗口形状重新计算它。

¥Invalidates the window shadow so that it is recomputed based on the current window shape.

透明的 BaseWindow 有时会在 macOS 上留下视觉伪影。例如,在执行动画时,可以使用此方法来清除这些伪影。

¥BaseWindows that are transparent can sometimes leave behind visual artifacts on macOS. This method can be used to clear these artifacts when, for example, performing an animation.

win.setHasShadow(hasShadow)

  • hasShadow 布尔值

    ¥hasShadow boolean

设置窗口是否应该有阴影。

¥Sets whether the window should have a shadow.

win.hasShadow()

返回 boolean - 窗户是否有阴影。

¥Returns boolean - Whether the window has a shadow.

win.setOpacity(opacity) Windows macOS

  • opacity 数字 - 介于 0.0(完全透明)和 1.0(完全不透明)之间

    ¥opacity number - between 0.0 (fully transparent) and 1.0 (fully opaque)

设置窗口的不透明度。在 Linux 上,不执行任何操作。超出范围的数值被限制在 [0, 1] 范围内。

¥Sets the opacity of the window. On Linux, does nothing. Out of bound number values are clamped to the [0, 1] range.

win.getOpacity()

返回 number - 介于 0.0(完全透明)和 1.0(完全不透明)之间。在 Linux 上,始终返回 1。

¥Returns number - between 0.0 (fully transparent) and 1.0 (fully opaque). On Linux, always returns 1.

win.setShape(rects) Windows Linux 实验

¥win.setShape(rects) Windows Linux Experimental

  • rects Rectangle[] - 在窗口上设置形状。传递空列表会将窗口恢复为矩形。

    ¥rects Rectangle[] - Sets a shape on the window. Passing an empty list reverts the window to being rectangular.

设置窗口形状确定窗口内系统允许绘图和用户交互的区域。在给定区域之外,不会绘制任何像素,也不会注册任何鼠标事件。该窗口不会接收该区域之外的鼠标事件,但会传递到窗口后面的任何事件。

¥Setting a window shape determines the area within the window where the system permits drawing and user interaction. Outside of the given region, no pixels will be drawn and no mouse events will be registered. Mouse events outside of the region will not be received by that window, but will fall through to whatever is behind the window.

win.setThumbarButtons(buttons) Windows

返回 boolean - 按钮是否添加成功

¥Returns boolean - Whether the buttons were added successfully

将具有指定按钮集的缩略图工具栏添加到任务栏按钮布局中窗口的缩略图中。返回一个 boolean 对象,表示缩略图是否添加成功。

¥Add a thumbnail toolbar with a specified set of buttons to the thumbnail image of a window in a taskbar button layout. Returns a boolean object indicates whether the thumbnail has been added successfully.

由于空间有限,缩略图工具栏中的按钮数量不应超过 7 个。一旦设置了缩略图工具栏,由于平台的限制,该工具栏将无法删除。但你可以使用空数组调用 API 来清理按钮。

¥The number of buttons in thumbnail toolbar should be no greater than 7 due to the limited room. Once you setup the thumbnail toolbar, the toolbar cannot be removed due to the platform's limitation. But you can call the API with an empty array to clean the buttons.

buttonsButton 对象的数组:

¥The buttons is an array of Button objects:

  • Button 对象

    ¥Button Object

    • icon NativeImage - 缩略图工具栏中显示的图标。

      ¥icon NativeImage - The icon showing in thumbnail toolbar.

    • click 函数

      ¥click Function

    • tooltip 字符串(可选) - 按钮工具提示的文本。

      ¥tooltip string (optional) - The text of the button's tooltip.

    • flags 字符串[](可选) - 控制按钮的特定状态和行为。默认为 ['enabled']

      ¥flags string[] (optional) - Control specific states and behaviors of the button. By default, it is ['enabled'].

flags 是一个数组,可以包含以下 string

¥The flags is an array that can include following strings:

  • enabled - 该按钮处于活动状态并且可供用户使用。

    ¥enabled - The button is active and available to the user.

  • disabled - 该按钮被禁用。它存在,但具有表明它不会响应用户操作的视觉状态。

    ¥disabled - The button is disabled. It is present, but has a visual state indicating it will not respond to user action.

  • dismissonclick - 单击该按钮后,缩略图窗口立即关闭。

    ¥dismissonclick - When the button is clicked, the thumbnail window closes immediately.

  • nobackground - 不要绘制按钮边框,仅使用图片。

    ¥nobackground - Do not draw a button border, use only the image.

  • hidden - 该按钮不会向用户显示。

    ¥hidden - The button is not shown to the user.

  • noninteractive - 按钮已启用但不可交互;没有绘制按下按钮的状态。该值适用于在通知中使用按钮的情况。

    ¥noninteractive - The button is enabled but not interactive; no pressed button state is drawn. This value is intended for instances where the button is used in a notification.

win.setThumbnailClip(region) Windows

设置将鼠标悬停在任务栏中的窗口上时显示的缩略图图片的窗口区域。你可以通过指定空白区域将缩略图重置为整个窗口:{ x: 0, y: 0, width: 0, height: 0 }

¥Sets the region of the window to show as the thumbnail image displayed when hovering over the window in the taskbar. You can reset the thumbnail to be the entire window by specifying an empty region: { x: 0, y: 0, width: 0, height: 0 }.

win.setThumbnailToolTip(toolTip) Windows

  • toolTip 字符串

    ¥toolTip string

设置将鼠标悬停在任务栏中的窗口缩略图上时显示的工具提示。

¥Sets the toolTip that is displayed when hovering over the window thumbnail in the taskbar.

win.setAppDetails(options) Windows

  • options 对象

    ¥options Object

    • appId 字符串(可选) - 窗户是 应用用户模型 ID。必须设置,否则其他选项无效。

      ¥appId string (optional) - Window's App User Model ID. It has to be set, otherwise the other options will have no effect.

    • appIconPath 字符串(可选) - 窗户是 重新启动图标

      ¥appIconPath string (optional) - Window's Relaunch Icon.

    • appIconIndex 整数(可选) - appIconPath 中图标的索引。当 appIconPath 未设置时忽略。默认为 0

      ¥appIconIndex Integer (optional) - Index of the icon in appIconPath. Ignored when appIconPath is not set. Default is 0.

    • relaunchCommand 字符串(可选) - 窗户是 重新启动命令

      ¥relaunchCommand string (optional) - Window's Relaunch Command.

    • relaunchDisplayName 字符串(可选) - 窗户是 重新启动显示名称

      ¥relaunchDisplayName string (optional) - Window's Relaunch Display Name.

设置窗口任务栏按钮的属性。

¥Sets the properties for the window's taskbar button.

注意:relaunchCommandrelaunchDisplayName 必须始终设置在一起。如果未设置这些属性之一,则两者都不会被使用。

¥Note: relaunchCommand and relaunchDisplayName must always be set together. If one of those properties is not set, then neither will be used.

win.setIcon(icon) Windows Linux

更改窗口图标。

¥Changes window icon.

win.setWindowButtonVisibility(visible) macOS

  • visible 布尔值

    ¥visible boolean

设置窗口交通灯按钮是否可见。

¥Sets whether the window traffic light buttons should be visible.

win.setAutoHideMenuBar(hide) Windows Linux

  • hide 布尔值

    ¥hide boolean

设置窗口菜单栏是否自动隐藏。一旦设置,菜单栏将仅在用户按下单个 Alt 键时显示。

¥Sets whether the window menu bar should hide itself automatically. Once set the menu bar will only show when users press the single Alt key.

如果菜单栏已经可见,调用 setAutoHideMenuBar(true) 不会立即隐藏它。

¥If the menu bar is already visible, calling setAutoHideMenuBar(true) won't hide it immediately.

win.isMenuBarAutoHide() Windows Linux

返回 boolean - 菜单栏是否自动隐藏。

¥Returns boolean - Whether menu bar automatically hides itself.

win.setMenuBarVisibility(visible) Windows Linux

  • visible 布尔值

    ¥visible boolean

设置菜单栏是否可见。如果菜单栏自动隐藏,用户仍然可以通过按单个 Alt 键调出菜单栏。

¥Sets whether the menu bar should be visible. If the menu bar is auto-hide, users can still bring up the menu bar by pressing the single Alt key.

win.isMenuBarVisible() Windows Linux

返回 boolean - 菜单栏是否可见。

¥Returns boolean - Whether the menu bar is visible.

win.setVisibleOnAllWorkspaces(visible[, options]) macOS Linux

  • visible 布尔值

    ¥visible boolean

  • options 对象(可选)

    ¥options Object (optional)

    • visibleOnFullScreen 布尔值(可选)macOS - 设置窗口是否应在全屏窗口上方可见。

      ¥visibleOnFullScreen boolean (optional) macOS - Sets whether the window should be visible above fullscreen windows.

    • skipTransformProcessType 布尔值(可选)macOS - 默认情况下,调用 setVisibleOnAllWorkspaces 将在 UIElementApplication 和 ForegroundApplication 之间转换进程类型,以确保正确的行为。但是,这会在每次调用时隐藏窗口和停靠栏一小段时间。如果你的窗口已经是 UIElementApplication 类型,则可以通过将 true 传递给 skipTransformProcessType 来绕过此转换。

      ¥skipTransformProcessType boolean (optional) macOS - Calling setVisibleOnAllWorkspaces will by default transform the process type between UIElementApplication and ForegroundApplication to ensure the correct behavior. However, this will hide the window and dock for a short time every time it is called. If your window is already of type UIElementApplication, you can bypass this transformation by passing true to skipTransformProcessType.

设置窗口是否应在所有工作区中可见。

¥Sets whether the window should be visible on all workspaces.

注意:该 API 在 Windows 上不执行任何操作。

¥Note: This API does nothing on Windows.

win.isVisibleOnAllWorkspaces() macOS Linux

返回 boolean - 窗口是否在所有工作区中可见。

¥Returns boolean - Whether the window is visible on all workspaces.

注意:此 API 在 Windows 上始终返回 false。

¥Note: This API always returns false on Windows.

win.setIgnoreMouseEvents(ignore[, options])

  • ignore 布尔值

    ¥ignore boolean

  • options 对象(可选)

    ¥options Object (optional)

    • forward 布尔值(可选) macOS Windows - 如果为 true,则将鼠标移动消息转发到 Chromium,启用鼠标相关事件,例如 mouseleave。仅当 ignore 为真时使用。如果 ignore 为 false,则无论该值如何,转发始终被禁用。

      ¥forward boolean (optional) macOS Windows - If true, forwards mouse move messages to Chromium, enabling mouse related events such as mouseleave. Only used when ignore is true. If ignore is false, forwarding is always disabled regardless of this value.

使窗口忽略所有鼠标事件。

¥Makes the window ignore all mouse events.

该窗口中发生的所有鼠标事件都会传递到该窗口下方的窗口,但如果该窗口具有焦点,它仍然会接收键盘事件。

¥All mouse events happened in this window will be passed to the window below this window, but if this window has focus, it will still receive keyboard events.

win.setContentProtection(enable) macOS Windows

  • enable 布尔值

    ¥enable boolean

防止窗口内容被其他应用捕获。

¥Prevents the window contents from being captured by other apps.

在 macOS 上,它将 NSWindow 的共享类型设置为 NSWindowSharingNone。在 Windows 上,它使用 WDA_EXCLUDEFROMCAPTURE 调用 SetWindowDisplayAffinity。对于 Windows 10 版本 2004 及更高版本,窗口将从捕获中完全删除,较旧的 Windows 版本的行为就像应用 WDA_MONITOR 捕获黑色窗口一样。

¥On macOS it sets the NSWindow's sharingType to NSWindowSharingNone. On Windows it calls SetWindowDisplayAffinity with WDA_EXCLUDEFROMCAPTURE. For Windows 10 version 2004 and up the window will be removed from capture entirely, older Windows versions behave as if WDA_MONITOR is applied capturing a black window.

win.setFocusable(focusable) macOS Windows

  • focusable 布尔值

    ¥focusable boolean

更改窗口是否可以聚焦。

¥Changes whether the window can be focused.

在 macOS 上,它不会从窗口中移除焦点。

¥On macOS it does not remove the focus from the window.

win.isFocusable() macOS Windows

返回 boolean - 窗口是否可以聚焦。

¥Returns boolean - Whether the window can be focused.

win.setParentWindow(parent)

  • parent 基本窗口 | 无效的

    ¥parent BaseWindow | null

设置 parent 为当前窗口的父窗口,传递 null 会将当前窗口变成顶层窗口。

¥Sets parent as current window's parent window, passing null will turn current window into a top-level window.

win.getParentWindow()

返回 BaseWindow | null - 父窗口或 null(如果没有父窗口)。

¥Returns BaseWindow | null - The parent window or null if there is no parent.

win.getChildWindows()

返回 BaseWindow[] - 所有子窗口。

¥Returns BaseWindow[] - All child windows.

win.setAutoHideCursor(autoHide) macOS

  • autoHide 布尔值

    ¥autoHide boolean

控制打字时是否隐藏光标。

¥Controls whether to hide cursor when typing.

win.selectPreviousTab() macOS

当启用原生选项卡并且窗口中还有其他选项卡时,选择上一个选项卡。

¥Selects the previous tab when native tabs are enabled and there are other tabs in the window.

win.selectNextTab() macOS

当启用原生选项卡并且窗口中有其他选项卡时选择下一个选项卡。

¥Selects the next tab when native tabs are enabled and there are other tabs in the window.

win.showAllTabs() macOS

启用原生选项卡时显示或隐藏选项卡概述。

¥Shows or hides the tab overview when native tabs are enabled.

win.mergeAllWindows() macOS

当启用原生选项卡并且有多个打开的窗口时,将所有窗口合并为具有多个选项卡的一个窗口。

¥Merges all windows into one window with multiple tabs when native tabs are enabled and there is more than one open window.

win.moveTabToNewWindow() macOS

如果启用了原生选项卡并且当前窗口中有多个选项卡,则将当前选项卡移动到新窗口中。

¥Moves the current tab into a new window if native tabs are enabled and there is more than one tab in the current window.

win.toggleTabBar() macOS

如果启用了原生选项卡并且当前窗口中只有一个选项卡,则切换选项卡栏的可见性。

¥Toggles the visibility of the tab bar if native tabs are enabled and there is only one tab in the current window.

win.addTabbedWindow(baseWindow) macOS

  • baseWindow BaseWindow

在窗口实例的选项卡之后添加一个窗口作为该窗口上的选项卡。

¥Adds a window as a tab on this window, after the tab for the window instance.

win.setVibrancy(type) macOS

  • type 字符串 | 无效的 - 可以是 titlebarselectionmenupopoversidebarheadersheetwindowhudfullscreen-uitooltipcontentunder-windowunder-page。有关详细信息,请参阅 macOS 文档

    ¥type string | null - Can be titlebar, selection, menu, popover, sidebar, header, sheet, window, hud, fullscreen-ui, tooltip, content, under-window, or under-page. See the macOS documentation for more details.

为窗户增添活力效果。传递 null 或空字符串将消除窗口的活力效果。

¥Adds a vibrancy effect to the window. Passing null or an empty string will remove the vibrancy effect on the window.

win.setBackgroundMaterial(material) Windows

  • material 字符串

    ¥material string

    • auto - 让桌面窗口管理器 (DWM) 自动决定系统为该窗口绘制的背景材质。这是默认设置。

      ¥auto - Let the Desktop Window Manager (DWM) automatically decide the system-drawn backdrop material for this window. This is the default.

    • none - 不要绘制任何系统背景。

      ¥none - Don't draw any system backdrop.

    • mica - 绘制与长寿命窗口相对应的背景材质效果。

      ¥mica - Draw the backdrop material effect corresponding to a long-lived window.

    • acrylic - 绘制与瞬态窗口相对应的背景材质效果。

      ¥acrylic - Draw the backdrop material effect corresponding to a transient window.

    • tabbed - 绘制与带有选项卡式标题栏的窗口相对应的背景材质效果。

      ¥tabbed - Draw the backdrop material effect corresponding to a window with a tabbed title bar.

此方法设置浏览器窗口的系统绘制背景材料,包括非客户区域后面。

¥This method sets the browser window's system-drawn background material, including behind the non-client area.

有关详细信息,请参阅 Windows 文档

¥See the Windows documentation for more details.

注意:此方法仅在 Windows 11 22H2 及更高版本上支持。

¥Note: This method is only supported on Windows 11 22H2 and up.

win.setWindowButtonPosition(position) macOS

在无框窗口中设置交通灯按钮的自定义位置。通过 null 会将位置重置为默认值。

¥Set a custom position for the traffic light buttons in frameless window. Passing null will reset the position to default.

win.getWindowButtonPosition() macOS

返回 Point | null - 无框窗红绿灯按钮自定义位置,无自定义位置时返回 null

¥Returns Point | null - The custom position for the traffic light buttons in frameless window, null will be returned when there is no custom position.

win.setTouchBar(touchBar) macOS

  • touchBar TouchBar | null

设置当前窗口的 touchBar 布局。指定 nullundefined 将清除触摸栏。此方法仅在机器具有触摸条时有效。

¥Sets the touchBar layout for the current window. Specifying null or undefined clears the touch bar. This method only has an effect if the machine has a touch bar.

注意:TouchBar API 目前处于实验阶段,可能会在未来的 Electron 版本中更改或删除。

¥Note: The TouchBar API is currently experimental and may change or be removed in future Electron releases.

win.setTitleBarOverlay(options) Windows Linux

  • options 对象

    ¥options Object

    • color 字符串(可选) - 启用时窗口控件叠加层的 CSS 颜色。

      ¥color String (optional) - The CSS color of the Window Controls Overlay when enabled.

    • symbolColor 字符串(可选) - 启用后窗口控件叠加上符号的 CSS 颜色。

      ¥symbolColor String (optional) - The CSS color of the symbols on the Window Controls Overlay when enabled.

    • height 整数(可选) - 标题栏和窗口控件叠加的高度(以像素为单位)。

      ¥height Integer (optional) - The height of the title bar and Window Controls Overlay in pixels.

在已启用窗口控件覆盖的窗口上,此方法更新标题栏覆盖的样式。

¥On a Window with Window Controls Overlay already enabled, this method updates the style of the title bar overlay.

在 Linux 上,如果未明确设置,则会自动计算 symbolColor 以具有与 color 的最小可访问对比度。

¥On Linux, the symbolColor is automatically calculated to have minimum accessible contrast to the color if not explicitly set.