Electron生成exe文件,并生成快捷键

1、编译

npm i electron-packager -- save

在package中注入编译指令

"build": "electron-packager . safe --platform=win32 --arch=x64 --overwrite --icon=icon.ico --out=./out --asar --app-version=0.0.1 && node build.js"

node build.js 执行的是编译exe处理

2、生成exe文件

npm i electron-winstaller -- save

根目录下新建build.js

var electronInstaller = require('electron-winstaller');
var path = require("path");

resultPromise = electronInstaller.createWindowsInstaller({
    appDirectory: path.join('./out/safe-win32-x64'), // 编译的目录 看下package里面的配置
    outputDirectory: path.join('./out'), //输出路径
    authors: 'safe', // 软件著作信息
    exe: 'safe.exe', // 软件exe名称
    noMsi: true, // 是否生成msi文件
});

resultPromise.then(() => console.log("It worked!"), (e) => console.log(`No dice: ${e.message}`));

3、生成快捷键

npm i electron-squirrel-startup --save

在main.js中添加

// 生成快捷方式
if(require('electron-squirrel-startup')) return;

4、执行命令npm run build即可

main.js

// Modules to control application life and create native browser window
const {app, BrowserWindow, Menu} = 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;

function createWindow() {
    // Create the browser window.
    mainWindow = new BrowserWindow({
        width: 950,
        height: 750,
        show: false,
        fullscreen: true, // 默认全屏
        backgroundColor: '#999', // 北京色 优化启动白屏
        webPreferences: {
            nodeIntegration: true,
            preload: path.join(__dirname, 'preload.js')
        }
    });

    // and load the index.html of the app.
    mainWindow.loadFile('index.html');
    // 处理界面打开后短暂的白屏
    mainWindow.on('ready-to-show', function () {
        mainWindow.show();
    })

    // Open the DevTools.
    // mainWindow.webContents.openDevTools()

    // 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.webContents.openDevTools()
}


// 菜单设置
Menu.setApplicationMenu(Menu.buildFromTemplate([{
    label: '设置',
    submenu: [
        { role: 'reload', label: '刷新' },
        { role: 'zoomIn', label: '放大' },
        { role: 'zoomOut' , label: '缩小'},
        { role: 'resetZoom' , label: '重置'},
        { role: 'togglefullscreen',  label: '进入|退出全屏'}
    ]
}]));

const ipc = require('electron').ipcMain;
//接收
ipc.on('test', function (msg) {
    console.log(msg)
});

// 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 macOS 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 macOS 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.


// 开机自启动
if (app.isPackaged) {
    const exeName = path.basename(process.execPath);

    app.setLoginItemSettings({
        openAtLogin: true,
        openAsHidden:false,
        path: process.execPath,
        args: [
            '--processStart', `"${exeName}"`,
        ]
    });
}


// 生成快捷方式
if(require('electron-squirrel-startup')) return;
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值