這篇文章帶給大家的內容是關於使用electron怎麼實現網盤的懸浮視窗?有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
相關依賴
裡面使用了vuex vue vue-route storeJs
storeJs 用來持久化vuex狀態
#展示
介紹說明
沒有使用electron內建的-webkit-app-region: drag 因為使用他那個有很多問題例如事件無法使用右鍵無法使用以及不能使用手型等!
安裝
安裝的時候沒有截圖所以就參考下其他的文章吧storeJs 安裝
npm install storejs
準備寫程式碼
設定路由檔案
export default new Router({ routes: [ {path: '/', name: 'home', component: ()=> import('@/view//home')}, {path: '/suspension', name: 'suspension', component: ()=> import('@/view/components/suspension')} ] })
寫入懸浮窗頁面
頁面路徑/src/renderer/view/components/suspension.vue
<template> <div> <div></div> <div> <div>拖拽上传</div> </div> </div> </template> <script> export default { name: "suspension", mounted() { let win = this.$electron.remote.getCurrentWindow(); let biasX = 0; let biasY = 0; let that = this; document.addEventListener('mousedown', function (e) { switch (e.button) { case 0: biasX = e.x; biasY = e.y; document.addEventListener('mousemove', moveEvent); break; case 2: that.$electron.ipcRenderer.send('createSuspensionMenu'); break; } }); document.addEventListener('mouseup', function () { biasX = 0; biasY = 0; document.removeEventListener('mousemove', moveEvent) }); function moveEvent(e) { win.setPosition(e.screenX - biasX, e.screenY - biasY) } } } </script> <style> * { padding: 0; margin: 0; } .upload { height: 25px; line-height: 25px; font-size: 12px; text-align: center; color: #74A1FA; } .logo { width: 40px; background: #5B9BFE url("../../assets/img/logo@2x.png") no-repeat 2px 3px; background-size: 80%; } .content_body { background-color: #EEF4FE; width: 100%; } #suspension { -webkit-user-select: none; cursor: pointer; overflow: hidden; } #suspension { cursor: pointer !important; height: 25px; border-radius: 4px; display: flex; border: 1px solid #3388FE; } </style>
主程序建立懸浮窗頁面程式碼
路徑: /src/main/window.jsimport {BrowserWindow, ipcMain, screen, Menu, shell, app, webContents} from 'electron' var win = null; const window = BrowserWindow.fromWebContents(webContents.getFocusedWebContents()); const winURL = process.env.NODE_ENV === 'development' ? `http://localhost:9080/#/suspension` : `file://${__dirname}/index.html/#/suspension`; ipcMain.on('showSuspensionWindow', () => { if (win) { if (win.isVisible()) { createSuspensionWindow(); } else { win.showInactive(); } } else { createSuspensionWindow(); } }); ipcMain.on('createSuspensionMenu', (e) => { const rightM = Menu.buildFromTemplate([ {label: '开始全部任务', enabled: false}, {label: '暂停全部任务', enabled: false}, {label: '本次传输完自动关机'}, {type: 'separator'}, { label: '隐藏悬浮窗', click: () => { window.webContents.send('hideSuspension', false); win.hide() } }, {type: 'separator'}, { label: '加入qq群', click: () => { shell.openExternal('tencent://groupwpa/?subcmd=all¶m=7B2267726F757055696E223A3831343237303636392C2274696D655374616D70223A313533393531303138387D0A'); } }, { label: 'GitHub地址', click: () => { shell.openExternal('https://github.com/lihaotian0607/auth'); } }, { label: '退出软件', click: () => { app.quit(); } }, ]); rightM.popup({}); }); function createSuspensionWindow() { win = new BrowserWindow({ width: 107, //悬浮窗口的宽度 比实际p的宽度要多2px 因为有1px的边框 height: 27, //悬浮窗口的高度 比实际p的高度要多2px 因为有1px的边框 type: 'toolbar', //创建的窗口类型为工具栏窗口 frame: false, //要创建无边框窗口 resizable: false, //禁止窗口大小缩放 show: false, //先不让窗口显示 webPreferences: { devTools: false //关闭调试工具 }, transparent: true, //设置透明 alwaysOnTop: true, //窗口是否总是显示在其他窗口之前 }); const size = screen.getPrimaryDisplay().workAreaSize; //获取显示器的宽高 const winSize = win.getSize(); //获取窗口宽高 //设置窗口的位置 注意x轴要桌面的宽度 - 窗口的宽度 win.setPosition(size.width - winSize[0], 100); win.loadURL(winURL); win.once('ready-to-show', () => { win.show() }); win.on('close', () => { win = null; }) } ipcMain.on('hideSuspensionWindow', () => { if (win) { win.hide(); } });store檔案
import storejs from 'storejs' const state = { show: storejs.get('showSuspension') }; const actions = { showSuspension: function ({state, commit}) { let status = true; storejs.set('showSuspension', status); state.show = status; }, hideSuspension: function ({state, commit}) { let status = false; storejs.set('showSuspension', status); state.show = status; }, }; export default ({ state, actions });版權說明
##裡面使用的百度的圖示以及UI作為學習使用,請不要作為商業用途!
遺留問題
以上是使用electron怎麼實現網盤的懸浮視窗?的詳細內容。更多資訊請關注PHP中文網其他相關文章!