// main.js - Electron主进程文件
const { app, BrowserWindow } = require('electron');
const path = require('path');
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
});
win.loadFile('index.html');
}
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
// preload.js - 预加载脚本
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector);
if (element) element.innerText = text;
};
for (const type of ['chrome', 'node', 'electron']) {
replaceText(`${type}-version`, process.versions[type]);
}
});
// index.html - 渲染进程的HTML文件
<!DOCTYPE html>
<html>
<head>
<title>Electron App</title>
</head>
<body>
<h1>Hello from Electron!</h1>
<p>Chrome version: <span id="chrome-version"></span></p>
<p>Node.js version: <span id="node-version"></span></p>
<p>Electron version: <span id="electron-version"></span></p>
</body>
</html>
BrowserWindow
),并处理应用程序的生命周期事件。通过这些代码,你可以创建一个基本的Electron应用程序,它会打开一个窗口并显示Electron、Chrome和Node.js的版本信息。
上一篇:js 获取当前经纬度
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站