BrowserWindow - win.getNativeWindowHandle()

概述

获取底层操作系统的窗口句柄 Buffer。

基本信息

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

OHOS 依赖与基本表现

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

差异说明

属性
是否存在差异 存在差异
差异说明 getNativeWindowHandle 返回句柄类型在鸿蒙下并非 HWND/NSView/XWindow,跨平台调用方需适配

Demo

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

let mainWindow = null;

function sendLog(type, message) {
  if (mainWindow && !mainWindow.isDestroyed()) {
    mainWindow.webContents.send('log-message', { type, message, time: new Date().toLocaleTimeString() });
  }
}

app.whenReady().then(() => {
  mainWindow = new BrowserWindow({
    width: 1400, height: 900,
    webPreferences: { preload: path.join(__dirname, '..', 'preload.js'), contextIsolation: true, nodeIntegration: false },
    title: 'method-getNativeWindowHandle',
  });
  mainWindow.loadFile(path.join(__dirname, '..', 'index.html'));

  ipcMain.handle('method:getNativeWindowHandle', () => {
    const handle = mainWindow.getNativeWindowHandle();
    return { type: 'Buffer', length: handle.length, hex: handle.toString('hex') };
  });
});

app.on('window-all-closed', () => { app.quit(); });