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.
screen
是 EventEmitter。
¥screen
is an EventEmitter.
[!NOTE] 在渲染器/DevTools 中,
window.screen
是保留的 DOM 属性,因此写入let { screen } = require('electron')
将不起作用。¥[!NOTE] In the renderer / DevTools,
window.screen
is a reserved DOM property, so writinglet { screen } = require('electron')
will not work.
创建一个充满整个屏幕的窗口的示例:
¥An example of creating a window that fills the whole screen:
- main.js
// 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
结构。该进程有两种可用的坐标:¥[!NOTE] Screen coordinates used by this module are
Point
structures. There are two kinds of coordinates available to the process:
物理屏幕点是显示器上的原始硬件像素。
¥Physical screen points are raw hardware pixels on a display.
与设备无关的像素 (DIP) 点是根据显示器的 DPI(每英寸点数)缩放的虚拟化屏幕点。
¥Device-independent pixel (DIP) points are virtualized screen points scaled based on the DPI (dots per inch) of the display.
事件
¥Events
screen
模块发出以下事件:
¥The screen
module emits the following events:
事件:'display-added'
¥Event: 'display-added'
返回:
¥Returns:
添加 newDisplay
后发出。
¥Emitted when newDisplay
has been added.
事件:'display-removed'
¥Event: 'display-removed'
返回:
¥Returns:
当 oldDisplay
被删除时发出。
¥Emitted when oldDisplay
has been removed.
事件:'display-metrics-changed'
¥Event: 'display-metrics-changed'
返回:
¥Returns:
当 display
中的一个或多个指标发生变化时发出。changedMetrics
是描述更改的字符串数组。可能的更改有 bounds
、workArea
、scaleFactor
和 rotation
。
¥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 点,而不是屏幕物理点。
¥[!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 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 尚不支持 - 如果在 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
-
window
BrowserWindow | 无效的¥
window
BrowserWindow | null -
rect
长方形¥
rect
Rectangle
返回 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
-
window
BrowserWindow | 无效的¥
window
BrowserWindow | null -
rect
长方形¥
rect
Rectangle
返回 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
.