BrowserWindow·续4

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 - 窗口的平台特定句柄

Windows上句柄类型为 HWND,macOS 上为 NSView*,Linux 上为Window (unsigned long)

win.hookWindowMessage(message, callback) Windows

  • message Integer

  • callback Function

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

win.isWindowMessageHooked(message) Windows

  • message Integer

返回 Boolean - true 或false ,具体取决于是否钩挂了消息.

win.unhookWindowMessage(message) Windows

  • message Integer

取消窗口信息的钩子。

win.unhookAllWindowMessages() Windows

取消所有窗口信息的钩子。

win.setRepresentedFilename(filename) macOS

  • filename String

设置窗口所代表的文件的路径名,并且将这个文件的图标放在窗口标题栏上。

win.getRepresentedFilename() macOS

返回 String - 获取窗口当前文件路径.

win.setDocumentEdited(edited) macOS

  • edited Boolean

明确指出窗口文档是否可以编辑, 如果设置为true则将标题栏的图标变成灰色.

win.isDocumentEdited() macOS

返回 Boolean - 判断当前窗口文档是否可编辑.

win.focusOnWebView()

win.blurWebView()

win.capturePage([rect])

  • rect Rectangle (可选) - 捕获的区域

Returns Promise<​NativeImage> - Resolves with a NativeImage

Captures a snapshot of the page within rect. Omitting rect will capture the whole visible page.

win.loadURL(url[, options])

  • url String

  • options Object (optional)

  • httpReferrer (String | Referrer) (optional) - An HTTP Referrer URL.

  • userAgent String (可选) - 发起请求的 userAgent.

  • extraHeaders String (可选) - 用 “\n” 分割的额外标题

  • postData (UploadRawData[] | UploadFile[] | UploadBlob[]) (可选)

  • baseURLForDataURL String (optional) - Base URL (with trailing path separator) for files to be loaded by the data URL. This is needed only if the specified url is a data URL and needs to load other files.

Returns Promise<​void> - the promise will resolve when the page has finished loading (see did-finish-load), and rejects if the page fails to load (see did-fail-load).

Same as webContents.loadURL(url[, options]).

