screen
Récupère les informations sur la taille, l'écran, la position du curseur, etc.
Processus : Main
Ce module ne peut pas être utilisé tant que l'événement prêt
du module app
n'est pas émis.
screen
est un EventEmitter.
Remarque : Dans le renderer / DevTools, window.screen
est une propriété réservée au DOM, alors écrire let { screen } = require('electron')
ne fonctionnera pas.
Un exemple de création d'une fenêtre qui prendra tout l'écran :
- main.js
// 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')
})
Un autre exemple de création d'une fenêtre dans l'écran externe :
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')
}
})
Événements
Le module screen
émet les événements suivants :
Événement : 'display-added'
Retourne :
event
EventnewDisplay
Display
Émis lorsque newDisplay
a été ajouté.
Événement : 'display-removed'
Retourne :
event
EventoldDisplay
Display
Émis lorsque oldDisplay
a été retiré.
Événement 'display-metrics-changed'
Retourne :
event
Eventdisplay
DisplaychangedMetrics
string[]
Émis lorsqu’un ou plusieurs métrics changent dans un display
. changedMetrics
est un tableau de chaîne de caractères décrivant les modifications. Les modifications possibles sont bounds
, workArea
, scaleFactor
et rotation
.
Méthodes
Le module screen
dispose des méthodes suivantes :
screen.getCursorScreenPoint()
Retourne Point
La position absolue du pointeur de la souris.
Note: The return value is a DIP point, not a screen physical point.
screen.getPrimaryDisplay()
Retourne Display
- L'écran principal.
screen.getAllDisplays()
Retourne Display[]
- Un tableau d'écrans qui sont actuellement disponibles.
screen.getDisplayNearestPoint(point)
point
Point
Retourne Display
- L'écran le plus proche du point spécifié.
screen.getDisplayMatching(rect)
rect
Rectangle
Retourne Display
- L'écran qui croise le plus les limites d'intersection données.
screen.screenToDipPoint(point)
Windows
point
Point
Retourne Point
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
Point
Retourne Point
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
window
BrowserWindow | nullrect
Rectangle
Retourne Rectangle
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 | nullrect
Rectangle
Retourne Rectangle
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
.