简体   繁体   English

Windows:有没有NPM模块可以getActiveWindow,setWindowPos

[英]Windows: Is there NPM modules that can getActiveWindow, and setWindowPos

I have a project exclusively for Windows that needs the following:我有一个专用于Windows的项目,需要以下内容:

1 - Get the active window
2 - Get the dimension of the active window
3 - From the dimension I get, update the (x, y, w, h)
4 - Set the active window to 'restore' mode
5 - Set the new position of the active window

I am looking for any modules that can do this and found winctl but I can't make it work.我正在寻找可以执行此操作的任何模块并找到了 winctl ,但我无法使其工作。

QUESTION
Are there any modules that can access the Windows active window?有没有模块可以访问Windows active window?
If there is, can you please provide a code sample?如果有的话,能否提供一个代码示例?

Thank you in advance!先感谢您!

This is how I solve my issue by using ffi-napi .这就是我使用ffi-napi解决问题的方法。
I added comments on the code to make it easy to understand.我在代码上添加了注释以使其易于理解。

const ffi = require('ffi-napi');

// create foreign function
const user32 = new ffi.Library('user32', {
  'GetForegroundWindow': ['long', []],
  'ShowWindow': ['bool', ['long', 'int']],
  'GetWindowRect': ['bool', ['long', 'pointer']],
  'SetWindowPos': ['bool', ['long', 'long', 'int', 'int', 'int', 'int', 'uint']]
});

// create rectangle from pointer
const pointerToRect = function (rectPointer) {
  const rect = {};
  rect.left = rectPointer.readInt16LE(0);
  rect.top = rectPointer.readInt16LE(4);
  rect.right = rectPointer.readInt16LE(8);
  rect.bottom = rectPointer.readInt16LE(12);
  return rect;
}

// obtain window dimension
const getWindowDimensions = function (handle) {
  const rectPointer = Buffer.alloc(16);
  const getWindowRect = user32.GetWindowRect(handle, rectPointer);
  return !getWindowRect
    ? null
    : pointerToRect(rectPointer);
}

// get active window
const activeWindow = user32.GetForegroundWindow();

// get window dimension
const activeWindowDimensions = getWindowDimensions(activeWindow);

// get active window width and height
const activeWindowWidth = activeWindowDimensions.right - activeWindowDimensions.left;
const activeWindowHeight = activeWindowDimensions.bottom - activeWindowDimensions.top;

// center the active window on the screen
const activeWindowX = (1920 / 2) - (activeWindowWidth / 2);
const activeWindowY = (1080 / 2) - activeWindowHeight / 2);

// force active window to restore mode
user32.ShowWindow(activeWindow, 9);

// set window position based on bounds values
user32.SetWindowPos(
  activeWindow,
  0,
  activeWindowX,
  activeWindowY,
  activeWindowWidth,
  activeWindowHeight,
  0x4000 | 0x0020 | 0x0020 | 0x0040
);

I use this code on my small project called Sōkan .我在名为Sōkan的小项目中使用此代码。

Perhaps you can get or set to window position by using remote and getFocusedWindow() .也许您可以使用remotegetFocusedWindow()获取或设置为 window position。

const remote = require('electron').remote;
let win = remote.BrowserWindow.getFocusedWindow();

BrowserWindow.getFocusedWindow()浏览器窗口.getFocusedWindow()
https://www.electronjs.org/docs/api/browser-window#browserwindowgetfocusedwindow https://www.electronjs.org/docs/api/browser-window#browserwindowgetfocusedwindow

console.log(win.getPosition());
win.setPosition(0, 0);

win.getPosition()赢.getPosition()
https://www.electronjs.org/docs/api/browser-window#wingetposition https://www.electronjs.org/docs/api/browser-window#wingetposition

win.setPosition()赢.setPosition()
https://www.electronjs.org/docs/api/browser-window#winsetpositionx-y-animate https://www.electronjs.org/docs/api/browser-window#winsetpositionx-y-animate

But be careful when using remote .但使用remote时要小心。 Currently, Electron makes it a security recommendation with "Disable the remote module".目前,Electron 将其作为“禁用远程模块”的安全建议。

Checklist: Security Recommendations清单:安全建议
https://www.electronjs.org/docs/tutorial/security#checklist-security-recommendations https://www.electronjs.org/docs/tutorial/security#checklist-security-recommendations

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM