首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >节点ffi - GetWindowRect

节点ffi - GetWindowRect
EN

Stack Overflow用户
提问于 2020-10-18 18:38:08
回答 1查看 868关注 0票数 4

我正在构建一个Windows电子应用程序,它将移动和调整活动窗口的大小。

我使用ffi-napi访问user32特定的函数,例如GetForegroundWindow、ShowWindow和SetWindowPos。

代码语言:javascript
复制
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)需要什么。

代码语言:javascript
复制
// 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);
...

这是我正在犯的错误:

代码语言:javascript
复制
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

希望有人能帮我。提前谢谢你。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-19 06:53:37

我就是这样解决我的问题的

代码语言:javascript
复制
...
// 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ō堪萨斯州的小项目中使用了这段代码。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64416980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档