screen
检索有关屏幕大小、显示器、光标位置等的信息。
Process: Main
在 app
模块 emitted ready
事件之前,您不能使用此模块。
screen
是一个EventEmitter.
[!NOTE] In the renderer / DevTools,
window.screen
is a reserved DOM property, so writinglet { screen } = require('electron')
will not work.
创建填充整个屏幕的窗口的示例:
- 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')
})
另一个在外部显示器中创建窗口的例子
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] 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.
- Device-independent pixel (DIP) points are virtualized screen points scaled based on the DPI (dots per inch) of the display.
事件
screen
模块触发以下事件:
Event: 'display-added'
返回:
event
EventnewDisplay
Display
当新的窗口newDisplay
被添加的时候触发。
Event: 'display-removed'
返回:
event
EventoldDisplay
Display
当旧的窗口oldDisplay
被移除的时候触发。
Event: 'display-metrics-changed'
返回:
event
Eventdisplay
DisplaychangedMetrics
string[]
当display
中的一个或多个值发生改变时发出。 changedMetrics
是描述更改信息的字符串数组。 Possible changes are bounds
, workArea
, scaleFactor
and rotation
.
方法
screen
模块有以下方法:
screen.getCursorScreenPoint()
Returns Point
当前鼠标的绝对位置。
[!NOTE] The return value is a DIP point, not a screen physical point.
screen.getPrimaryDisplay()
Returns Display - The primary display.
screen.getAllDisplays()
Returns Display[] - An array of displays that are currently available.
screen.getDisplayNearestPoint(point)
point
Point
Returns Display - The display nearest the specified point.
screen.getDisplayMatching(rect)
rect
Rectangle
Returns Display - The display that most closely intersects the provided bounds.
screen.screenToDipPoint(point)
Windows
point
Point
Returns Point
将屏幕物理点转换为屏幕DIP点。 相对于物理点的显示执行DPI缩放
screen.dipToScreenPoint(point)
Windows
point
Point
Returns Point
将屏幕DIP点转换为屏幕物理点。 相对于DPI的显示关系执行缩放
screen.screenToDipRect(window, rect)
Windows
window
BrowserWindow | nullrect
Rectangle
Returns Rectangle
将屏幕物理矩阵转换为屏幕DIP矩阵。 相对最近于 window
的显示来执行DPI缩放。 如果 window
为空,将执行缩放到最近的 rect
的显示位置。
screen.dipToScreenRect(window, rect)
Windows
window
BrowserWindow | nullrect
Rectangle
Returns Rectangle
将屏幕DIP矩阵转换为屏幕物理矩阵。 相对最近于 window
的显示来执行DPI缩放。 如果 window
为空,将执行缩放到最近的 rect
的显示位置。