Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows DwmGetWindowAttribute function #619

Open
genesisneo opened this issue Apr 9, 2021 · 0 comments
Open

Windows DwmGetWindowAttribute function #619

genesisneo opened this issue Apr 9, 2021 · 0 comments

Comments

@genesisneo
Copy link

Hi, I hope everyone fine and well.
I am new to ffi and I have a small project using electron and that can move Windows current active window.
I am planning to use the same solution from StackOverflow and I manage to do the GetWindowRect, however, I can't find a solution to do the DwmGetWindowAttribute. Below is my code to the said project.

...
// user32 functions
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']]
});

// dwmapi functions
const dwmapi = new ffi.Library('dwmapi', {
  'DwmGetWindowAttribute': ['bool', ['long', 'long', 'pointer', 'long']]
});

// 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) {
  // reference: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowrect
  const rectPointer = Buffer.alloc(16);
  const getWindowRect = user32.GetWindowRect(handle, rectPointer);

  // ⚠ can't get the value
  // reference: https://docs.microsoft.com/en-us/windows/win32/api/dwmapi/nf-dwmapi-dwmgetwindowattribute
  const extendedRectPointer = Buffer.alloc(16);
  dwmapi.DwmGetWindowAttribute(
    handle,
    'DWMWA_EXTENDED_FRAME_BOUNDS',
    extendedRectPointer,
    pointerToRect(rectPointer)
  );
  console.log('>>>', extendedRectPointer);

  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;
...

Hope someone can help me. Thank you and keep safe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant