创建带有公共头部的Electron窗口

2024-05-26 12:52

本文主要是介绍创建带有公共头部的Electron窗口,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

创建带有公共头部的Electron窗口

创建一个公共头部的html文件

1.我们在项目根目录创建一个名为app-header的文件夹

2.在app-header创建一个文件名为header.html的文件

结构如下:

在这里插入图片描述

基本结构和脚本如下


<body>
<div class="header"><div class="left"><div class="back disabled" onclick="onBack()" id="backButton"><div class="backImg" id="backImgButton"></div></div><div class="forward disabled" onclick="onForward()" id="forwardButton"><div class="forwardImg" id="forwardImgButton"></div></div><div class="refresh" onclick="onRefresh()"><div class="refreshImg"><svg>...</svg></div></div></div><div class="center" id="centerName"></div><div class="right"><div class="close" onclick="onCloseWindow()"><svg>...<svg></div></div>
</div>
<div class="lodings" id="lodings"><svg width="50%" height="50%" viewBox="0 0 50 50">...</svg>
</div>
</body></html>
<script>function onBack() {window.electron.app.switchWindow(1);}function onForward() {window.electron.app.switchWindow(2);}function onRefresh() {window.electron.app.switchWindow(3);}//监听是否路由被改变 修改图标和点击状态window.electron.app.OnUpdateNavigationButtons((_event, args) => {if (args.canGoBack) {document.getElementById('backButton').classList.remove('disabled');document.getElementById('backImgButton').style.backgroundImage = 'url("./img/left_arrow.png")';} else {document.getElementById('backButton').classList.add('disabled');document.getElementById('backImgButton').style.backgroundImage = 'url("./img/left_arrow_tint.png")';}if (args.canGoForward) {document.getElementById('forwardButton').classList.remove('disabled');document.getElementById('forwardImgButton').style.backgroundImage = 'url("./img/right_arrow.png")';} else {document.getElementById('forwardButton').classList.add('disabled');document.getElementById('forwardImgButton').style.backgroundImage = 'url("./img/right_arrow_tint.png")';}})function onCloseWindow(){window.close()}
</script>
<style>/*省略
*/
</style>

结果运行如下:

在这里插入图片描述

创建broseWindow并嵌入broseView

根据应用传入的窗口大小,创建对应的broseWindow,并且引入公共头部文件

    let options = JSON.parse(JSON.stringify(args.options))// 窗口最小宽高if (!options.minWidth) {options.minWidth = options.width}if (!options.minHeight) {options.minHeight = options.height}options.show = false//隐藏工具栏options.autoHideMenuBar = trueoptions.frame = falseif (!options.webPreferences) {options.webPreferences = {}}options.webPreferences = Object.assign(options.webPreferences, {preload: path.join(app.getAppPath(), 'preload.js'),// preload: '../screenShotApp/app-header/header.html',webSecurity: false,})const win = new BrowserWindow(options)win.loadFile('../screenShotApp/app-header/header.html')

然后把要打开的页面放进broseView中

//应用本体const view: View = new BrowserView({webPreferences: {preload: path.join(app.getAppPath(), 'preload.js'),webSecurity: false,},})// 禁止使用 ctrl+r、  F5 等刷新win.webContents.on('before-input-event', (event, input) => {if ((input.control || input.meta) && input.key === 'r') {event.preventDefault();}if (input.key === 'f5') {event.preventDefault();}})view.webContents.loadURL('打开的页面地址')

将创建好的broseView添加到公共的broseWindows中,创建后的broseView 需要header头下偏移,不然会覆盖掉头部样式

 // view.setBounds({ x: 0, y: 45, width: options.width, height: options.height })win.on('ready-to-show', () => {win.show()win.addBrowserView(view);const bounds = win.getBounds()view.setBounds({ x: 0, y: 40, width: bounds.width, height: bounds.height - 35 })})win?.on('resize', () => {const bounds = win.getBounds()view.setBounds({ x: 0, y: 40, width: bounds.width, height: bounds.height - 35 })})win?.on('close', () => {view.webContents.close();win?.close();})

监听路由是否发生变化 改变左上角图标的状态

    // 监听子窗口路由变化view.webContents.on('did-navigate-in-page', (event, url) => {const canGoForward = view.webContents.canGoForward();const canGoBack = view.webContents.canGoBack();win.webContents?.send(channel.appUpdateNavigationButtons, { canGoForward, canGoBack, args });});BrowserWindow.getAllWindows()BrowserWindow.getFocusedWindow()

结果如下:

在这里插入图片描述

总结

