首页 > 解决方案 > 如何在电子应用程序中设置 Windows 缩略图工具栏

问题描述

我正在尝试从其文档中在应用程序的任务栏布局中添加带有指定按钮的缩略图工具栏。但它显示出一些问题。

main.js:

// Modules to control application life and create native browser window
const {app, BrowserWindow, ipcMain, dialog} = require('electron')
const path = require('path')

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;

const isSecondInstance = app.makeSingleInstance((commandLine, workingDirectory) => {
  if (mainWindow) {
    if (mainWindow.isMinimized()) mainWindow.restore()
    mainWindow.focus()
  }
})
if (isSecondInstance) {
  app.quit();
}

function createWindow () {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    show: false,
    width: 500, 
    height: 200,
    minWidth: 500,
    minHeight: 200,
    transparent: true,
    frame: false,
  })
  mainWindow.setResizable(false)
  // and load the index.html of the app.
  mainWindow.loadFile('index.html')

  // Open the DevTools.
  // mainWindow.webContents.openDevTools()
  mainWindow.once('ready-to-show', () => {
      mainWindow.show()
      mainWindow.focus()
    })
  // Emitted when the window is closed.
  mainWindow.on('closed', function () {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null
  })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', function () {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', function () {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) {
    createWindow()
  }
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
ipcMain.on('open-information-dialog', (event) => {
      if (mainWindow) {
        const options = {
          type: 'info',
          title: 'I am abled',
          buttons: ['Ok'],
          message: 'This is the way.'
        }
        dialog.showMessageBox(mainWindow, options, function () {})
      }

})
ipcMain.on('close', (event) => {
  app.quit()
})
ipcMain.on('minimize', (event) => {
  mainWindow.minimize()
})

ipcMain.on('progress', (event, per) => {
    mainWindow.setProgressBar(per)
})

  mainWindow.setThumbarButtons([
    {
      tooltip: 'button1',
      icon: path.join(__dirname, 'button1.png'),
      click () { console.log('button1 clicked') }
    }, {
      tooltip: 'button2',
      icon: path.join(__dirname, 'button2.png'),
      flags: ['enabled', 'dismissonclick'],
      click () { console.log('button2 clicked.') }
    }
  ])

错误:

在此处输入图像描述

我所有的 mainWindow 参考工作正常,但是当我尝试设置拇指按钮时,setThumbarButtons()它就出现了问题。我只是试试这个,我没有收到任何错误,但它没有在 taskber 窗口上显示任何按钮, The Code:

app.on('ready', function(){
  console.log(mainWindow)
  mainWindow.setThumbarButtons([
    {
      tooltip: 'button1',
      icon: path.join(__dirname, 'start.png'),
      click () { console.log('button1 clicked') }
    }
  ])
})

我对这个问题没有任何意义。

标签: javascriptnode.jselectron

解决方案


问题是当你运行mainWindow.setThumbarButtons你的窗口时还没有创建。因此mainWindow未定义。您遇到的错误是什么,以及您发布的错误屏幕截图中所说的内容。

当前,您正在createWindow函数中创建和设置窗口。

如果您将setThumbarButtons代码移动到createWindow函数中,它应该可以工作。

类似于以下内容:

function createWindow () {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    show: false,
    width: 500, 
    height: 200,
    minWidth: 500,
    minHeight: 200,
    transparent: true,
    frame: false,
  })
  mainWindow.setResizable(false)
  // and load the index.html of the app.
  mainWindow.loadFile('index.html')

  mainWindow.setThumbarButtons([
    {
      tooltip: 'button1',
      icon: path.join(__dirname, 'start.png'),
      click () { console.log('button1 clicked') }
    }
  ])


  // Open the DevTools.
  // mainWindow.webContents.openDevTools()
  mainWindow.once('ready-to-show', () => {
      mainWindow.show()
      mainWindow.focus()
    })
  // Emitted when the window is closed.
  mainWindow.on('closed', function () {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null
  })
}

这样,您将创建第mainWindow一个并在调用该函数之前对其进行定义。否则mainWindow将是未定义的,这就是您所经历的。


推荐阅读