main.js
import { dialog } from "electron"
//弹出消息框
const openMsg = () => {
dialog.showMessageBox({
type:"info", //info, error, warning
//icon:"resource/images/code.ico", //自定义图标
title:"邓瑞编程",
message:"dengruicode.com",
detail:"网站"
})
}
//弹出确认框
const openConfirm = async () => {
const result = await dialog.showMessageBox({
type:"warning", //info, error, warning
icon:"resource/images/code.ico", //自定义图标
title:"邓瑞编程",
message:"确认删除?",
detail:"该记录删除后无法恢复~",
buttons:["确认","取消"]
})
//console.log(result)
if (result.response === 0) {
console.log("确认")
} else {
console.log("取消")
}
}
//当应用准备就绪后执行
app.whenReady().then(() => {
//openMsg() //弹出消息框
//openConfirm() //弹出确认框
})
//选择文件
ipcMain.handle("selectFile", async () => {
const result = await dialog.showOpenDialog({
icon:"resource/images/code.ico", //自定义图标
title:"打开",
//properties: ["multiSelections"] //允许多选
})
//console.log(result)
if (!result.canceled) { //如果用户没有点击取消
return result.filePaths
} else {
return null
}
})
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'">
<title>邓瑞编程</title>
<link rel="stylesheet" href="../css/base.css">
</head>
<body>
dengruicode.com <hr>
<button id="toggleTheme">切换主题</button>
<button id="selectFile">选择文件</button> <br>
<textarea id="selectedFile" style="width: 600px;height: 120px;"></textarea>
<script src="../js/index.js"></script>
</body>
</html>
index.js
//选择文件
document.querySelector('#selectFile').addEventListener('click', async () => {
let result = await DRAPI.invoke("selectFile")
console.log(result)
if(result){
document.querySelector("#selectedFile").value = result
}
})
12.对话框
-- 未经授权禁止转载 --