Skip to main content

screen

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

¥Retrieve information about screen size, displays, cursor position, etc.

进程:主进程

¥Process: Main

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

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

screenEventEmitter

¥screen is an EventEmitter.

注意:在渲染器/DevTools 中,window.screen 是保留的 DOM 属性,因此编写 let { screen } = require('electron') 不起作用。

¥Note: In the renderer / DevTools, window.screen is a reserved DOM property, so writing let { screen } = require('electron') will not work.

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

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

事件

¥Events

screen 模块发出以下事件:

¥The screen module emits the following events:

事件:'display-added'

¥Event: 'display-added'

返回:

¥Returns:

  • event 事件

    ¥event Event

  • newDisplay 展示

    ¥newDisplay Display

添加 newDisplay 后发出。

¥Emitted when newDisplay has been added.

事件:'display-removed'

¥Event: 'display-removed'

返回:

¥Returns:

  • event 事件

    ¥event Event

  • oldDisplay 展示

    ¥oldDisplay Display

oldDisplay 被删除时发出。

¥Emitted when oldDisplay has been removed.

事件:'display-metrics-changed'

¥Event: 'display-metrics-changed'

返回:

¥Returns:

  • event 事件

    ¥event Event

  • display 展示

    ¥display Display

  • changedMetrics 字符串[]

    ¥changedMetrics string[]

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.

注意:返回值是一个 DIP 点,而不是屏幕物理点。

¥Note: The return value is a DIP point, not a screen physical point.

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

返回 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.

screen.dipToScreenPoint(point) Windows

返回 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.

screen.screenToDipRect(window, rect) Windows

返回 Rectangle

¥Returns Rectangle

将屏幕物理矩形转换为屏幕 DIP 矩形。DPI 缩放是相对于最接近 window 的显示器进行的。如果 window 为空,则缩放到最接近 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 为空,则缩放到最接近 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.