Skip to main content

screen

Retrieve information about screen size, displays, cursor position, etc.检索有关屏幕大小、显示、游标位置等的信息。

Process:进程:Main

This module cannot be used until the ready event of the app module is emitted.在发出app模块的ready事件之前,无法使用此模块。

screen is an EventEmitter.是一个EventEmitter

Note: In the renderer / DevTools, window.screen is a reserved DOM property, so writing let { screen } = require('electron') will not work.在renderer/DevTools中,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://electronjs.org/docs/api/screen

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

let mainWindow = null

app.whenReady().then(() => {
// We cannot require the screen module until the app is ready.
const { screen } = require('electron')

// 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')
}
})

Events事件

The screen module emits the following events:screen模块会发出以下事件:

Event: 'display-added'

Returns:返回:

Emitted when newDisplay has been added.添加newDisplay时发出。

Event: 'display-removed'

Returns:返回:

Emitted when oldDisplay has been removed.在删除oldDisplay时发出。

Event: 'display-metrics-changed'

Returns:返回:

  • event Event
  • display Display
  • changedMetrics string[]

Emitted when one or more metrics change in a display. display中的一个或多个指标发生变化时发出。The changedMetrics is an array of strings that describe the changes. changedMetrics是一个描述更改的字符串数组。Possible changes are bounds, workArea, scaleFactor and rotation.可能的更改是boundsworkAreascaleFactorrotation

Methods方法

The screen module has the following methods:screen模块有以下方法:

screen.getCursorScreenPoint()

Returns 返回Point

The current absolute position of the mouse pointer.鼠标游标的当前绝对位置。

Note: The return value is a DIP point, not a screen physical point.返回值是DIP点,而不是屏幕物理点。

screen.getPrimaryDisplay()

Returns 返回Display - The primary display.主显示器。

screen.getAllDisplays()

Returns 返回Display[] - An array of displays that are currently available.当前可用的显示器阵列。

screen.getDisplayNearestPoint(point)

Returns 返回Display - The display nearest the specified point.离指定点最近的显示。

screen.getDisplayMatching(rect)

Returns 返回Display - The display that most closely intersects the provided bounds.与所提供的边界相交最紧密的显示。

screen.screenToDipPoint(point) Windows

Returns 返回Point

Converts a screen physical point to a screen DIP point. 将屏幕物理点转换为屏幕DIP点。The DPI scale is performed relative to the display containing the physical point.DPI缩放是相对于包含物理点的显示器执行的。

screen.dipToScreenPoint(point) Windows

Returns 返回Point

Converts a screen DIP point to a screen physical point. 将屏幕DIP点转换为屏幕物理点。The DPI scale is performed relative to the display containing the DIP point.DPI缩放是相对于包含DIP点的显示器执行的。

screen.screenToDipRect(window, rect) Windows

Returns 返回Rectangle

Converts a screen physical rect to a screen DIP rect. 将屏幕物理矩形转换为屏幕DIP矩形。The DPI scale is performed relative to the display nearest to window. DPI缩放是相对于离window最近的显示器执行的。If window is null, scaling will be performed to the display nearest to rect.如果windownull,则缩放将执行到最接近rect的显示。

screen.dipToScreenRect(window, rect) Windows

Returns 返回Rectangle

Converts a screen DIP rect to a screen physical rect. 将屏幕DIP矩形转换为屏幕物理矩形。The DPI scale is performed relative to the display nearest to window. DPI缩放是相对于离window最近的显示器执行的。If window is null, scaling will be performed to the display nearest to rect.如果windownull,则缩放将执行到最接近rect的显示。