url 可以是远程地址 (例如 http://),也可以是 file:// 协议的本地HTML文件的路径.

为了确保文件网址格式正确, 建议使用Node的 url.format 方法:

let url = require('url').format({  protocol: 'file',  slashes: true,  pathname: require('path').join(__dirname, 'index.html')})win.loadURL(url)

您可以通过执行以下操作, 使用带有网址编码数据的 POST请求​​加载网址:

win.loadURL('http://localhost:8000/post', {  postData: [{    type: 'rawData',    bytes: Buffer.from('hello=world')  }],  extraHeaders: 'Content-Type: application/x-www-form-urlencoded'})

win.loadFile(filePath[, options])

  • filePath String

  • options Object (optional)

  • query Record (optional) - Passed to url.format().

  • search String (可选) - 传递给 url.format().

  • hash String (可选) - 传递给 url.format().

Returns Promise<​void> - the promise will resolve when the page has finished loading (see did-finish-load), and rejects if the page fails to load (see did-fail-load).

Same as webContents.loadFile, filePath should be a path to an HTML file relative to the root of your application. See the webContents docs for more information.

win.reload()

与 webContents.reload 相同.

win.setMenu(menu) Linux Windows

  • menu Menu | null

Sets the menu as the window’s menu bar.

win.removeMenu() Linux Windows

Remove the window’s menu bar.

win.setProgressBar(progress[, options])

  • progress Double

  • options Object (optional)

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

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

当进度小于0时不显示进度; 当进度大于0时显示结果不确定.

在 Linux 平台上,只支持 Unity 桌面模式, 你需要在 package.json 中为 desktopName 指定 *.desktop 的文件名. By default, it will assume {app.name}.desktop.

在 Windows 上, 可以传递模式。 可以接受的值为none, normal, indeterminate, error和 paused. 如果没有设置模式 (但值在有效范围内) 的情况下调用 setProgressBar, 默认值为normal。

win.setOverlayIcon(overlay, description) Windows

  • overlay NativeImage | null - 右下角任务栏的显示图标。 如果此参数是 null,覆盖层层会被清除。

  • description String -提供给屏幕阅读器的描述文字

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

win.setHasShadow(hasShadow)

  • hasShadow Boolean

Sets whether the window should have a shadow.

win.hasShadow()

返回 Boolean - 判断窗口是否有阴影.

win.setOpacity(opacity) Windows macOS

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

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

win.getOpacity()

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

win.setShape(rects) Windows Linux 实验性

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

对窗口形状的设置决定了窗口内系统允许绘制与用户交互的区域. 在给定的区域外, 没有像素会被绘制, 且没有鼠标事件会被登记. 在该区域外的鼠标事件将不会被该窗口接收, 而是落至该窗口后方的任意窗口.

win.setThumbarButtons(buttons) Windows

  • buttons ThumbarButton[]

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

将指定的一组按钮添加到菜单栏的缩图工具栏上。 返回一个 Boolean 对象表示是否成功地添加了缩略图.

由于空间有限, 缩图工具栏中的按钮数量不要超过7个。 一旦设置了缩略图工具栏,则无法删除。 但你可以通过调用 API 传递一个空数组来清除按钮.

buttons 是一个 Button 对象的数组:

  • Button Object

  • icon NativeImage - 在缩图工具栏上显示的图标.

  • click Function

  • tooltip String (可选) - 按钮的提示文本.

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

flags 属性是一个数组,包含以下String类型的值:

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

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

  • dismissonclick - 当按钮被点击时,缩略图窗口立即关闭。

  • nobackground - 不可以画按钮边框,只能使用图片背景。

  • hidden - 该按钮对用户不可见。

  • 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

  • region Rectangle 窗口的区域

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

win.setThumbnailToolTip(toolTip) Windows

  • toolTip String

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

win.setAppDetails(options) Windows

  • options Object

  • appId String (可选) - 窗口的 App User Model ID.aspx). 该项必须设置, 否则其他选项将没有效果.

  • appIconPath String (可选) -窗口的 Relaunch Icon.aspx).

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

  • relaunchCommand String (可选) - 窗口的 重新启动命令.aspx).

  • relaunchDisplayName String (可选) - 窗口的重新启动显示名称.aspx).

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

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

win.showDefinitionForSelection() macOS

和 webContents.showDefinitionForSelection() 相同.

win.setIcon(icon) Windows Linux

  • icon NativeImage | String

设置窗口图标

win.setWindowButtonVisibility(visible) macOS

  • visible Boolean

设置是否窗口交通灯需要显示。

当titleBarStyle 是 customButtonsOnHover的时候,不可调用。

win.setAutoHideMenuBar(hide)

  • hide Boolean

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)时不会立刻隐藏.

过时的

win.isMenuBarAutoHide()

返回 Boolean - 判断窗口的菜单栏是否自动隐藏.

过时的

win.setMenuBarVisibility(visible) Windows Linux

  • visible Boolean

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

返回 Boolean - 判断窗口的菜单栏是否可见.

win.setVisibleOnAllWorkspaces(visible[, options])

  • visible Boolean

  • options Object (optional)

  • visibleOnFullScreen Boolean (optional) macOS - Sets whether the window should be visible above fullscreen windows deprecated

设置窗口是否在所有工作空间上可见

注意: 该 API 在 Windows 上无效.

win.isVisibleOnAllWorkspaces()

返回 Boolean - 判断窗口是否在所有工作空间上可见.

注意: 该 API 在 Windows 上始终返回 false.

win.setIgnoreMouseEvents(ignore[, options])

  • ignore Boolean

  • options Object (optional)