使用electron實(shí)現(xiàn)百度網(wǎng)盤懸浮窗口功能的示例代碼
相關(guān)依賴
里面使用了vuex vue vue-route storeJs
storeJs 用來持久化vuex狀態(tài)
展示


介紹說明
沒有使用electron內(nèi)置的-webkit-app-region: drag 因?yàn)槭褂盟莻€(gè)有很多問題
比如事件無法使用 右鍵無法使用 以及不能使用手型等!
安裝
安裝的時(shí)候沒有截圖 所以就參考下我其他的文章吧
storeJs 安裝
npm install storejs
準(zhǔn)備寫代碼
配置路由文件
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 id="suspension">
<div class="logo"></div>
<div class="content_body">
<div class="upload">拖拽上傳</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>
主進(jìn)程創(chuàng)建懸浮窗頁面代碼
路徑: /src/main/window.js
import {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: '開始全部任務(wù)', enabled: false},
{label: '暫停全部任務(wù)', enabled: false},
{label: '本次傳輸完自動(dòng)關(guān)機(jī)'},
{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, //懸浮窗口的寬度 比實(shí)際DIV的寬度要多2px 因?yàn)橛?px的邊框
height: 27, //懸浮窗口的高度 比實(shí)際DIV的高度要多2px 因?yàn)橛?px的邊框
type: 'toolbar', //創(chuàng)建的窗口類型為工具欄窗口
frame: false, //要?jiǎng)?chuàng)建無邊框窗口
resizable: false, //禁止窗口大小縮放
show: false, //先不讓窗口顯示
webPreferences: {
devTools: false //關(guān)閉調(diào)試工具
},
transparent: true, //設(shè)置透明
alwaysOnTop: true, //窗口是否總是顯示在其他窗口之前
});
const size = screen.getPrimaryDisplay().workAreaSize; //獲取顯示器的寬高
const winSize = win.getSize(); //獲取窗口寬高
//設(shè)置窗口的位置 注意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文件
路徑: /src/renderer/store/modules/suspension.js
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
});
遺留問題
- 在軟件關(guān)閉之后重啟會導(dǎo)致懸浮窗口的位置重置 也曾嘗試在主進(jìn)程中使用store.js 但是不能用!
- 如果想解決這個(gè)問題 可以在渲染進(jìn)程中將拖動(dòng)的最后坐標(biāo)保存到storejs中
- 在渲染進(jìn)程給主進(jìn)程發(fā)送異步消息的時(shí)候?qū)⒆鴺?biāo)攜帶進(jìn)去 也可以使用nedb在主進(jìn)程中存儲坐標(biāo)!
github地址
使用electron制作百度網(wǎng)盤客戶端: https://github.com/lihaotian0607/baidupan
使用electron制作百度網(wǎng)盤懸浮窗: https://github.com/lihaotian0607/electron-suspension
目前這個(gè)開源代碼中沒有懸浮窗 有時(shí)間了會加上去!!!
相關(guān)文章
使用JS批量選中功能實(shí)現(xiàn)更改數(shù)據(jù)庫中的status狀態(tài)值(批量展示)
我們在開發(fā)項(xiàng)目的時(shí)候經(jīng)常會在后臺管理時(shí)用到批量展示功能來動(dòng)態(tài)的修改數(shù)據(jù)庫的值。下面以修改數(shù)據(jù)庫的status狀態(tài)值來實(shí)現(xiàn)批量展示功能2016-11-11
js控住DOM實(shí)現(xiàn)發(fā)布微博效果
這篇文章主要為大家詳細(xì)介紹了js控住DOM實(shí)現(xiàn)發(fā)布微博效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
ES6基礎(chǔ)之?dāng)?shù)組和對象的拓展實(shí)例詳解
這篇文章主要介紹了ES6基礎(chǔ)之?dāng)?shù)組和對象的拓展,結(jié)合實(shí)例形式詳細(xì)分析了ES6數(shù)組和對象拓展運(yùn)算符、拓展方法的使用及相關(guān)操作技巧,需要的朋友可以參考下2019-08-08
js setTimeout()函數(shù)介紹及應(yīng)用以倒計(jì)時(shí)為例
setTimeout() 方法用于在指定的毫秒數(shù)后調(diào)用函數(shù)或計(jì)算表達(dá)式,下面有個(gè)倒計(jì)時(shí)的示例,需要的朋友可以學(xué)習(xí)下2013-12-12
微信小程序Page中data數(shù)據(jù)操作和函數(shù)調(diào)用方法
這篇文章主要介紹了微信小程序Page中data數(shù)據(jù)操作和函數(shù)調(diào)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
使用JSX實(shí)現(xiàn)Carousel輪播組件的方法(前端組件化)
做這個(gè)輪播圖的組件,我們先從一個(gè)最簡單的 DOM 操作入手。使用 DOM 操作把整個(gè)輪播圖的功能先實(shí)現(xiàn)出來,然后在一步一步去考慮怎么把它設(shè)計(jì)成一個(gè)組件系統(tǒng)2021-04-04
layer iframe 設(shè)置關(guān)閉按鈕的方法
今天小編就為大家分享一篇layer iframe 設(shè)置關(guān)閉按鈕的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09

