初始化
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
const { Menu, app, BrowserWindow, dialog } = require("electron");
|
||||
let fs = require('fs')
|
||||
const fontList = require("font-list");
|
||||
const path = require("path");
|
||||
const child_process = require("child_process");
|
||||
console.log(`Node.js: ${process.versions.node}`);
|
||||
require("@electron/remote/main").initialize();
|
||||
let preFilePath = "";
|
||||
app.on("will-finish-launching", () => {
|
||||
app.on("open-file", (e, filePath) => {
|
||||
preFilePath = filePath;
|
||||
});
|
||||
if (process.platform === "win32" && process.argv.length >= 2) {
|
||||
console.log("process argv:", process.argv);
|
||||
// windows系统当没有路径参数时这个位置默认有个.,需要加以判断
|
||||
preFilePath = process.argv[1] === "." ? "" : process.argv[1];
|
||||
}
|
||||
});
|
||||
const exePath = !app.isPackaged ? process.cwd() : path.dirname(process.execPath)
|
||||
console.log(exePath);
|
||||
function createWindow() {
|
||||
// Create the browser window.
|
||||
let size = require("electron").screen.getPrimaryDisplay().workAreaSize;
|
||||
//console.log(require('electron').screen.getPrimaryDisplay());
|
||||
let width = parseInt(size.width);
|
||||
var mainWindow;
|
||||
mainWindow = new BrowserWindow({
|
||||
show: false,
|
||||
width: size.width,
|
||||
height: size.height,
|
||||
resizable: false,
|
||||
icon: "public/images/favicon.ico", // sets window icon
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, "preload.js"),
|
||||
nodeIntegration: true,
|
||||
contextIsolation: false,
|
||||
enableRemoteModule: true,
|
||||
},
|
||||
});
|
||||
|
||||
mainWindow.maximize();
|
||||
// Menu.setApplicationMenu(null);//关闭菜单
|
||||
// and load the index.html of the app.
|
||||
//mainWindow.maximize();
|
||||
if (preFilePath) {
|
||||
let soonData = fs.readFileSync(preFilePath)
|
||||
let j = JSON.parse(soonData.toString());
|
||||
let type = j.soonType ? j.soonType : 1
|
||||
mainWindow.loadURL(`file://${__dirname}/design${type}.html?file=${preFilePath}&type=${type}`);
|
||||
} else {
|
||||
mainWindow.loadFile("index.html");
|
||||
}
|
||||
var close_flag = 0;
|
||||
mainWindow.on("close", function (e) {
|
||||
if (close_flag == 0) {
|
||||
e.preventDefault();
|
||||
mainWindow.webContents.send("close");
|
||||
}
|
||||
});
|
||||
mainWindow.show();
|
||||
// mainWindow.webContents.openDevTools({mode:'bottom'});
|
||||
//1150 width 720 height
|
||||
// Open the DevTools.
|
||||
//mainWindow.webContents.openDevTools()
|
||||
const { ipcMain } = require("electron");
|
||||
ipcMain.on("get-sys-fonts", (event) => {
|
||||
fontList
|
||||
.getFonts()
|
||||
.then((fonts) => {
|
||||
event.reply("font-list", fonts);
|
||||
})
|
||||
.catch((err) => {
|
||||
//console.log(err);
|
||||
});
|
||||
});
|
||||
ipcMain.on("get-sys-language", (event) => {
|
||||
event.reply("sys-lan", app.getLocale());
|
||||
});
|
||||
ipcMain.on("open-help-file", (event) => {
|
||||
let fullpath = path.join(exePath, 'help', 'User Manual.pdf')
|
||||
child_process.exec(`start "" "${fullpath}"`);
|
||||
});
|
||||
|
||||
// 监听从渲染进程发送来的请求
|
||||
ipcMain.on("print-pdf", (event, pdfData) => {
|
||||
// 打印 PDF 数据
|
||||
printPDF(pdfData);
|
||||
});
|
||||
|
||||
function printPDF(pdfData) {
|
||||
// 使用 Electron 的打印对话框或默认打印机进行打印
|
||||
// 创建一个隐藏的webview以加载PDF数据并进行打印
|
||||
const printWindow = new BrowserWindow({ show: true });
|
||||
printWindow.loadURL(pdfData);
|
||||
|
||||
printWindow.webContents.on("did-finish-load", () => {
|
||||
// 使用默认打印机打印PDF
|
||||
printWindow.webContents.print({});
|
||||
console.log("123")
|
||||
//printWindow.close();
|
||||
});
|
||||
}
|
||||
ipcMain.on("open-design-page", (event, arg, type) => {
|
||||
arg = arg ? arg : 'empty'
|
||||
type = type ? type : '1'
|
||||
mainWindow.loadURL(`file://${__dirname}/design${type}.html?file=${arg}&type=${type}`);
|
||||
});
|
||||
ipcMain.on("open-first-page", (event, arg) => {
|
||||
mainWindow.loadURL(`file://${__dirname}/index.html`);
|
||||
});
|
||||
ipcMain.on("run-close", (event, arg) => {
|
||||
close_flag = 1;
|
||||
app.quit();
|
||||
});
|
||||
|
||||
require("@electron/remote/main").enable(mainWindow.webContents);
|
||||
}
|
||||
|
||||
// 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.commandLine.appendSwitch("no-sandbox");
|
||||
app.whenReady().then(() => {
|
||||
createWindow();
|
||||
require("@electron/remote/main").initialize();
|
||||
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 (BrowserWindow.getAllWindows().length === 0) createWindow();
|
||||
});
|
||||
});
|
||||
|
||||
// Quit when all windows are closed, except on macOS. There, it's common
|
||||
// for applications and their menu bar to stay active until the user quits
|
||||
// explicitly with Cmd + Q.
|
||||
app.on("window-all-closed", function () {
|
||||
if (process.platform !== "darwin") app.quit();
|
||||
});
|
||||
|
||||
// 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.
|
||||
Reference in New Issue
Block a user