Skip to main content

screen

检索有关屏幕尺寸、显示器、光标位置等的信息。

进程:主进程

🌐 Process: Main

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

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

screen 是一个 事件触发器

note

在渲染器 / 开发者工具中,window.screen 是保留的 DOM 属性,因此写入 let { screen } = require('electron') 将不起作用。

创建一个充满整个屏幕的窗口的示例:

🌐 An example of creating a window that fills the whole screen:

// Retrieve information about screen size, displays, cursor position, etc.
//
// For more info, see:
// https://www.electronjs.org/docs/latest/api/screen

const { app, BrowserWindow, screen } = require('electron/main')

let mainWindow = null

app.whenReady().then(() => {
// Create a window that fills the screen's available work area.
const primaryDisplay = screen.getPrimaryDisplay()
const { width, height } = primaryDisplay.workAreaSize

mainWindow = new BrowserWindow({ width, height })
mainWindow.loadURL('https://electronjs.org')
})

在外部显示器中创建窗口的另一个示例:

🌐 Another example of creating a window in the external display:

const { app, BrowserWindow, screen } = require('electron')

let win

app.whenReady().then(() => {
const displays = screen.getAllDisplays()
const externalDisplay = displays.find((display) => {
return display.bounds.x !== 0 || display.bounds.y !== 0
})

if (externalDisplay) {
win = new BrowserWindow({
x: externalDisplay.bounds.x + 50,
y: externalDisplay.bounds.y + 50
})
win.loadURL('https://github.com')
}
})
note

此模块使用的屏幕坐标是 Point 结构。 进程可用的坐标有两种:

  • 物理屏幕点 是显示器上的原始硬件像素。
  • 设备无关像素 (DIP) 点 是根据显示器的 DPI(每英寸像素数)缩放的虚拟屏幕点。

事件

🌐 Events

screen 模块会触发以下事件:

🌐 The screen module emits the following events:

Event: 'display-added'

返回:

🌐 Returns:

  • event 活动
  • newDisplay 显示

newDisplay 已添加时触发。

🌐 Emitted when newDisplay has been added.

Event: 'display-removed'

返回:

🌐 Returns:

  • event 活动
  • oldDisplay 显示

oldDisplay 已被移除时触发。

🌐 Emitted when oldDisplay has been removed.

Event: 'display-metrics-changed'

返回:

🌐 Returns:

  • event 活动
  • display 显示
  • changedMetrics 字符串数组

当一个或多个指标在 display 中发生变化时触发。changedMetrics 是一个描述这些变化的字符串数组。可能的变化有 boundsworkAreascaleFactorrotation

🌐 Emitted when one or more metrics change in a display. The changedMetrics is an array of strings that describe the changes. Possible changes are bounds, workArea, scaleFactor and rotation.

方法

🌐 Methods

screen 模块具有以下方法:

🌐 The screen module has the following methods:

screen.getCursorScreenPoint()

返回 Point

🌐 Returns Point

鼠标指针当前的绝对位置。

🌐 The current absolute position of the mouse pointer.

note

返回值是 DIP 点,而不是屏幕物理点。

screen.getPrimaryDisplay()

返回 Display - 主要显示。

🌐 Returns Display - The primary display.

screen.getAllDisplays()

返回 Display[] - 当前可用的显示器数组。

🌐 Returns Display[] - An array of displays that are currently available.

screen.getDisplayNearestPoint(point)

返回 Display - 显示最接近指定点的内容。

🌐 Returns Display - The display nearest the specified point.

screen.getDisplayMatching(rect)

返回 Display - 与提供的范围最接近交叉的显示屏。

🌐 Returns Display - The display that most closely intersects the provided bounds.

screen.screenToDipPoint(point) Windows Linux

返回 Point

🌐 Returns Point

将屏幕物理点转换为屏幕DIP点。DPI缩放是相对于包含该物理点的显示器进行的。

🌐 Converts a screen physical point to a screen DIP point. The DPI scale is performed relative to the display containing the physical point.

当前在 Wayland 上不支持——如果在那里使用,它将返回传入的点,且不做任何更改。

🌐 Not currently supported on Wayland - if used there it will return the point passed in with no changes.

screen.dipToScreenPoint(point) Windows Linux

返回 Point

🌐 Returns Point

将屏幕 DIP 点转换为屏幕物理点。DPI 缩放是相对于包含该 DIP 点的显示器进行的。

🌐 Converts a screen DIP point to a screen physical point. The DPI scale is performed relative to the display containing the DIP point.

目前 Wayland 尚不支持。

🌐 Not currently supported on Wayland.

screen.screenToDipRect(window, rect) Windows

返回 Rectangle

🌐 Returns Rectangle

将屏幕物理矩形转换为屏幕DIP矩形。DPI缩放是相对于最接近 window 的显示器执行的。如果 window 为 null,缩放将相对于最接近 rect 的显示器执行。

🌐 Converts a screen physical rect to a screen DIP rect. The DPI scale is performed relative to the display nearest to window. If window is null, scaling will be performed to the display nearest to rect.

screen.dipToScreenRect(window, rect) Windows

返回 Rectangle

🌐 Returns Rectangle

将屏幕DIP矩形转换为屏幕物理矩形。DPI缩放是相对于最接近 window 的显示器执行的。如果 window 为 null,缩放将相对于最接近 rect 的显示器执行。

🌐 Converts a screen DIP rect to a screen physical rect. The DPI scale is performed relative to the display nearest to window. If window is null, scaling will be performed to the display nearest to rect.