[英]Windows: Is there NPM modules that can getActiveWindow, and setWindowPos
我有一个专用于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
我正在寻找可以执行此操作的任何模块并找到了 winctl ,但我无法使其工作。
题
有没有模块可以访问Windows active window?
如果有的话,能否提供一个代码示例?
先感谢您!
这就是我使用ffi-napi解决问题的方法。
我在代码上添加了注释以使其易于理解。
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
);
我在名为Sōkan的小项目中使用此代码。
也许您可以使用remote
和getFocusedWindow()
获取或设置为 window position。
const remote = require('electron').remote;
let win = remote.BrowserWindow.getFocusedWindow();
浏览器窗口.getFocusedWindow()
https://www.electronjs.org/docs/api/browser-window#browserwindowgetfocusedwindow
console.log(win.getPosition());
win.setPosition(0, 0);
赢.getPosition()
https://www.electronjs.org/docs/api/browser-window#wingetposition
赢.setPosition()
https://www.electronjs.org/docs/api/browser-window#winsetpositionx-y-animate
但使用remote
时要小心。 目前,Electron 将其作为“禁用远程模块”的安全建议。
清单:安全建议
https://www.electronjs.org/docs/tutorial/security#checklist-security-recommendations
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.