【问题标题】:Changing the dialog box title in Electron在 Electron 中更改对话框标题
【发布时间】:2021-07-26 23:46:40
【问题描述】:

我在电子中使用 react,我有一个删除按钮,提示确认消息。我注意到确认框的标题为“electron.exe”,如下所示。

这里是代码:

 <td style={{textAlign:'center' }}><Link title="delete customer"><i className="material-icons" style={{ fontSize: "1.2em"}} title="Delete"
  onClick={(event) => {const confirmBox = window.confirm("Do you really want to delete this contact?")
 if (confirmBox === true) { handleDelete(event, record._id) }
 }}> delete</i></Link></td>

main.js

const {app, BrowserWindow,Menu, shell} = require('electron')
const server = require('../../server.js')
const isDev = require('electron-is-dev')

process.env.NODE_ENV = "development"

const isMac = process.platform === "darwin" ? true : false

let mainWindow

function createMainWindow (){
     mainWindow = new BrowserWindow({
        width:1366,
        height:768,
        show: false,
        backgroundColor:"#263238"
    })

    mainWindow.loadURL(isDev ? 'http://localhost:3000' : `file://${path.join(__dirname, '../build/index.html')}`)


    mainWindow.once('ready-to-show', () => {
        mainWindow.show();
        mainWindow.focus();
      });

    mainWindow.webContents.on('new-window', function(event, url){    
        event.preventDefault();  
        shell.openExternal(url);
    })
}

function createAboutWindow (){
    aboutWindow = new BrowserWindow({
       width:300,
       height:300,
       backgroundColor:"black"
      
   })

   aboutWindow.loadFile('http://localhost:3000/test')
}
 

app.on('ready', () => {
    createMainWindow()
    const mainMenu = Menu.buildFromTemplate(menu)
    Menu.setApplicationMenu(mainMenu)    
    mainWindow.on('ready', () => mainWindow = null)
})

const menu = [
    ...(isMac ? [{
        label: 'app.name',
        submenu:[
            {
                label:'About',
                click: createAboutWindow
            }
        ]}]:[]),
    {
        role:'fileMenu'
    },
    ...(!isMac ? [
        {
            label:'Help',
            submenu:[
                {
                    label:'About',
                    click: createAboutWindow
                }
            ]
        }
    ]:[]),
    ...(isDev ? [
        {
            label:'Developer',
            submenu:[
                {role:'reload'},
                {role:'forcereload'},
                {role:'separator'},
                {role:'toggledevtools'},
            ]
        }
    ]:[])
]

app.on('window-all-closed', function () {
    if (isMac) app.quit()
  })

  app.whenReady().then(() => {
        app.on('activate', function () {
      if (BrowserWindow.getAllWindows().length === 0) createMainWindow()
    })
  })

如何更改确认框中的标题?提前非常感谢,非常感谢任何帮助。再次感谢。

【问题讨论】:

    标签: reactjs electron


    【解决方案1】:

    您可以使用 Electron 的 dialog API,而不是使用 window.confirm,它为您提供了很多控制权。您可以自定义标题、按钮...等

    // in main.js
    const { ipcMain, dialog } = require('electron');
    
    ipcMain.handle('show-dialog', (event, dialogOptions) => {
      return dialog.showMessageBox(mainWindow, dialogOptions);
    });
    
    // in the renderer process (onClick handler). Although,
    // you might want to implement this in a preload script
    const { ipcRenderer } = require('electron');
    
    const showDialog = async (dialogOptions) => {
      const result = await ipcRenderer.invoke('show-dialog', dialogOptions);
      // handle result (which button the user clicked)
    }
    

    【讨论】:

      猜你喜欢
      • 2012-05-27
      • 1970-01-01
      • 2011-01-27
      • 2014-02-15
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多