electron dialog.showMessageBox使用案例

electron 版本:25.3.1

index.html

html 复制代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
    <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';"/>
</head>
<body>
<h1>Hello World!</h1>
<button id="sad">点我弹出消息对话框</button>
<script>
    const {ipcRenderer} = require('electron')
    document.querySelector('#sad').addEventListener('click', () => {
        ipcRenderer.send('open-message-dialog')
    })
</script>
</body>
</html>

main.js

js 复制代码
const {app, BrowserWindow, ipcMain, dialog} = require('electron')

function createWindow() {
    let win = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false
        }
    })

    win.loadFile('index.html')

    ipcMain.on('open-message-dialog', () => {
        dialog.showMessageBox({
            title: '消息',
            message: '这是一个消息框。',
            type: 'info',
            buttons: ['确定', '取消']
        })
    })
}

app.whenReady().then(createWindow)


app.on('window-all-closed', function () {
    if (process.platform !== 'darwin') app.quit()
})

app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
        createWindow()
    }
})

效果展示

相关推荐
杨航 AI8 小时前
JEV的用法
java·前端·数据库
Amos_Web8 小时前
Rspack 源码解析(三十五):rspack_error 如何用 miette 渲染带源码定位的错误链
前端
修炼的dance8 小时前
OCR预处理测了108组,纠斜去噪有用,坑在顺序和力度
前端
独隅9 小时前
WebStorm Windows 版 React / Next.js 实战替换指南
javascript·windows·webstorm
冷眸_9 小时前
同名食材不能直接相减:一个 JavaScript 用量模型的边界
javascript
不可能片场9 小时前
AI视频成片 帧间差分把拼接缝照出来
前端·electron
写的都是BUG9 小时前
APK 里的钥匙,一直就放在门口:聊聊加固真正该做什么
前端·算法
dpharness9 小时前
我踩过的四个记忆坑,从预算到换机
前端·后端
apollo_qwe9 小时前
接口超时了,前端为什么不能直接重试?
javascript·http
用户7783366132119 小时前
SerpBase驱动:用原生JS实现搜索意图分流浮层
前端