Building Desktop Applications with Electron

Building Desktop Applications with Electron body { font-family: sans-serif; margin: 0; padding: 0; } header { background-color: #f0f0f0; padding: 20px; text-align: center; } h1, h2, h3 { color: #333; } main { padding: 20px; } section { margin-bottom: 40px; } pre { background-color: #222; color: #fff; padding: 10px; overflow-x: auto; border-radius: 5px; } code { font-family: monospace; } .highlight { background-color: #f5f5f5; padding: 10px; border-radius: 5px; }

Building Desktop Applications with Electron

Introduction

Electron is a framework for building cross-platform desktop applications using web technologies like JavaScript, HTML, and CSS. This means you can leverage your existing web development skills to create native desktop apps that run on Windows, macOS, and Linux.

Getting Started

To get started with Electron, you'll need to install Node.js and npm (Node Package Manager). Once you have them installed, you can use the following command to create a new Electron project:


                npm init electron-app my-electron-app
            

This will create a new directory called "my-electron-app" with a basic Electron application structure.

Creating the Main Window

The main entry point for your Electron application is the "main.js" file. In this file, you'll create the main window and load your application's HTML content.


                const { app, BrowserWindow } = require('electron');

                function createWindow() {
                    const win = new BrowserWindow({
                        width: 800,
                        height: 600,
                        webPreferences: {
                            nodeIntegration: true
                        }
                    });

                    win.loadFile('index.html');
                }

                app.whenReady().then(createWindow);

                app.on('window-all-closed', () => {
                    if (process.platform !== 'darwin') {
                        app.quit();
                    }
                });

                app.on('activate', () => {
                    if (BrowserWindow.getAllWindows().length === 0) {
                        createWindow();
                    }
                });
            

Loading HTML Content

The "index.html" file is where you'll define the structure and content of your application's user interface.