elctron手写拖动

vue作为渲染进程使用remote

yarn add @electron/remote --dev

主进程代码

const remote = require("@electron/remote/main"); 
remote.initialize();


function init_vue_app_login() {
  vue_app_login = new BrowserWindow({
    webPreferences: {
      enableRemoteModule: true,
      nodeIntegration: true,
      contextIsolation: false,
      // 禁用同源策略,允许跨域
      webSecurity: false
    }
  });
  vue_app_login.loadURL(
    isDev
      ? "http://localhost:3000"
      : `file://${path.join(__dirname, "../dist/index.html")}`
  );

  remote.enable(vue_app_login.webContents); 
}

渲染进程代码

const remote = require("@electron/remote");

function test() {
  remote.getCurrentWindow().setSize(1000, 900);
  console.log(remote,"remote==========");
}

手写拖动

const remote = require("@electron/remote");
const win = remote.getCurrentWindow();

setup(){
	let isDrag = ref(false);
let moveX = ref(0);
let moveY = ref(0);
let winBounds = ref();


const handleHeaderMouseDown = () => {
    document.body.addEventListener('mousedown', (e) => {
        isDrag = true
        moveX = e.screenX
        moveY = e.screenY
        winBounds = win.getBounds();
    })
    window.addEventListener('mousemove', (e) => {
        if (!isDrag) {
            return
        }
        const { x, y } = winBounds;
        let newWinX = x + (e.screenX - moveX)
        let newWinY = y + (e.screenY - moveY)
        // win.setPosition(Math.min(size.width-346,Math.max(0,x)), Math.min(size.height-78,Math.max(0,y)))
        win.setBounds({
            x: newWinX,
            y: newWinY
        })
    })
    window.addEventListener('mouseup', () => {
        isDrag = false
    })
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值