electron窗口相关操作(放大缩小退出,可拖动,可resize等)

Posted chenbeibei520

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了electron窗口相关操作(放大缩小退出,可拖动,可resize等)相关的知识,希望对你有一定的参考价值。

如下是对窗口最大化,最小化等相关操作:

import  ipcMain, ipcRenderer, remote  from ‘electron‘
import is from ‘electron-is‘

// ipc 通信发送的窗口状态改变事件的channel名称
export const windowStateChangeChannel = ‘window-state-changed‘

// window的当前状态
export const WINDOW_STATE = 
  FULLSCREEN: ‘full-screen‘,
  MAXIMIZED: ‘maximized‘,
  MINIMIZED: ‘minimized‘,
  HIDDEN: ‘hidden‘,
  NORMAL: ‘normal‘,


// window可执行的操作,通过发送消息触发
const windowAction = 
  maximize: ‘window-maximize‘,
  unmaximize: ‘window-unmaximize‘,
  minimize: ‘window-minimize‘,
  close: ‘window-close‘,


/**
* 获取window的状态
* @param window对象 window
*/
export function getWindowState(window) 
  if (window.isFullScreen()) 
    return WINDOW_STATE.FULLSCREEN
   if (window.isMaximized()) 
    return WINDOW_STATE.MAXIMIZED
   if (window.isMinimized()) 
    return WINDOW_STATE.MINIMIZED
   if (!window.isVisible()) 
    return WINDOW_STATE.HIDDEN
  

  return WINDOW_STATE.NORMAL


/**
* 发送一个 window-state-changed 消息到 renderer 进程
* @param \* window
* @param \* state
*/
function sendWindowStateEvent(window, state) 
  window.webContents.send(windowStateChangeChannel, state)


/**
 * 注册 window 状态变化后事件,它会发送一个消息到 renderer 进程
 * @param window对象 window
 */
export function registerWindowStateChangedEvents(window) 
  window.on(‘enter-full-screen‘, () => sendWindowStateEvent(window, ‘full-screen‘))
  window.on(‘leave-full-screen‘, () => sendWindowStateEvent(window, ‘normal‘))
  window.on(‘maximize‘, () => sendWindowStateEvent(window, ‘maximized‘))
  window.on(‘minimize‘, () => sendWindowStateEvent(window, ‘minimized‘))
  window.on(‘unmaximize‘, () => sendWindowStateEvent(window, ‘normal‘))
  window.on(‘restore‘, () => sendWindowStateEvent(window, ‘normal‘))
  window.on(‘hide‘, () => sendWindowStateEvent(window, ‘hidden‘))
  window.on(‘show‘, () => sendWindowStateEvent(window, ‘normal‘))


/**
 * 注册 window 状态变化动作,使用 ipc.send 对应的消息触发*
 * @param window对象 window
 */
export function registerWindowStateChangeActions(window) 
  // 窗口最小化
  ipcMain.on(windowAction.minimize, () => 
    window.minimize()
  )

  // 窗口最大化
  ipcMain.on(windowAction.maximize, () => 
    window.maximize()
  )

  // 窗口取消最大化
  ipcMain.on(windowAction.unmaximize, () => 
    window.unmaximize()
  )

  // 窗口关闭
  ipcMain.on(windowAction.close, () => 
    window.close()
  )


/**
 *  生成带有promise的操作窗口的函数,可以进一步处理事件结束后的逻辑
 * @param 窗口可执行的操作 windowAction
 */
function generatePromisedWindowStateFunc(action) 
  return () => 
    return new Promise((resolve) => 
      ipcRenderer.send(action)
      ipcRenderer.once(windowStateChangeChannel, (event, args) => 
        resolve(args)
      )
    )
  


/**
 * 生成不带有promise的操作窗口函数,只负责触发事件
 * @param 窗口可执行的操作 windowAction
 */
function generateWindowStateFunc(action) 
  return () => 
    ipcRenderer.send(action)
  


/**
 * 最大化窗口的方法,因为windows和macOS之间的差异,单独写成一个函数
 */
function handleMaximizeWindow() 
  if (is.windows()) 
    remote.getCurrentWindow().maximize()
    return Promise.resolve(WINDOW_STATE.MAXIMIZED)
  

  return new Promise((resolve) => 
    ipcRenderer.send(windowAction.maximize)
    ipcRenderer.once(windowStateChangeChannel, (event, args) => 
      resolve(args)
    )
  )


