【发布时间】:2021-11-27 18:12:20
【问题描述】:
我想知道BrowserWindow 在两个或多个屏幕上的位置。
我猜 Electron API 没有给出方法。我怎样才能做到这一点?有什么办法吗??
【问题讨论】:
-
你能提供更多关于你想要什么的信息吗?谢谢。
标签: javascript electron
我想知道BrowserWindow 在两个或多个屏幕上的位置。
我猜 Electron API 没有给出方法。我怎样才能做到这一点?有什么办法吗??
【问题讨论】:
标签: javascript electron
没有直接的方法可以实现这一点。但是跟随绝对可以达到您的目的。
const {BrowserWindow, screen} = require('electron');
let window = new BrowserWindow();
// Load a URL and show the window first
const winBounds = window.getBounds();
const whichScreen = screen.getDisplayNearestPoint({x: winBounds.x, y: winBounds.y});
// Returns the screen where your window is located
经过测试可以正常工作。
【讨论】:
用于在您创建窗口后检测哪个屏幕包含您的窗口:
const win = new BrowserWindow({...});
使用Screen API和getDisplayNearestPoint()方法,可以得到屏幕坐标:
const winBounds = win.getBounds();
const distScreen = screen.getDisplayNearestPoint({x: winBounds.x, y: winBounds.y})
另外,如果你想知道哪个屏幕里面有鼠标光标,使用:
let cursor = screen.getCursorScreenPoint();
let distScreen = screen.getDisplayNearestPoint({x: cursor.x, y: cursor.y});
【讨论】: