[electron]窗口 BrowserWindow

优雅的显示窗口

javascript 复制代码
const {app, BrowserWindow} = require('electron');

function createMainwindow(){
    const mainwindow = new BrowserWindow({
        x: 300,
        y: 400,
        width: 600,
        height: 600,
    });
    mainwindow.loadFile('index.html');
}

app.on('ready', ()=>{
    createMainwindow();
});

对于这样的代码,出现的一个情况就是。当窗口创建好了,等了一会界面的内容才出来。这是因为 BrowserWindow 默认就是直接显示的。所以可以使用如下方法

javascript 复制代码
const {app, BrowserWindow} = require('electron');

function createMainwindow(){
    const mainwindow = new BrowserWindow({
        x: 300,
        y: 400,
        width: 600,
        height: 600,
        show: false,
    });
    mainwindow.loadFile('index.html');
    mainwindow.on('ready-to-show', ()=>{
        mainwindow.show();
    });
    
}

app.on('ready', ()=>{
    createMainwindow();
});

BrowserWinodw 的其它属性

BrowserWindow属性集合

自定义标题栏程序

官网介绍
主要代码

html 复制代码
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="index.css">
</head>
<body>
    <div id="titleBar">
        <input type="button" id="btn" value="退出程序">
    </div>
    life is fucking movie
    <script src="index.js"></script>
</body>
</html>
css 复制代码
*{
    margin: 0px;
    padding: 0px;
}
#titleBar{
    height: 40px;
    border: 1px solid pink;
    -webkit-app-region: drag;
}
#btn{
    -webkit-app-region: no-drag;
}

就是这么简单而且能够实现拖拽全屏半屏1/4屏的效果

取消electron自带的菜单栏

javascript 复制代码
const {app, BrowserWindow, ipcMain, Menu} = require('electron');

function createMainwindow(){
    const mainwindow = new BrowserWindow({
        x: 300,
        y: 400,
        width: 600,
        height: 600,
        show: false,
        webPreferences: {
            preload: 'F:/electron/test2/preload.js'
        }
    });
    Menu.setApplicationMenu(null);
    mainwindow.loadFile('index.html');
    mainwindow.on('ready-to-show', ()=>{
        mainwindow.show();
    });
}


app.on('ready', ()=>{
    createMainwindow();
});

ipcMain.on('quit', ()=>{
    app.quit();
})

使用 Menu.setApplicationMenu(null); 即可

相关推荐
frontend_frank1 天前
脱离 Electron autoUpdater:uni-app跨端更新:Windows+Android统一实现方案
android·前端·javascript·electron·uni-app
cn_mengbei1 天前
鸿蒙PC原生应用开发避坑指南:Qt 6.6与Electron 28兼容性问题全解析
qt·electron·harmonyos
cn_mengbei1 天前
鸿蒙PC跨端开发实战:从Qt环境配置到Electron应用鸿蒙化的完整指南
qt·electron·harmonyos
呆头鸭L1 天前
用vue3+ts+elementPlus+vite搭建electron桌面端应用
前端·vue.js·electron
musashi2 天前
用 Electron 写了一个 macOS 版本的 wallpaper(附源码、下载地址)
前端·vue.js·electron
叶落无痕522 天前
Electron应用自动化测试实例
前端·javascript·功能测试·测试工具·electron·单元测试
卡布叻_星星2 天前
Vue3+Vite+Electron实现开发桌面应用
electron
ChangYan.2 天前
Electron使用ffi-napi报错External buffers are not allowed解决办法
前端·javascript·electron
薛定谔的猫-菜鸟程序员3 天前
从零到一:用Electron打造专业的Markdown转Word桌面应用。
javascript·electron·word
PBitW3 天前
Electron 脚本调用大坑!害惨我了
前端·electron