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()
    }
})

效果展示

相关推荐
梦想的颜色11 分钟前
一天一个SKILL——前端最佳自动化测试 webapp-testing
前端·web app
SoaringHeart36 分钟前
Flutter进阶:放弃 MediaQuery.of(context) 使用 NScreenManager
前端·flutter
openKaka_1 小时前
从 scheduleUpdateOnFiber 到 Root 微任务调度:React 如何把更新交给调度系统
开发语言·前端·javascript
CoovallyAIHub1 小时前
铁路环境障碍物检测新框架:YOLOv11+MiDaS+LiDAR 深度融合,距离估计MAE低至0.63米
前端
C澒1 小时前
AI CR:前端团队代码审查规范及高频坑汇总
前端·ai·code review
盏灯1 小时前
以前有一个同事说:最讨厌下班提需求又没电脑在身边...
前端·后端·面试
LIO1 小时前
一文读懂 Vue 3:核心特性、组合式 API 与最佳实践
前端·vue.js
LIO1 小时前
前端响应式通用 CSS(Flex 为主,含主色调)
前端·css
前进的李工1 小时前
智能Agent实战指南:记忆组件嵌入技巧(记忆)
开发语言·前端·javascript·python·langchain·agent
西洼工作室1 小时前
B站登录流程全解析:RSA+极验验证
前端·python·极验