首页 > 其他分享 >electron-vite_4使用WebContentsView快速集成已有项目

electron-vite_4使用WebContentsView快速集成已有项目

时间:2024-10-12 08:49:32浏览次数:3  
标签:index view1 height width electron WebContentsView mainWindow vite

Web 嵌入官方推荐使用WebContentsView;集成也比较简单,但还是需要你单独写点东西;

src/main/index.ts进行修改

在这里插入图片描述

import { app, 
shell, BrowserWindow, 
ipcMain, nativeImage, WebContentsView, dialog } from 'electron';

function createWindow(): void {
  // 1.创建 browser window.
  const mainWindow = new BrowserWindow({
    width: 900,
    height: 670,
    show: false,
    autoHideMenuBar: true,
    ...(process.platform === 'linux' ? { icon: appIcon } : {}),
    transparent: false,
    frame: true,
    resizable: true,
    webPreferences: {
      preload: join(__dirname, '../preload/index.js'),
      sandbox: false
    }
  })
	// 2.创建WebContentsView
  const view1 = new WebContentsView()
  // 3. 添加给mainWindow 
  mainWindow.contentView.addChildView(view1)
  // 4.知道要加载的线上url最好是https
  view1.webContents.loadURL('https://baidu.com')
  // 5.指定显示位置;x,y是偏移;这里选择0;width和height和mainWindow保持一致
  view1.setBounds({ x: 0, y: 0, width: 900, height: 670});
  // 染进程第一次完成绘制时
  mainWindow.on('ready-to-show', () => {
  	// 6-1.关闭谷歌调试工具
    mainWindow.webContents.closeDevTools();
    // 6-2.当页面发生改变的时候重新指定显示位置
    mainWindow.on('resize', () => {  
      // 获取mainWindow宽高
      const [width, height] = mainWindow.getContentSize();
      view1.setBounds({ x: 0, y: 0, width, height});  
    });
    mainWindow.show()
  })

  mainWindow.webContents.setWindowOpenHandler((details) => {
    shell.openExternal(details.url)
    return { action: 'deny' }
  })

  // HMR for renderer base on electron-vite cli.
  // Load the remote URL for development or the local html file for production.
  if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
    mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
  } else {
  	// 7.注意这里一定要加载index.html
    mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
  }
}
renderer/index.html

注意修改title, 比方说‘微信’、‘钉钉’、‘小红书’、‘有道云’

<!DOCTYPE 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>百度</title>
</head>
<body>
</body>
</html>

标签:index,view1,height,width,electron,WebContentsView,mainWindow,vite
From: https://blog.csdn.net/u014708123/article/details/142828461

相关文章

  • electron-vite_6js-cookie失效
    我们项目是用了js-cookie,后续集成的时候发现,无法进入首页;经过排查是js-cookie无法使用,可能是electron打包后的项目运行的时候是file:///猜测原因:因为Cookie是与域名相关联的,而file:///协议没有域名,因此Cookie可能无法正常工作。file:///C:/Users/Administrator/AppData/......
  • vue3--vite环境变量的配置
    在开发环境、测试环境、生产环境需要请求不同的接口,对应不同的数据。因此,配置环境变量也尤为重要第一:为项目根目录开发、测试、生成(也可以加入预发布)环境创建.env文件.env.development.env.production.env.test第二:配置数据#变量必须以VITE_为前缀才能暴露给外部读取......
  • Vite和Wabpack进行打包项目
    问题:首先,咱们为什么要打包?答案:打包(Packaging)是软件开发中的一个重要步骤,主要目的是将开发好的代码和依赖项打包成一个可分发和运行的格式。关键原因:依赖管理、环境隔离、便于分发、版本控制、安全性、性能优化、部署简化在不同的编程语言和框架中,打包工具和方法可能会有所不......
  • 运行使用Electron-forge打包的electron package时遇到在js文件中执行的exec命令和在渲
    js文件中执行的exec命令出错很可能是项目中使用了一些非html,css,js的源文件,比如用了Makefile来编译了cpp代码,或者执行的exec命令为cpdir/something.cpp之类的文件操作命令。可以使用修改forge.config.js文件配置的方式,使得npmrunmake的时候自动把Makefile等exec命令中用到......
  • 从零搭建Cesium+vue3+vite
    介绍在现代前端开发中,Cesium是一个功能强大的WebGL库,广泛应用于3D地球可视化、空间数据展示等领域。结合Vue3的组件化开发模式,我们可以创建一个高效且可维护的3D地图应用。本文将带你一步步搭建一个简单的Cesium+Vue3项目,并且对各个功能模块进行细致划分,帮助你快......
  • ELX304 – Electronic Systems
    ELX304–ElectronicSystemsIndividualCourseworkAssignmentDigitalDesignSUBMISSIONONLINEon13/10/2024viaCANVASIntroductionThiscourseworkexercisewillprovideyouwiththeopportunitytodemonstratetheskillsyouhavedevelopedthroughout......
  • [Electron] 应用不关闭窗口退出而是保留到后台运行
    import{app,BrowserWindow,Tray,Menu}from"electron";import{fileURLToPath}from"url";importpathfrom"path";const__filename=fileURLToPath(import.meta.url);const__dirname=path.dirname(__filename);lettray......
  • [Electron] 搭建 Vite+Electron 项目
    安装搭建Vite项目(根据官方文档搭建),安装electron、nodemon。pnpminstallelectronnodemon-D配置electron/main.jsfile:[electron/main.js]import{app,BrowserWindow}from"electron";constcreateWindow=()=>{constwin=newBrowserWindow({wid......
  • vite 快速入门指南
    相关链接演示地址源码地址vite官网地址Vite是什么Vite是由EvanYou(Vue.js创始人)开发的现代前端构建工具,专为提升开发体验而设计。它通过创新的开发模式和高效的构建流程,极大提高了开发效率,尤其在处理大型项目和复杂的前端框架时,表现出色。Vite的核心特点包括:......
  • vite5+tauri2.0+vue3+rust桌面exe聊天系统演示
    Tauri2.0-Vue3Chat:自研基于tauri2.0+rust+vue3setup+pinia2+element-plus等技术搭建的一款高颜值仿QQ/微信客户端聊天EXE软件。整个聊天窗口采用自定义无边框透明圆角阴影UI模式。tauri2.0-vitechat:自研vue3+tauri2+element-plus客户端聊天程序项目特点整体窗口采......