screen
擷取關於螢幕尺寸、顯示器、游標位置等等的資訊。
程序: 主程序
此模組必須等到 app
模組的 ready
事件發射後才能使用。
screen
是一個 EventEmitter。
注意: 在渲染器 / DevTools 中,window.screen
是一個保留的 DOM 屬性,因此寫入 let { screen } = require('electron')
將不會work。
建立填滿整個螢幕視窗的範例
- main.js
// Retrieve information about screen size, displays, cursor position, etc.
//
// For more info, see:
// https://electron.dev.org.tw/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://electron.dev.org.tw')
})
在外部顯示器中建立視窗的另一個範例
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')
}
})
事件
screen
模組發射下列事件
事件:'display-added'
回傳
event
事件newDisplay
Display
當 newDisplay
被新增時發射。
事件:'display-removed'
回傳
event
事件oldDisplay
Display
當 oldDisplay
被移除時發射。
事件:'display-metrics-changed'
回傳
event
事件display
DisplaychangedMetrics
string[]
當 display
中的一個或多個指標變更時發射。changedMetrics
是一個字串陣列,描述變更。可能的變更為 bounds
、workArea
、scaleFactor
和 rotation
。
方法
screen
模組具有下列方法
screen.getCursorScreenPoint()
回傳 Point
滑鼠指標目前的絕對位置。
注意: 回傳值是一個 DIP 點,而不是螢幕實體點。
screen.getPrimaryDisplay()
回傳 Display - 主要顯示器。
screen.getAllDisplays()
回傳 Display[] - 目前可用的顯示器陣列。
screen.getDisplayNearestPoint(point)
point
Point
回傳 Display - 最靠近指定點的顯示器。
screen.getDisplayMatching(rect)
rect
Rectangle
回傳 Display - 最接近相交於所提供邊界的顯示器。
screen.screenToDipPoint(point)
Windows
point
Point
回傳 Point
將螢幕實體點轉換為螢幕 DIP 點。DPI 縮放是相對於包含實體點的顯示器執行的。
screen.dipToScreenPoint(point)
Windows
point
Point
回傳 Point
將螢幕 DIP 點轉換為螢幕實體點。DPI 縮放是相對於包含 DIP 點的顯示器執行的。
screen.screenToDipRect(window, rect)
Windows
window
BrowserWindow | nullrect
Rectangle
回傳 Rectangle
將螢幕實體矩形轉換為螢幕 DIP 矩形。DPI 縮放是相對於最靠近 window
的顯示器執行的。如果 window
為 null,則縮放將對最靠近 rect
的顯示器執行。
screen.dipToScreenRect(window, rect)
Windows
window
BrowserWindow | nullrect
Rectangle
回傳 Rectangle
將螢幕 DIP 矩形轉換為螢幕實體矩形。DPI 縮放是相對於最靠近 window
的顯示器執行的。如果 window
為 null,則縮放將對最靠近 rect
的顯示器執行。