screen - screen.getDisplayId()

概述

查询鼠标光标或触控点所在的屏幕ID(需要鼠标光标或触控点在electron窗口范围内)

基本信息

属性
模块 screen
类型 Method
鸿蒙支持 支持

OHOS 依赖与基本表现

属性
系统权限依赖 无需申请
添加JIT权限 支持
坚盾模式 支持

使用方式差异

属性
使用方式差异 存在差异
差异说明 鸿蒙平台独有接口,单独提供使用Demo,详见Demo

Demo

const { app, BrowserWindow, Tray, nativeImage, ipcMain, screen } = require('electron');
const path = require('path');

let tray;
let windows = [];
let childWindows = [];

function createWindows() {
  tray = new Tray(nativeImage.createFromPath(path.join(__dirname, 'electron_white.png')));
  const displays = screen.getAllDisplays();
  console.log('Electron Screen Test. 当前屏幕信息:');
  displays.forEach((display, index) => {
    console.log(`Electron Screen Test. ${index}:`, {
      displayId: display.id,
      bounds: display.bounds,
      size: display.size,
      scaleFactor: display.scaleFactor
    });
    let win;
    if(display.id == 0) {
      win = new BrowserWindow({
        displayId: display.id,
        width: 800,
        height: 600,
        webPreferences: {
          nodeIntegration: true,
          contextIsolation: false
        }
      });
    } else {
      win = new BrowserWindow({
        windowInfo: {
          type: 'floatWindow'
        },
        parent: windows[0],
        displayId: display.id,
        width: 400,
        height: 300,
        frame: false,
        webPreferences: {
          nodeIntegration: true,
          contextIsolation: false
        }
      });
    }

    win.loadURL('https://www.baidu.com');
    windows.push(win);
  });
}

// 监听渲染进程的右键点击
ipcMain.on('create-child-window', (event, { x, y }) => {
  const parentWin = BrowserWindow.fromWebContents(event.sender);

  // 转换为屏幕绝对坐标
  const [winX, winY] = parentWin.getPosition();
  const absX = winX + x;
  const absY = winY + y;

  let id = screen.getDisplayId();
  console.log('Electron Screen Test. 当前屏幕信息:', id);

  const child = new BrowserWindow({
    displayId: id,
    x: absX,
    y: absY,
    width: 200,
    height: 120,
    frame: true,
    alwaysOnTop: true,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false
    }
  });

  child.loadURL('https://www.baidu.com');
  childWindows.push(child);
});

app.whenReady().then(createWindows);