我正在构建一个Windows电子应用程序,它将移动和调整活动窗口的大小。
我使用ffi-napi访问user32特定的函数,例如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。
问题是当我将它添加到user32函数中时,我不确定第二个param (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希望有人能帮我。提前谢谢你。
发布于 2021-01-19 06:53:37
我就是这样解决我的问题的
...
// 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ō堪萨斯州的小项目中使用了这段代码。
https://stackoverflow.com/questions/64416980
复制相似问题