/**
 * 窗口操作方法,包括最大化,最小化,关闭
 * 每个方法返回一个promise,方便处理后续逻辑
 */
export const windowStateActionResponse = 
  maximize: handleMaximizeWindow,
  unmaximize: generatePromisedWindowStateFunc(windowAction.unmaximize),
  minimize: generatePromisedWindowStateFunc(windowAction.minimize),
  close: generatePromisedWindowStateFunc(windowAction.close),


/**
 * 窗口操作方法,包括最大化,最小化,关闭
 * 只发送消息,不处理成功之后的回调
 */
export const WindowStateAction = 
  maximize: generateWindowStateFunc(windowAction.maximize),
  unmaximize: generateWindowStateFunc(windowAction.unmaximize),
  minimize: generateWindowStateFunc(windowAction.minimize),
  close: generateWindowStateFunc(windowAction.close),


/**
 * 给窗口改变事件增加监听
 * @param 事件触发后的回调 handle
 */
export function listenToWindowStateChange(handle) 
  ipcRenderer.on(windowStateChangeChannel, handle)
  return () => 
    ipcRenderer.removeListener(windowStateChangeChannel, handle)
  

 

下面是electron配置:

import  BrowserWindow, screen  from ‘electron‘

function createWebRTCInternalWindow() 
  const displayWorkAreaSize = screen.getAllDisplays()[0].workArea

  const options = 
    x: displayWorkAreaSize.x,
    y: displayWorkAreaSize.y,
    width: displayWorkAreaSize.width,
    height: displayWorkAreaSize.height,
    resizable: true,
    movable: true,
    fullscreenable: false,
    enableLargerThanScreen: false,
    frame: true,
    transparent: false,
    alwaysOnTop: false,
    hasShadow: false,
    minWidth: 1000,
    minHeight: 648,
    webPreferences: 
      devTools: true,
      webSecurity: false,
      plugins: true,
      experimentalFeatures: true,
      experimentalCanvasFeatures: true,
      minimumFontSize: 10,
    ,
  

  const mainWindow = new BrowserWindow(options)

  return mainWindow


export default createWebRTCInternalWindow
import  BrowserWindow, screen  from ‘electron‘

function createBrowserWindow() 
  const displayWorkAreaSize = screen.getAllDisplays()[0].workArea

  const options = 
    width: parseInt(displayWorkAreaSize.width * 0.85, 10),
    height: parseInt(displayWorkAreaSize.height * 0.85, 10),
    center: true,
    resizable: true,
    movable: true,
    fullscreenable: false,
    enableLargerThanScreen: false,
    frame: false,
    transparent: true,
    alwaysOnTop: false,
    clickThrough: ‘pointer-events‘,
    acceptFirstMouse: true,
    hasShadow: false,
    minWidth: 1000,
    minHeight: 648,
    webPreferences: 
      devTools: true,
      webSecurity: false,
      plugins: true,
      experimentalFeatures: true,
      experimentalCanvasFeatures: true,
      minimumFontSize: 10,
    ,
  

  const mainWindow = new BrowserWindow(options)

  return mainWindow


export default createBrowserWindow

如上配置中可设置一系列初始值,其中包括 是否可resize,是否可拖动等。如果想在其它地方修改初始值,可按照如下实现:

const currentWindow = require(‘electron‘).remote.getCurrentWindow()

currentWindow.setResizable(false)
currentWindow.setMovable(false)
// 禁止Windows下双击最大化和调用maximize行为不一致导致的窗口尺寸变化
currentWindow.setMaximizable(false)

 

以上是关于electron窗口相关操作(放大缩小退出,可拖动,可resize等)的主要内容,如果未能解决你的问题,请参考以下文章

电脑应用拖到上边放大

在WORD里拖动鼠标滑轮,页面不上上下走,而是放大缩小,怎么回事?

android中ImageView放大和缩小相关问题?

C# GDI绘制矩形框,鼠标左键拖动可移动矩形框,滚轮放大缩小矩形框

Qt自定义主窗口的放大,缩小,关闭功能

js进行数字图像处理:亮度对比度马赛克画笔放大缩小镜像贴纸旋转颜色值显示