当前位置: 首页 > 文档资料 > Electron 中文文档 >

类 BrowserView

优质
小牛编辑
175浏览
2023-12-01

类: BrowserView

创建和控制视图

进程:主进程

BrowserView 被用来让 BrowserWindow 嵌入更多的 web 内容。 它就像一个子窗口,除了它的位置是相对于父窗口。 这意味着可以替代webview标签.

Class: BrowserView

Create and control views.

Process: Main

A BrowserView can be used to embed additional web content into a BrowserWindow. It is like a child window, except that it is positioned relative to its owning window. It is meant to be an alternative to the webview tag.

示例

// 在主进程中.
const { BrowserView, BrowserWindow } = require('electron')
let win = new BrowserWindow({ width: 800, height: 600 })
win.on('closed', () => {
  win = null
})
let view = new BrowserView()
win.setBrowserView(view)
view.setBounds({ x: 0, y: 0, width: 300, height: 300 })
view.webContents.loadURL('https://electronjs.org')

Example

// In the main process.
const { BrowserView, BrowserWindow } = require('electron')
let win = new BrowserWindow({ width: 800, height: 600 })
win.on('closed', () => {
  win = null
})
let view = new BrowserView()
win.setBrowserView(view)
view.setBounds({ x: 0, y: 0, width: 300, height: 300 })
view.webContents.loadURL('https://electronjs.org')

new BrowserView([可选]) 实验功能

  • 参数 Object (可选)

    • webPreferences Object (可选) - 详情请看 BrowserWindow.

new BrowserView([options]) Experimental

  • options Object (optional)

    • webPreferences Object (optional) - See BrowserWindow.

静态方法

Static Methods

BrowserView.getAllViews()

返回 BrowserWindow[] - 所有打开的窗口的数组

BrowserView.getAllViews()

Returns BrowserView[] - An array of all opened BrowserViews.

BrowserView.fromWebContents(webContents)

  • webContents WebContents

返回 BrowserView | null - 如果内容不属于BrowserView,则它拥有返回的webContentsnull

BrowserView.fromWebContents(webContents)

  • webContents WebContents

Returns BrowserView | null - The BrowserView that owns the given webContents or null if the contents are not owned by a BrowserView.

BrowserView.fromId(id)

  • id Integer

返回 BrowserView - 带有id的视图.

BrowserView.fromId(id)

  • id Integer

Returns BrowserView - The view with the given id.

实例属性

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

Instance Properties

Objects created with new BrowserView have the following properties:

view.webContents 实验功能

视图的WebContents 对象

view.webContents Experimental

A WebContents object owned by this view.

view.id 实验功能

视图的唯一ID Integer.

view.id Experimental

A Integer representing the unique ID of the view.

实例方法

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

Instance Methods

Objects created with new BrowserView have the following instance methods:

view.destroy()

强制关闭视图, 不会为网页发出 unloadbeforeunload 事件。 完成视图后, 请调用此函数, 以便尽快释放内存和其他资源。

view.destroy()

Force closing the view, the unload and beforeunload events won't be emitted for the web page. After you're done with a view, call this function in order to free memory and other resources as soon as possible.

view.isDestroyed()

返回 Boolean -判断窗口是否被销毁

view.isDestroyed()

Returns Boolean - Whether the view is destroyed.

view.setAutoResize(options) 实验功能

  • options Object

    • width Boolean - 如果为true,视图宽度跟随窗口变化. 默认为 false.
    • height Boolean - 如果为true,视图高度跟随窗口变化. 默认为 false.
    • horizontal Boolean - If true, the view's x position and width will grow and shrink proportionly with the window. false by default.
    • vertical Boolean - If true, the view's y position and height will grow and shrink proportinaly with the window. false by default.

view.setAutoResize(options) Experimental

  • options Object

    • width Boolean - If true, the view's width will grow and shrink together with the window. false by default.
    • height Boolean - If true, the view's height will grow and shrink together with the window. false by default.
    • horizontal Boolean - If true, the view's x position and width will grow and shrink proportionly with the window. false by default.
    • vertical Boolean - If true, the view's y position and height will grow and shrink proportinaly with the window. false by default.

view.setBounds(bounds) 实验功能

  • bounds Rectangle

调整视图的大小,并将它移动到窗口边界

view.setBounds(bounds) Experimental

  • bounds Rectangle

Resizes and moves the view to the supplied bounds relative to the window.

view.setBackgroundColor(color) 实验功能

  • color String - 颜色值格式为 #aarrggbb#argb, 透明度为可选参数.

view.setBackgroundColor(color) Experimental

  • color String - Color in #aarrggbb or #argb form. The alpha channel is optional.