• electron 创建圆角窗口附带阴影效果


    窗口属性的设置

    1. win = new BrowserWindow({
    2. width: 520,
    3. height: 320,
    4. transparent: true,
    5. backgroundColor: '#00000000',
    6. frame: false,
    7. resizable: false
    8. })

    因为electron创建的主窗口既不能倒圆角,也无法添加阴影,我曾经想从这里突破,查看BrowserWindow属性文档后发现没有这些属性。所以要想有圆角,BrowserWindow的主窗口就必须消失,设置transparent: true,即主窗口透明,不让它碍事。backgroundColor: '#00000000',背景色也需要透明。然后frame: false,先得无边框才能有圆角。这两项是必须的,圆角只能通过操作CSS才能得到。

    vue页面如下

    1. <template>
    2. <div class="shadow">
    3. <div id="win">这是自定义窗口和添加阴影特效div>
    4. div>
    5. template>
    6. <script>
    7. export default {};
    8. script>
    9. <style lang="scss" scoped>
    10. .shadow {
    11. body {
    12. background-color: rgba(0, 0, 0, 0);
    13. }
    14. #win {
    15. width: 500px;
    16. height: 300px;
    17. text-align: center;
    18. background-color: #1fa431;
    19. border-radius: 30px;
    20. box-shadow: 8px 8px 10px grey;
    21. -webkit-app-region: drag;
    22. }
    23. }
    24. style>

    background.js

    1. 'use strict'
    2. import { app, protocol, BrowserWindow } from 'electron'
    3. import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
    4. import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
    5. // Scheme must be registered before the app is ready
    6. protocol.registerSchemesAsPrivileged([
    7. { scheme: 'app', privileges: { secure: true, standard: true } }
    8. ])
    9. //系统托盘
    10. const path = require('path')
    11. //打开新窗口的路径
    12. const winURL = process.env.NODE_ENV === 'development' ? `http://localhost:8080` : `file://${__dirname}/index.html`
    13. async function createWindow() {
    14. const win = new BrowserWindow({
    15. width: 520,
    16. height: 320,
    17. transparent: true,
    18. backgroundColor: '#00000000',
    19. frame: false,
    20. resizable: false,
    21. icon: path.join(__static, "./static/logo.ico"),
    22. webPreferences: {
    23. devTools: true,//关闭开发者工具
    24. nodeIntegration: true,//开启node模块
    25. enableRemoteModule: true, // 使用remote模块 electron12版本之后废除了,需要自己安装
    26. contextIsolation: false,
    27. //解决axios跨域请求 不推荐,不安全,但简单好用
    28. webSecurity: false,
    29. },
    30. })
    31. win.loadURL(winURL + "#/shadow");
    32. win.once('ready-to-show', () => {
    33. // 初始化后再显示
    34. win.show()
    35. })
    36. if (process.env.WEBPACK_DEV_SERVER_URL) {
    37. // Load the url of the dev server if in development mode
    38. await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
    39. // await win3.loadURL(process.env.WEBPACK_DEV_SERVER_URL + '#/chat_index')
    40. // await win2.loadURL(process.env.WEBPACK_DEV_SERVER_URL + '#/test')
    41. if (!process.env.IS_TEST) win.webContents.openDevTools()
    42. } else {
    43. createProtocol('app')
    44. // Load the index.html when not in development
    45. win.loadURL('app://./index.html')
    46. }
    47. }
    48. // Quit when all windows are closed.
    49. app.on('window-all-closed', () => {
    50. // On macOS it is common for applications and their menu bar
    51. // to stay active until the user quits explicitly with Cmd + Q
    52. if (process.platform !== 'darwin') {
    53. app.quit()
    54. }
    55. })
    56. app.on('activate', () => {
    57. // On macOS it's common to re-create a window in the app when the
    58. // dock icon is clicked and there are no other windows open.
    59. if (BrowserWindow.getAllWindows().length === 0) createWindow()
    60. })
    61. // This method will be called when Electron has finished
    62. // initialization and is ready to create browser windows.
    63. // Some APIs can only be used after this event occurs.
    64. app.once('ready-to-show', () => {
    65. win.show();
    66. })
    67. app.on('ready', async () => {
    68. if (isDevelopment && !process.env.IS_TEST) {
    69. // Install Vue Devtools
    70. try {
    71. await installExtension(VUEJS_DEVTOOLS)
    72. } catch (e) {
    73. console.error('Vue Devtools failed to install:', e.toString())
    74. }
    75. }
    76. createWindow()
    77. })
    78. // Exit cleanly on request from parent process in development mode.
    79. if (isDevelopment) {
    80. if (process.platform === 'win32') {
    81. process.on('message', (data) => {
    82. if (data === 'graceful-exit') {
    83. app.quit()
    84. }
    85. })
    86. } else {
    87. process.on('SIGTERM', () => {
    88. app.quit()
    89. })
    90. }
    91. }

    效果图

    添加内容之后的效果图

     

  • 相关阅读:
    【Vue 组件化开发 二 】注册组件的语法糖、组件模板的分离写法、组件的数据
    (编程语言界的丐帮 C#).NET MD5 HASH 哈希 加密 与JAVA 互通
    【MATLAB教程案例39】语音信号的PCM编解码matlab仿真学习
    vue设计原理-带你重走vue诞生路程
    【C++】不用include,使用C++模块语法实现函数调用
    RabbitMQ项目实战(一)
    五子棋-牛客网
    GO 语言的函数??
    Timed out waiting for device dev-ttymxc3.device.
    hive分桶表创建教程
  • 原文地址:https://blog.csdn.net/weixin_46171419/article/details/126752735