本文通过将公共头部的HTML代码分离到一个独立的文件中,可以方便地在多个窗口之间共享和重用头部代码。这样的结构有助于保持代码的整洁和可维护性,同时使得界面设计更加一致。

– 欢迎点赞、关注、转发、收藏【我码玄黄】,gonghao同名

这篇关于创建带有公共头部的Electron窗口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1004525

相关文章

ESP32 esp-idf esp-adf环境安装及.a库创建与编译

简介 ESP32 功能丰富的 Wi-Fi & 蓝牙 MCU, 适用于多样的物联网应用。使用freertos操作系统。 ESP-IDF 官方物联网开发框架。 ESP-ADF 官方音频开发框架。 文档参照 https://espressif-docs.readthedocs-hosted.com/projects/esp-adf/zh-cn/latest/get-started/index

公共筛选组件(二次封装antd)支持代码提示

如果项目是基于antd组件库为基础搭建,可使用此公共筛选组件 使用到的库 npm i antdnpm i lodash-esnpm i @types/lodash-es -D /components/CommonSearch index.tsx import React from 'react';import { Button, Card, Form } from 'antd'

剑指offer(C++)--两个链表的第一个公共结点

题目 输入两个链表,找出它们的第一个公共结点。 解法一 两个链表一定有交点的话,方法是指向短链表指针先走完,然后指向长链表,指向长链表指针后走完,指向短链表。所以,第二次走过,一定会在交点相遇。 class Solution {public:ListNode* FindFirstCommonNode( ListNode *pHead1, ListNode *pHead2) {ListN

vscode-创建vue3项目-修改暗黑主题-常见错误-element插件标签-用法涉及问题

文章目录 1.vscode创建运行编译vue3项目2.添加项目资源3.添加element-plus元素4.修改为暗黑主题4.1.在main.js主文件中引入暗黑样式4.2.添加自定义样式文件4.3.html页面html标签添加样式 5.常见错误5.1.未使用变量5.2.关闭typescript检查5.3.调试器支持5.4.允许未到达代码和未定义代码 6.element常用标签6.1.下拉列表

【Java算法】滑动窗口 下

​ ​    🔥个人主页: 中草药 🔥专栏:【算法工作坊】算法实战揭秘 🦌一.水果成篮 题目链接:904.水果成篮 ​ 算法原理 算法原理是使用“滑动窗口”(Sliding Window)策略,结合哈希表(Map)来高效地统计窗口内不同水果的种类数量。以下是详细分析: 初始化:创建一个空的哈希表 map 用来存储每种水果的数量,初始化左右指针 left

【Qt6.3 基础教程 17】 Qt布局管理详解:创建直观和响应式UI界面

文章目录 前言布局管理的基础为什么需要布局管理器? 盒布局:水平和垂直排列小部件示例:创建水平盒布局 栅格布局:在网格中对齐小部件示例:创建栅格布局 表单布局:为表单创建标签和字段示例:创建表单布局 调整空间和伸缩性示例:增加弹性空间 总结 前言 当您开始使用Qt设计用户界面(UI)时,理解布局管理是至关重要的。布局管理不仅关系到UI的外观,更直接影响用户交互的体验。本篇博

3_创建Tab控件

1,新建MFC 对话框项目,为对话框添加Tab控件,选中Tab控件,新建控件变量m_tab_ctrl 2,为Tab控件添加tab项 m_tab_ctrl.InsertItem(0, L”000”),参数1,哪个位置;参数2,item的名称 3,为Tab控件添加监听事件, void C测试Dlg::OnTcnSelchangeTab1(NMHDR *pNMHDR, LRESUL

Java NIO 创建/复制缓冲区

创建缓冲区的方式 主要有以下两种方式创建缓冲区: 1、调用allocate方法 2、调用wrap方法 我们将以charBuffer为例,阐述各个方法的含义; allocate方法创建缓冲区 调用allocate方法实际上会返回new HeapCharBuffer(capacity, capacity)对象; 缓存空间存储在CharBuffer类的成员属性char[] h

spring mvc完整项目创建步骤记录

快速创建一个spring mvc项目(只有页面调用→到controller→到页面) 1、首先创建Dynamic Web Project 2、创建jsp页面index.jsp以及成功(/WEB-INF/view/success.jsp)和失败页面(/WEB-INF/view/error.jsp) index.jsp <%@ page language="java" contentType=

poj 1330 LCA 最近公共祖先

水题目。直接上代码了。 VIEW CODE #include<cstdio>#include<algorithm>#include<iostream>#include<cmath>#include<queue>#include<stack>#include<string>#include<cstring>#include<map>#include<vector>#