gpt4 book ai didi

node.js - Node ffi - GetWindowRect

转载 作者:行者123 更新时间:2023-12-03 12:38:52 28 4
gpt4 key购买 nike

我正在构建一个 Windows Electron 应用程序,它将移动和调整事件窗口的大小。
我正在使用 ffi-napi访问 用户32 特定的函数,例如 GetForegroundWindow、ShowWindow 和 SetWindowPos。

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

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

// get active window
const activeWindow = user32.GetForegroundWindow();
// force active window to restore mode
user32.ShowWindow(activeWindow, 9);
// set window position
user32.SetWindowPos(
activeWindow,
0,
0, // 0 left have margin on left 🤔
0, // 0 top have margin on top 🤔
1024,
768,
0x4000 | 0x0020 | 0x0020 | 0x0040
);
现在我的问题🙂
我需要获取事件窗口尺寸。我在网上搜索,我找到了 GetWindowRect。
问题是当我将它添加到 时用户32 函数,我不确定第二个参数(RECT)需要什么。
// create foreign function
const user32 = new ffi.Library('user32', {
'GetForegroundWindow': ['long', []],
'ShowWindow': ['bool', ['long', 'int']],
+ 'GetWindowRect': ['bool', ['int', 'rect']],
'SetWindowPos': ['bool', ['long', 'long', 'int', 'int', 'int', 'int', 'uint']]
});
...
// get active window dimensions
user32.GetWindowRect(activeWindow, 0);
...
这是我得到的错误:
A javascript error occurred in the main process

Uncaught Exemption:
TypeError: error setting argument 2 - writePointer: Buffer instance expected as
third argument at Object.writePointer
希望任何人都可以帮助我。先感谢您。 🙏

最佳答案

这就是我如何解决我的问题🙂

...
// 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;
...
我在名为 Sōkan 的小项目中使用此代码.

关于node.js - Node ffi - GetWindowRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64416980/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com